-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpin.cpp
More file actions
471 lines (379 loc) · 11.9 KB
/
pin.cpp
File metadata and controls
471 lines (379 loc) · 11.9 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//
// OOSMOS pin Class
//
// Copyright (C) 2014-2020 OOSMOS, LLC
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2 of the License ("GPLv2").
//
// This software may be used without the GPLv2 restrictions by entering
// into a commercial license agreement with OOSMOS, LLC.
// See <https://www.oosmos.com/licensing/>.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef pinMAX
#define pinMAX 6
#endif
//===================================
#include "pin.h"
#include "oosmos.h"
#include <stdbool.h>
#include <stdint.h>
typedef enum {
Unknown_State = 1,
On_State,
Off_State,
ConfirmingOn_State,
ConfirmingOff_State
} eStates;
struct pinTag
{
#if defined(ARDUINO) || defined(oosmos_RASPBERRY_PI)
uint8_t m_PinNumber;
#elif defined(__PIC32MX)
IoPortId m_Port;
uint16_t m_Bit;
#elif defined(__MBED__)
uint8_t m_Pin[sizeof(DigitalOut)];
PinName m_PinName;
#elif defined(__IAR_SYSTEMS_ICC__)
GPIO_TypeDef* m_Port;
uint16_t m_Pin;
#elif defined(_MSC_VER)
int m_Key;
#else
#error pin.c: Unsupported platform.
#endif
oosmos_sActiveObject m_ActiveObject;
oosmos_sTimeout m_Timeout;
uint8_t m_DebounceTimeMS;
unsigned m_State:4; // eStates
unsigned m_Logic:4; // pin_eLogic
unsigned m_Direction:4; // pin_eDirection
};
static bool IsPhysicallyOn(const pin * pPin);
static bool IsPhysicallyOff(const pin * pPin)
{
return !IsPhysicallyOn(pPin);
}
static void RunStateMachine(void * pObject)
{
oosmos_POINTER_GUARD(pObject);
pin * pPin = (pin *) pObject;
switch ((eStates) pPin->m_State) {
case On_State: {
if (IsPhysicallyOff(pPin)) {
pPin->m_State = (unsigned) ConfirmingOff_State;
oosmos_TimeoutInMS(&pPin->m_Timeout, pPin->m_DebounceTimeMS);
}
break;
}
case Off_State: {
if (IsPhysicallyOn(pPin)) {
pPin->m_State = (unsigned) ConfirmingOn_State;
oosmos_TimeoutInMS(&pPin->m_Timeout, pPin->m_DebounceTimeMS);
}
break;
}
case ConfirmingOn_State: {
if (!oosmos_TimeoutHasExpired(&pPin->m_Timeout)) {
break;
}
if (!IsPhysicallyOn(pPin)) {
pPin->m_State = (unsigned) Unknown_State;
break;
}
pPin->m_State = (unsigned) On_State;
break;
}
case ConfirmingOff_State: {
if (!oosmos_TimeoutHasExpired(&pPin->m_Timeout)) {
break;
}
if (!IsPhysicallyOff(pPin)) {
pPin->m_State = (unsigned) Unknown_State;
break;
}
pPin->m_State = (unsigned) Off_State;
break;
}
case Unknown_State: {
pPin->m_State = (unsigned) (IsPhysicallyOn(pPin) ? ConfirmingOn_State : ConfirmingOff_State);
oosmos_TimeoutInMS(&pPin->m_Timeout, pPin->m_DebounceTimeMS);
break;
}
default: {
oosmos_FOREVER();
}
}
}
extern bool pinIsOn(const pin * pPin)
{
oosmos_POINTER_GUARD(pPin);
if (pPin->m_DebounceTimeMS == 0) {
return IsPhysicallyOn(pPin);
}
return pPin->m_State == (unsigned) On_State;
}
extern bool pinIsOff(const pin * pPin)
{
return !pinIsOn(pPin);
}
#if defined(ARDUINO) || defined(oosmos_RASPBERRY_PI)
static bool IsPhysicallyOn(const pin * pPin)
{
const unsigned PinValue = digitalRead(pPin->m_PinNumber);
return PinValue == (pPin->m_Logic == pinActiveHigh ? HIGH : LOW);
}
extern void pinOn(const pin * pPin)
{
const unsigned PinNumber = pPin->m_PinNumber;
if (pPin->m_Direction == pinInOut) {
pinMode(PinNumber, pPin->m_Logic == pinActiveHigh ? INPUT : OUTPUT);
}
digitalWrite(PinNumber, pPin->m_Logic == pinActiveHigh ? HIGH : LOW);
}
extern void pinOff(const pin * pPin)
{
const unsigned PinNumber = pPin->m_PinNumber;
if (pPin->m_Direction == pinInOut) {
pinMode(PinNumber, pPin->m_Logic == pinActiveHigh ? OUTPUT : INPUT);
}
digitalWrite(PinNumber, pPin->m_Logic == pinActiveHigh ? LOW : HIGH);
}
extern unsigned pinGetPinNumber(pin * pPin)
{
return pPin->m_PinNumber;
}
extern pin * pinNew(const unsigned PinNumber, const pin_eDirection Direction, const pin_eLogic Logic)
{
#if defined(oosmos_RASPBERRY_PI)
static bool WiringPi_Initialized = false;
if (!WiringPi_Initialized) {
wiringPiSetup();
WiringPi_Initialized = true;
}
#endif
oosmos_Allocate(pPin, pin, pinMAX, NULL);
pPin->m_PinNumber = PinNumber;
pPin->m_Logic = (unsigned) Logic;
pPin->m_State = (unsigned) Unknown_State;
pPin->m_Direction = (unsigned) Direction;
pPin->m_DebounceTimeMS = 0;
switch (Direction) {
case pinIn:
case pinInOut: {
pinMode(PinNumber, INPUT);
break;
}
case pinIn_Pullup: {
pinMode(PinNumber, INPUT_PULLUP);
break;
}
case pinOut: {
pinMode(PinNumber, OUTPUT);
break;
}
}
return pPin;
}
extern pin * pinNew_Debounce(const unsigned PinNumber, const pin_eDirection Direction, const pin_eLogic Logic, const uint8_t DebounceTimeMS)
{
pin * pPin = pinNew(PinNumber, Direction, Logic);
pPin->m_DebounceTimeMS = DebounceTimeMS;
if (DebounceTimeMS > 0) {
oosmos_ActiveObjectInit(pPin, m_ActiveObject, RunStateMachine);
}
return pPin;
}
#elif defined(__PIC32MX)
static bool IsPhysicallyOn(const pin * pPin)
{
const uint32_t PinValue = PORTReadBits(pPin->m_Port, pPin->m_Bit);
return (pPin->m_Logic == pinActiveHigh) ? PinValue != 0 : PinValue == 0;
}
extern void pinOn(const pin * pPin)
{
(pPin->m_Logic == pinActiveHigh ? PORTSetBits : PORTClearBits)(pPin->m_Port, pPin->m_Bit);
}
extern void pinOff(const pin * pPin)
{
(pPin->m_Logic == pinActiveHigh ? PORTClearBits : PORTSetBits)(pPin->m_Port, pPin->m_Bit);
}
extern pin * pinNew(const IoPortId Port, const unsigned Bit, const pin_eDirection Direction, const pin_eLogic Logic)
{
oosmos_Allocate(pPin, pin, pinMAX, NULL);
pPin->m_Port = Port;
pPin->m_Bit = Bit;
pPin->m_Logic = (unsigned) Logic;
pPin->m_State = (unsigned) Unknown_State;
pPin->m_Direction = (unsigned) Direction;
pPin->m_DebounceTimeMS = 0;
switch (pPin->m_Direction) {
case pinOut: {
pinOff(pPin);
PORTSetPinsDigitalOut(Port, Bit);
break;
}
case pinIn: {
pinOff(pPin);
PORTSetPinsDigitalIn(Port, Bit);
break;
}
}
return pPin;
}
extern pin * pinNew_Debounce(const IoPortId Port, const unsigned Bit, const pin_eDirection Direction, const pin_eLogic Logic, const uint8_t DebounceTimeMS)
{
pin * pPin = pinNew(Port, Bit, Direction, Logic);
pPin->m_DebounceTimeMS = DebounceTimeMS;
if (DebounceTimeMS > 0) {
oosmos_ActiveObjectInit(pPin, m_ActiveObject, RunStateMachine);
}
return pPin;
}
#elif defined(__MBED__)
#include "mbed.h"
#include <new>
extern pin * pinNew(const PinName Pin, const pin_eDirection Direction, const pin_eLogic Logic)
{
oosmos_Allocate(pPin, pin, pinMAX, NULL);
::new(&pPin->m_Pin) DigitalOut(Pin);
pPin->m_PinName = Pin;
pPin->m_Logic = (unsigned) Logic;
pPin->m_State = (unsigned) Unknown_State;
pPin->m_Direction = (unsigned) Direction;
pPin->m_DebounceTimeMS = 0;
return pPin;
}
extern pin * pinNew_Debounce(const PinName PinNumber, const pin_eDirection Direction, const pin_eLogic Logic, const uint8_t DebounceTimeMS)
{
pin * pPin = pinNew(PinNumber, Direction, Logic);
pPin->m_DebounceTimeMS = DebounceTimeMS;
if (DebounceTimeMS > 0) {
oosmos_ActiveObjectInit(pPin, m_ActiveObject, RunStateMachine);
}
return pPin;
}
static bool IsPhysicallyOn(const pin * pPin)
{
DigitalOut * pDigitalOut = (DigitalOut *) pPin->m_Pin;
const unsigned PinValue = pDigitalOut->read();
return PinValue == (pPin->m_Logic == pinActiveHigh ? 1 : 0);
}
extern void pinOn(const pin * pPin)
{
DigitalOut * pDigitalOut = (DigitalOut *) pPin->m_Pin;
pDigitalOut->write(pPin->m_Logic == pinActiveHigh ? 1 : 0);
}
extern void pinOff(const pin * pPin)
{
DigitalOut * pDigitalOut = (DigitalOut *) pPin->m_Pin;
pDigitalOut->write(pPin->m_Logic == pinActiveHigh ? 0 : 1);
}
extern PinName pinGetPinName(pin * pPin)
{
return pPin->m_PinName;
}
#elif defined(__IAR_SYSTEMS_ICC__)
static bool IsPhysicallyOn(const pin * pPin)
{
oosmos_POINTER_GUARD(pPin);
const GPIO_PinState PinValue = HAL_GPIO_ReadPin(pPin->m_Port, pPin->m_Pin);
return (pPin->m_Logic == pinActiveHigh) ? (PinValue == GPIO_PIN_SET) : (PinValue == GPIO_PIN_RESET);
}
extern void pinOn(const pin * pPin)
{
oosmos_POINTER_GUARD(pPin);
HAL_GPIO_WritePin(pPin->m_Port, pPin->m_Pin, pPin->m_Logic == pinActiveHigh ? GPIO_PIN_SET : GPIO_PIN_RESET);
}
extern void pinOff(const pin * pPin)
{
oosmos_POINTER_GUARD(pPin);
HAL_GPIO_WritePin(pPin->m_Port, pPin->m_Pin, pPin->m_Logic == pinActiveHigh ? GPIO_PIN_RESET : GPIO_PIN_SET);
}
extern pin * pinNew(GPIO_TypeDef* Port, uint16_t Pin, const pin_eDirection Direction, const pin_eLogic Logic)
{
oosmos_Allocate(pPin, pin, pinMAX, NULL);
pPin->m_Port = Port;
pPin->m_Pin = Pin;
pPin->m_Logic = (unsigned) Logic;
pPin->m_State = (unsigned) Unknown_State;
pPin->m_Direction = (unsigned) Direction;
pPin->m_DebounceTimeMS = 0;
pinOff(pPin);
return pPin;
}
extern pin * pinNew_Debounce(GPIO_TypeDef* Port, const uint16_t Bit, const pin_eDirection Direction, const pin_eLogic Logic, const uint8_t DebounceTimeMS)
{
pin * pPin = pinNew(Port, Bit, Direction, Logic);
pPin->m_DebounceTimeMS = DebounceTimeMS;
if (DebounceTimeMS > 0) {
oosmos_ActiveObjectInit(pPin, m_ActiveObject, RunStateMachine);
}
return pPin;
}
#elif defined(_MSC_VER)
#include <stdio.h>
#include <windows.h>
static bool pinFirst = true;
static bool KeyIsDown[0xFF];
static HANDLE hStdin;
static bool IsPhysicallyOn(const pin * pPin)
{
oosmos_POINTER_GUARD(pPin);
DWORD NumRead = 0;
INPUT_RECORD InputRecord;
while (PeekConsoleInput(hStdin, &InputRecord, 1, &NumRead) && NumRead > 0) {
if (InputRecord.EventType == KEY_EVENT) {
const KEY_EVENT_RECORD KER = InputRecord.Event.KeyEvent;
const int Char = KER.uChar.AsciiChar;
KeyIsDown[Char] = (KER.bKeyDown == TRUE);
}
(void) ReadConsoleInput(hStdin, &InputRecord, 1, &NumRead);
}
const bool IsDown = KeyIsDown[pPin->m_Key];
return pPin->m_Logic == ((unsigned) pinActiveHigh) ? IsDown : !IsDown;
}
extern pin * pinNew(char Key, const pin_eLogic Logic)
{
oosmos_Allocate(pPin, pin, pinMAX, NULL);
pPin->m_Key = Key;
pPin->m_Logic = (unsigned) Logic;
pPin->m_State = (unsigned) Unknown_State;
pPin->m_DebounceTimeMS = 0;
if (pinFirst) {
memset(KeyIsDown, false, sizeof(KeyIsDown));
hStdin = GetStdHandle(STD_INPUT_HANDLE);
pinFirst = false;
}
return pPin;
}
extern void pinOn(const pin * pPin)
{
#if defined(pin_DEBUG)
printf("%p Pin ON\n", pPin);
#else
oosmos_UNUSED(pPin);
#endif
}
extern void pinOff(const pin * pPin)
{
#if defined(pin_DEBUG)
printf("%p Pin OFF\n", pPin);
#else
oosmos_UNUSED(pPin);
#endif
}
void (*pin_pDummy)(void *) = RunStateMachine; // To satisfy compiler
#else
#error pin.c: Unsupported platform.
#endif