forked from selenur/OLD---CNC-controller-version-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SuperByte.cs
330 lines (305 loc) · 9.37 KB
/
SuperByte.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
namespace CNC_App
{
/// <summary>
/// Êëàññ äëÿ ðàáîòû ñ áàéòîì, ñî ñïåö. âîçìîæíîñòÿìè
/// </summary>
public class SuperByte
{
/// <summary>
/// Ñîäåðæèò çíà÷åíèå áàéòà
/// </summary>
private byte _valueByte;
/// <summary>
/// Ñîäåðæèò çíà÷åíèÿ áèòîâ
/// </summary>
private readonly bool[] _valueBit = new bool[8];
/// <summary>
/// îáíîâëåíèå ñîòîÿíèÿ áèòîâ, ïîñëå èçìåíåíèÿ áàéòà
/// </summary>
private void RefreshBitsFromByte()
{
_valueBit[0] = (_valueByte & (1 << 0)) != 0;
_valueBit[1] = (_valueByte & (1 << 1)) != 0;
_valueBit[2] = (_valueByte & (1 << 2)) != 0;
_valueBit[3] = (_valueByte & (1 << 3)) != 0;
_valueBit[4] = (_valueByte & (1 << 4)) != 0;
_valueBit[5] = (_valueByte & (1 << 5)) != 0;
_valueBit[6] = (_valueByte & (1 << 6)) != 0;
_valueBit[7] = (_valueByte & (1 << 7)) != 0;
}
/// <summary>
/// Îáíîâëåíèå çíà÷åíèÿ áàéòà, ïîñëå èçìåíåíèÿ áèòîâ
/// </summary>
private void RefreshByteFromBits()
{
byte res = 0x00;
if (_valueBit[7]) res += 0x80;
if (_valueBit[6]) res += 0x40;
if (_valueBit[5]) res += 0x20;
if (_valueBit[4]) res += 0x10;
if (_valueBit[3]) res += 0x08;
if (_valueBit[2]) res += 0x04;
if (_valueBit[1]) res += 0x02;
if (_valueBit[0]) res += 0x01;
_valueByte = res;
}
/// <summary>
/// Ñâîéñòâî, äëÿ ðàáîòû ñ áàéòîì
/// </summary>
public byte ValueByte
{
get
{
return _valueByte;
}
// ReSharper disable once UnusedMember.Global
set
{
_valueByte = value;
RefreshBitsFromByte();
}
}
/// <summary>
/// Ïîëó÷èòü çíà÷åíèå 1-ãî áèòà
/// </summary>
public bool GetBit(int index)
{
if (index > 7) return false;
return _valueBit[index];
}
/// <summary>
/// Óñòàíîâêà çíà÷åíèÿ ó 1-ãî áèòà
/// </summary>
public void SetBit(int index, bool value)
{
if (index > 7) return;
_valueBit[index] = value;
RefreshByteFromBits();
}
public string ValueString
{
get
{
return _valueByte.ToString("X2");
}
//set
//{
// ValueByte = tByte(value.ToString());
// RefreshBitsFromByte();
//}
}
/// <summary>
/// Áèò 7
/// </summary>
// ReSharper disable once UnusedMember.Global
public bool Bit7
{
get
{
return _valueBit[7];
}
set
{
_valueBit[7] = value;
RefreshByteFromBits();
}
}
/// <summary>
/// Áèò 6
/// </summary>
// ReSharper disable once UnusedMember.Global
public bool Bit6
{
get
{
return _valueBit[6];
}
set
{
_valueBit[6] = value;
RefreshByteFromBits();
}
}
/// <summary>
/// Áèò 5
/// </summary>
// ReSharper disable once UnusedMember.Global
public bool Bit5
{
get
{
return _valueBit[5];
}
set
{
_valueBit[5] = value;
RefreshByteFromBits();
}
}
/// <summary>
/// Áèò 4
/// </summary>
// ReSharper disable once UnusedMember.Global
public bool Bit4
{
get
{
return _valueBit[4];
}
set
{
_valueBit[4] = value;
RefreshByteFromBits();
}
}
/// <summary>
/// Áèò 3
/// </summary>
// ReSharper disable once UnusedMember.Global
public bool Bit3
{
get
{
return _valueBit[3];
}
set
{
_valueBit[3] = value;
RefreshByteFromBits();
}
}
/// <summary>
/// Áèò 2
/// </summary>
// ReSharper disable once UnusedMember.Global
public bool Bit2
{
get
{
return _valueBit[2];
}
set
{
_valueBit[2] = value;
RefreshByteFromBits();
}
}
/// <summary>
/// Áèò 1
/// </summary>
// ReSharper disable once UnusedMember.Global
public bool Bit1
{
get
{
return _valueBit[1];
}
set
{
_valueBit[1] = value;
RefreshByteFromBits();
}
}
/// <summary>
/// Áèò 0
/// </summary>
// ReSharper disable once UnusedMember.Global
public bool Bit0
{
get
{
return _valueBit[0];
}
set
{
_valueBit[0] = value;
RefreshByteFromBits();
}
}
/// <summary>
/// Êîíñòðóêòîð èç áàéòà
/// </summary>
/// <param name="inValue"></param>
public SuperByte(byte inValue)
{
_valueByte = inValue;
RefreshBitsFromByte();
}
/// <summary>
/// Êîíñòðóêòîð èç áèòîâ
/// </summary>
/// <param name="bit7"></param>
/// <param name="bit6"></param>
/// <param name="bit5"></param>
/// <param name="bit4"></param>
/// <param name="bit3"></param>
/// <param name="bit2"></param>
/// <param name="bit1"></param>
/// <param name="bit0"></param>
public SuperByte(bool bit7, bool bit6, bool bit5, bool bit4, bool bit3, bool bit2, bool bit1, bool bit0)
{
_valueBit[0] = bit0;
_valueBit[1] = bit1;
_valueBit[2] = bit2;
_valueBit[3] = bit3;
_valueBit[4] = bit4;
_valueBit[5] = bit5;
_valueBit[6] = bit6;
_valueBit[7] = bit7;
RefreshByteFromBits();
}
/// <summary>
/// Êîíñòðóêòîð èç ñòðîêè
/// </summary>
/// <param name="inValue"></param>
public SuperByte(string inValue)
{
byte b1 = 0x00;
byte b2 = 0x00;
if (inValue.Substring(0, 1) == "1") b1 = 0x01;
if (inValue.Substring(0, 1) == "2") b1 = 0x02;
if (inValue.Substring(0, 1) == "3") b1 = 0x03;
if (inValue.Substring(0, 1) == "4") b1 = 0x04;
if (inValue.Substring(0, 1) == "5") b1 = 0x05;
if (inValue.Substring(0, 1) == "6") b1 = 0x06;
if (inValue.Substring(0, 1) == "7") b1 = 0x07;
if (inValue.Substring(0, 1) == "8") b1 = 0x08;
if (inValue.Substring(0, 1) == "9") b1 = 0x09;
if (inValue.Substring(0, 1) == "A") b1 = 0x0A;
if (inValue.Substring(0, 1) == "a") b1 = 0x0A;
if (inValue.Substring(0, 1) == "B") b1 = 0x0B;
if (inValue.Substring(0, 1) == "b") b1 = 0x0B;
if (inValue.Substring(0, 1) == "C") b1 = 0x0C;
if (inValue.Substring(0, 1) == "c") b1 = 0x0C;
if (inValue.Substring(0, 1) == "D") b1 = 0x0D;
if (inValue.Substring(0, 1) == "d") b1 = 0x0D;
if (inValue.Substring(0, 1) == "E") b1 = 0x0E;
if (inValue.Substring(0, 1) == "e") b1 = 0x0E;
if (inValue.Substring(0, 1) == "F") b1 = 0x0F;
if (inValue.Substring(0, 1) == "f") b1 = 0x0F;
if (inValue.Substring(1, 1) == "1") b2 = 0x01;
if (inValue.Substring(1, 1) == "2") b2 = 0x02;
if (inValue.Substring(1, 1) == "3") b2 = 0x03;
if (inValue.Substring(1, 1) == "4") b2 = 0x04;
if (inValue.Substring(1, 1) == "5") b2 = 0x05;
if (inValue.Substring(1, 1) == "6") b2 = 0x06;
if (inValue.Substring(1, 1) == "7") b2 = 0x07;
if (inValue.Substring(1, 1) == "8") b2 = 0x08;
if (inValue.Substring(1, 1) == "9") b2 = 0x09;
if (inValue.Substring(1, 1) == "A") b2 = 0x0A;
if (inValue.Substring(1, 1) == "a") b2 = 0x0A;
if (inValue.Substring(1, 1) == "B") b2 = 0x0B;
if (inValue.Substring(1, 1) == "b") b2 = 0x0B;
if (inValue.Substring(1, 1) == "C") b2 = 0x0C;
if (inValue.Substring(1, 1) == "c") b2 = 0x0C;
if (inValue.Substring(1, 1) == "D") b2 = 0x0D;
if (inValue.Substring(1, 1) == "d") b2 = 0x0D;
if (inValue.Substring(1, 1) == "E") b2 = 0x0E;
if (inValue.Substring(1, 1) == "e") b2 = 0x0E;
if (inValue.Substring(1, 1) == "F") b2 = 0x0F;
if (inValue.Substring(1, 1) == "f") b2 = 0x0F;
_valueByte = (byte)((b1 * 0x10) + b2);
RefreshBitsFromByte();
}
}
}