Advertisement
Guest User

Untitled

a guest
Aug 14th, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.76 KB | None | 0 0
  1. using System;
  2. using Microsoft.SPOT;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using Microsoft.SPOT.Hardware;
  6. using SecretLabs.NETMF.Hardware;
  7. using SecretLabs.NETMF.Hardware.Netduino;
  8.  
  9. namespace NetduinoApplication2
  10. {
  11.     class Display
  12.     {
  13.         private bool on, off;
  14.  
  15.         OutputPort a = new OutputPort(Pins.GPIO_PIN_D0, false);
  16.         OutputPort b = new OutputPort(Pins.GPIO_PIN_D1, false);
  17.         OutputPort c = new OutputPort(Pins.GPIO_PIN_D2, false);
  18.         OutputPort d = new OutputPort(Pins.GPIO_PIN_D3, false);
  19.         OutputPort e = new OutputPort(Pins.GPIO_PIN_D4, false);
  20.         OutputPort f = new OutputPort(Pins.GPIO_PIN_D5, false);
  21.         OutputPort g = new OutputPort(Pins.GPIO_PIN_D6, false);
  22.         OutputPort point = new OutputPort(Pins.GPIO_PIN_D7, false);
  23.  
  24.         public static OutputPort rightDigit = new OutputPort(Pins.GPIO_PIN_D9, false);
  25.         public static OutputPort leftDigit = new OutputPort(Pins.GPIO_PIN_D10, false);
  26.  
  27.  
  28.         public void Reset() // resets display to off
  29.         {
  30.             a.Write(off);
  31.             b.Write(off);
  32.             c.Write(off);
  33.             d.Write(off);
  34.             e.Write(off);
  35.             f.Write(off);
  36.             g.Write(off);
  37.             point.Write(off);
  38.         }
  39.  
  40.  
  41.         public void Write(string inputString)
  42.         {
  43.             // turns off both digits
  44.             rightDigit.Write(false);
  45.             leftDigit.Write(false);
  46.            
  47.             if (inputString.Length == 1)
  48.             {
  49.                 bool[] temp = CharacterCode(inputString);
  50.  
  51.                 rightDigit.Write(true);  // picks which digit to write to
  52.  
  53.                 a.Write(temp[0]);
  54.                 b.Write(temp[1]);
  55.                 c.Write(temp[2]);
  56.                 d.Write(temp[3]);
  57.                 e.Write(temp[4]);
  58.                 f.Write(temp[5]);
  59.                 g.Write(temp[6]);
  60.             }
  61.  
  62.             // seperates the input to two characters and gets their codes for the display
  63.             bool[] digit1 = CharacterCode(inputString.Substring(0, 1));
  64.             bool[] digit2 = CharacterCode(inputString.Substring(1, 1));
  65.  
  66.  
  67.             Debug.Print("inputString: " + inputString);
  68.             Debug.Print("substring 0: " + inputString.Substring(0, 1));
  69.             Debug.Print("substring 1: " + inputString.Substring(1, 1));
  70.                          
  71.  
  72.             while (true) // updates the display to show the 2 digit number, switching between them quickly
  73.                          // How do I do this in another thread until an update is 'pushed' to the display?
  74.             {
  75.                 leftDigit.Write(false);
  76.  
  77.                 a.Write(digit2[0]);
  78.                 b.Write(digit2[1]);
  79.                 c.Write(digit2[2]);
  80.                 d.Write(digit2[3]);
  81.                 e.Write(digit2[4]);
  82.                 f.Write(digit2[5]);
  83.                 g.Write(digit2[6]);
  84.  
  85.                 rightDigit.Write(true);
  86.  
  87.                 Thread.Sleep(5);
  88.  
  89.                 rightDigit.Write(false);
  90.  
  91.                 a.Write(digit1[0]);
  92.                 b.Write(digit1[1]);
  93.                 c.Write(digit1[2]);
  94.                 d.Write(digit1[3]);
  95.                 e.Write(digit1[4]);
  96.                 f.Write(digit1[5]);
  97.                 g.Write(digit1[6]);
  98.  
  99.                 leftDigit.Write(true);
  100.                 Thread.Sleep(5);
  101.             }
  102.  
  103.  
  104.         }
  105.  
  106.         public void Write(int input)
  107.         {
  108.             Write(input.ToString());
  109.         }
  110.  
  111.  
  112.         public void Mode(string mode) // configuration for common anode or common cathode displays
  113.         {
  114.             if (mode == "commonAnode")
  115.             {
  116.                 on = false;
  117.                 off = true;
  118.             }
  119.             else if (mode == "commonCathode")
  120.             {
  121.                 on = true;
  122.                 off = false;
  123.             }
  124.             this.Reset(); // resets the display to off when the mode is set
  125.         }
  126.  
  127.  
  128.         private bool[] CharacterCode(string charInput)  // returns an array of booleans to code for a character
  129.         {
  130.             switch (charInput)
  131.             {
  132.                 // Segments of the display
  133.                 //                   A   B   C   D   E   F   G      
  134.                 case "0":
  135.                     bool[] num0 = { on, on, on, on, on, on, off };
  136.                     return num0;
  137.  
  138.                 case "1":
  139.                     bool[] num1 = { off, on, on, off, off, off, off };
  140.                     return num1;
  141.  
  142.                 case "2":
  143.                     bool[] num2 = { on, on, off, on, on, off, on };
  144.                     return num2;
  145.  
  146.                 case "3":
  147.                     bool[] num3 = { on, on, on, on, off, off, on };
  148.                     return num3;
  149.  
  150.                 case "4":
  151.                     bool[] num4 = { off, on, on, off, off, on, on };
  152.                     return num4;
  153.  
  154.                 case "5":
  155.                     bool[] num5 = { on, off, on, on, off, on, on };
  156.                     return num5;
  157.  
  158.                 case "6":
  159.                     bool[] num6 = { on, off, on, on, on, on, on };
  160.                     return num6;
  161.  
  162.                 case "7":
  163.                     bool[] num7 = { on, on, on, off, off, off, off };
  164.                     return num7;
  165.  
  166.                 case "8":
  167.                     bool[] num8 = { on, on, on, on, on, on, on };
  168.                     return num8;
  169.  
  170.                 case "9":
  171.                     bool[] num9 = { on, on, on, off, off, on, on };
  172.                     return num9;
  173.  
  174.                 case "A":
  175.                     bool[] A = { on, on, on, off, on, on, on };
  176.                     return A;
  177.  
  178.                 case "B":
  179.                     bool[] B = { on, on, on, on, on, on, on };
  180.                     return B;
  181.  
  182.                 case "C":
  183.                     bool[] C = { on, off, off, on, on, on, off };
  184.                     return C;
  185.  
  186.                 case "D":
  187.                     bool[] D = { on, on, on, on, on, on, off };
  188.                     return D;
  189.  
  190.                 case "E":
  191.                     bool[] E = { on, off, off, on, on, on, on };
  192.                     return E;
  193.  
  194.                 case "F":
  195.                     bool[] F = { on, off, off, off, on, on, on };
  196.                     return F;
  197.  
  198.                 case "G":
  199.                     bool[] G = { on, off, on, on, on, on, on };
  200.                     return G;
  201.  
  202.                 case "H":
  203.                     bool[] H = { off, on, on, off, on, on, on };
  204.                     return H;
  205.  
  206.                 case "I":
  207.                     bool[] I = { off, on, on, off, off, off, off };
  208.                     return I;
  209.  
  210.                 default:
  211.                     bool[] error = { off, off, off, off, off, off, off };
  212.                     return error;
  213.             }
  214.         }
  215.  
  216.  
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement