ラズパイマウスのIO専用名前空間を作る

RaspiIO名前空間

これまで作った、LED、ボタンスイッチ、距離の測定のクラスをまとめてRaspiIO名前空間として登録します。これからは、この名前空間のクラスを用いてプログラムの開発を行います。

RaspiIO名前空間
using System;
using RaspberryPiDotNet;
using System.Threading;

namespace RaspiIO
{
  public class LEDs
  {
    GPIOMem[] LED = new GPIOMem[4];
    public LEDs()
    {
      LED[0] = new GPIOMem(GPIOPins.V2_Pin_P1_22);
      LED[1] = new GPIOMem(GPIOPins.V2_Pin_P1_18);
      LED[2] = new GPIOMem(GPIOPins.V2_Pin_P1_16);
      LED[3] = new GPIOMem(GPIOPins.V2_Pin_P1_12);

      for (int i = 0; i < LED.Length; i++)
        LED[i].PinDirection = GPIODirection.Out;
    }

    public void Write(int n, bool state)
    {
      LED[n].Write(state);
    }
  }

  public class Buttons
  {
    GPIOMem[] SW = new GPIOMem[3];

    private const GPIOPins SW0 = GPIOPins.V2Plus_Pin_P1_38;
    private const GPIOPins SW1 = GPIOPins.V2Plus_Pin_P1_37;
    private const GPIOPins SW2 = GPIOPins.V2Plus_Pin_P1_40;

    public Buttons()
    {
      SW[0] = new GPIOMem(SW0);
      SW[1] = new GPIOMem(SW1);
      SW[2] = new GPIOMem(SW2);
      for (int i = 0; i < SW.Length; i++)
        SW[i].PinDirection = GPIODirection.In;
    }

    public PinState value(int n)
    {
      return SW[n].Read();
    }

    public int waitPress(out int n)
    {
      for (int i = 0; i < SW.Length; i++)
      {
        if (SW[i].Read() == PinState.Low)
        {
           n = i;
           return i;
        }
      }
      n = -1;
      return n;
    }
    public int waitButton()
    {
      int n;
      while (true)
      {
        if (waitPress(out n) != -1)
        {
          break;
        }
        Thread.Sleep(50);
      }
      return n;
    }
  }
  public class ADcon
  {
    static GPIOMem[] IrLED = new GPIOMem[4];
    static GPIOMem SPICLK = new GPIOMem(GPIOPins.V2_Pin_P1_23);
    static GPIOMem SPIMISO = new GPIOMem(GPIOPins.V2_Pin_P1_21);
    static GPIOMem SPIMOSI = new GPIOMem(GPIOPins.V2_Pin_P1_19);
    static GPIOMem SPICS = new GPIOMem(GPIOPins.V2_Pin_P1_24);
    public static double[] distance = new double[4];

    public ADcon()
    {
      IrLED[0] = new GPIOMem(GPIOPins.V2_Pin_P1_07);
      IrLED[1] = new GPIOMem(GPIOPins.V2_Pin_P1_11);
      IrLED[2] = new GPIOMem(GPIOPins.V2_Pin_P1_13);
      IrLED[3] = new GPIOMem(GPIOPins.V2_Pin_P1_15);
      for (int i = 0; i < IrLED.Length; i++)
        IrLED[i].PinDirection = GPIODirection.Out;

      SPICLK.PinDirection = GPIODirection.Out;
      SPIMISO.PinDirection = GPIODirection.In;
      SPIMOSI.PinDirection = GPIODirection.Out;
      SPICS.PinDirection = GPIODirection.Out;
    }

    public static void adGet(object state)
    {
      MCP3008 mcp3208;

      for (int adcnum = 0; adcnum < 4; adcnum++)
      {
        IrLED[adcnum].Write(true);
        Thread.Sleep(10);
        mcp3208 = new MCP3008(adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS);
        distance[adcnum] = mcp3208.AnalogToDigital;
        IrLED[adcnum].Write(false);
      }
    }
    public static void turnOut()
    {
      for (int i = 0; i < IrLED.Length; i++)
        IrLED [i].Write (0);
    }
  }
}
 1 
 2 
 3 
 4 
 5 名前空間の設定
 6 
 7 LEDのクラス
 8 
 9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 ボタンスイッチのクラス
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 距離センサーのクラス
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101
102
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119