Category Archives: .Net Micro
Interrupt Port Handling
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// this moved out here so it can be used by other methods static OutputPort LED; public static void Main() { LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, true); // the pin will generate interrupt on high and low edges InterruptPort IntButton = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.LDR, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth); // add an interrupt handler to the pin IntButton.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt); //do anything you like here Thread.Sleep(Timeout.Infinite); } static void IntButton_OnInterrupt(uint port, uint state, DateTime time) { // set LED to the switch state LED.Write(state == 0); } |
Posted in .Net Micro
Comments Off on Interrupt Port Handling
Switch Statement
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
DateTime currentTime = DateTime.Now; int day = (int)currentTime.DayOfWeek; switch (day) { case 0: Debug.Print("Sunday"); break; case 1: Debug.Print("Monday"); break; case 2: Debug.Print("Tuesday"); break; case 3: Debug.Print("Wednsday"); break; case 4: Debug.Print("Thursday"); break; case 5: Debug.Print("Friday"); break; case 6: Debug.Print("Saturday"); break; default: Debug.Print("We should never see this"); break; } |
Posted in .Net Micro
Comments Off on Switch Statement
Enumeration
An Enumeration is very similar to a constant.
|
1 2 3 4 5 6 7 8 9 10 |
enum Command { MOVE = 1, STOP = 2, RIGHT = 3, LEFT = 4, } //now we can send a command... SendCommand(Command.LEFT); SendCommand(Command.STOP); |
Posted in .Net Micro
Comments Off on Enumeration
Classes
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class MyClass { int Add(int a, int b) { return a + b; } } Example of a public method using a private method public int Add(int a, int b) { // the object can use private methods // inside the class only DoSomething(); return a + b; } private void DoSomething() { } |
Posted in .Net Micro
Comments Off on Classes
Methods
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
int Add(int var1, int var2) { int var3; var3 = var1 + var2; return var3; } string Add(int var1, int var2) { int var3; var3 = var1 + var2; string MyString; MyString = var3.ToString(); return MyString; } string Add(int var1, int var2) { return (var1+var2).ToString(); } |
Posted in .Net Micro
Comments Off on Methods
LED example
using System; //use base classes using System.Threading; //use threading classes using Microsoft.SPOT; //use smart personal objects technology classes using Microsoft.SPOT.Hardware; //use hardware related SPOT classes using SecretLabs.NETMF.Hardware; //use Secret Labs hardware framework using SecretLabs.NETMF.Hardware.Netduino; //use the Netduino specific classes namespace … Continue reading
Posted in .Net Micro
Comments Off on LED example
Arrays
Example handling arrays.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
namespace ArraySample { public class Program { public static void Main() { //concatenate two arbitrary arrays char[] charArray1 = new char[] { 'A', 'B', 'C', 'D' }; char[] charArray2 = new char[] { 'E', 'F', 'G', 'H' }; char[] charArray3 = new char[charArray1.Length + charArray2.Length]; Array.Copy(charArray1, 0, charArray3, 0, charArray1.Length); Array.Copy(charArray2, 0, charArray3, charArray1.Length,charArray2.Length); } } } |
Posted in .Net Micro
Comments Off on Arrays
Blinking LED
Example for blinking the onboard LED.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//Libraries that our program uses using System; //use base classes using System.Threading; //use threading classes using Microsoft.SPOT; //use smart personal objects technology classes using Microsoft.SPOT.Hardware; //use hardware related SPOT classes using SecretLabs.NETMF.Hardware; //use Secret Labs hardware framework using SecretLabs.NETMF.Hardware.Netduino; //use the Netduino specific classes namespace NCIR01 /// Define the namespace we are in /// { public class Program { public static void Main() /// The Main loop (run at power up) /// { /// Setup Portion of Program, runs once at /// OutputPort led = new OutputPort(Pins.GPIO_PIN_D13, false); //define our LED as being connected to pin 13 while (true) /// Do Forever /// { led.Write(true); //turn led on Thread.Sleep(250); //wait 250 milliseconds led.Write(false); //turn light off Thread.Sleep(250); //wait 250 milliseconds } /// Close Forever Loop /// } /// Close the Main() Loop /// } /// Close the Program Loop /// } /// Close the Namespace Loop /// |
Posted in .Net Micro
Comments Off on Blinking LED
Byte to Hex Conversion
Converting a Byte Value into a Hexadecimal String
|
1 2 3 4 5 6 7 8 |
public static string ByteToHex(byte b) { const string hex = "0123456789ABCDEF"; int lowNibble = b & 0x0F; int highNibble = (b & 0xF0) >> 4; string s = new string(new char[] { hex[highNibble], hex[lowNibble] }); return s; } |
Posted in .Net Micro
Comments Off on Byte to Hex Conversion
Text Encoding
Using System.Text.Encoding
|
1 2 3 4 |
string text = "Hello World"; byte[] bytes = Encoding.UTF8.GetBytes(text); string restoredText = new string(Encoding.UTF8.GetChars(bytes)); Debug.Print(restoredText); |
Posted in .Net Micro
Comments Off on Text Encoding