-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
151 lines (125 loc) · 4.15 KB
/
Program.cs
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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
namespace EST_Panel
{
public class ESTPanelSet
{
private SPI.Configuration _Config = null;
SPI SPIBus;
private byte[] _Buffer;
private byte[] _OutBuff;
public byte[] _InBuff;
private OutputPort _Latch;
private int _Modules;
private ESTPanel[] _Panels;
public enum Lights
{
Alarm = 0,
Trouble = 1,
Supervisory = 2
}
public ESTPanelSet(SPI.SPI_module SPIModule, Cpu.Pin ChipSelectPort, int NumModules, Cpu.Pin LatchPort)
{
_Config = new SPI.Configuration(ChipSelectPort, false, 0, 0, false, false, 10000, SPIModule);
_Latch = new OutputPort(LatchPort, false);
_Buffer = new byte[(NumModules * 17)];
_InBuff = new byte[17];
_Modules = NumModules;
_Panels = new ESTPanel[_Modules];
for (int Panel = 0; Panel < _Modules; Panel++)
_Panels[Panel] = new ESTPanel(this, Panel);
SPIBus = new SPI(_Config);
}
public ESTPanel[] Panels
{
get
{
return _Panels;
}
}
public void Refresh()
{
for (int Panel = 0; Panel < _Modules; Panel++)
{
SPIBus.WriteRead(_Buffer, (Panel * 17), 17, _InBuff, 0, 17, 0); //Exchange IO Data with next panel
Array.Copy(_InBuff, 0, _Buffer, Panel * 17, 5); // Copy button data in to buffer
}
_Latch.Write(true);
_Latch.Write(false);
}
public class ESTPanel
{
private int _IndexStart;
private ESTPanelSet _Master;
internal ESTPanel(ESTPanelSet SetMaster, int PanelIndex)
{
_Master = SetMaster;
_IndexStart = PanelIndex;
}
public void SetLight(int ButtonIndex, Lights Light, bool LightState)
{
int BitAddr = ((ButtonIndex * 3) + (int)Light);
SetRaw(BitAddr, LightState);
}
public void SetRaw(int Index, bool LightState)
{
int ArrayIndex = (_IndexStart * 17) + 5 + (Index >> 3);
int ModifyBit = 1 << (Index & 7);
int LightValue = _Master._Buffer[ArrayIndex];
if (LightState)
LightValue |= ModifyBit;
else
LightValue &= ~ModifyBit;
_Master._Buffer[ArrayIndex] = (byte)LightValue;
}
public bool ReadButton(int ButtonIndex)
{
return (_Master._Buffer[(_IndexStart * 17) + (ButtonIndex >> 3)] & (1 << (ButtonIndex & 7))) == 0;
}
}
}
public class Program
{
public static void Main()
{
Random R = new Random();
ESTPanelSet Set = new ESTPanelSet(SPI_Devices.SPI1, Pins.GPIO_PIN_D0, 1, Pins.GPIO_PIN_D1);
ESTPanelSet.ESTPanel Panel = Set.Panels[0];
int i = 0;
bool t = true;
while (true)
{
Panel.SetRaw(i, t);
i++;
if (i == 90)
{
i = 0;
t = !t;
}
//String S = "";
//foreach (byte B in Set._InBuff)
//{
// S += B.ToString("X") + " ";
//}
//for (int I = 0; I < 5; I++)
//Debug.Print(StateLine(I, Panel));
//Debug.Print(S);
Set.Refresh();
Thread.Sleep(10);
}
}
public static string StateLine(int Line, ESTPanelSet.ESTPanel Panel)
{
string S = "";
for (int B = 1; B < 7; B++)
S += Panel.ReadButton((B * 5) + Line) ? "X" : ".";
return S;
}
}
}