forked from MobiFlight/MobiFlight-FirmwareSource
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMFOutputShifter.cpp
More file actions
87 lines (70 loc) · 1.79 KB
/
MFOutputShifter.cpp
File metadata and controls
87 lines (70 loc) · 1.79 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
//
// MFOutputShifter.cpp
//
// (C) MobiFlight Project 2022
//
#include "MFOutputShifter.h"
#include "allocateMem.h"
MFOutputShifter::MFOutputShifter()
{
_initialized = false;
}
void MFOutputShifter::setPin(uint8_t pin, uint8_t value, uint8_t refresh)
{
if (!_initialized) return;
uint8_t idx = (pin & 0xF8) >> 3;
uint8_t msk = (0x01 << (pin & 0x07));
if (value != MF_LOW) {
_lastState[idx] |= msk;
} else {
_lastState[idx] &= ~msk;
}
if (refresh) update();
}
void MFOutputShifter::setPins(char *pins, uint8_t value)
{
if (!_initialized) return;
char *pinTokens = strtok(pins, "|");
while (pinTokens != 0) {
uint8_t num = (uint8_t)atoi(pinTokens);
setPin(num, value, 0);
pinTokens = strtok(0, "|");
}
update();
}
bool MFOutputShifter::attach(uint8_t latchPin, uint8_t clockPin, uint8_t dataPin, uint8_t moduleCount)
{
_initialized = true;
_latchPin = latchPin;
_clockPin = clockPin;
_dataPin = dataPin;
_moduleCount = moduleCount;
pinMode(_latchPin, OUTPUT);
pinMode(_clockPin, OUTPUT);
pinMode(_dataPin, OUTPUT);
if (!FitInMemory(sizeof(uint8_t) * _moduleCount))
return false;
_lastState = new (allocateMemory(sizeof(uint8_t) * _moduleCount)) uint8_t;
clear();
return true;
}
void MFOutputShifter::detach()
{
_initialized = false;
}
void MFOutputShifter::clear()
{
for (uint8_t i = 0; i < _moduleCount; i++) {
_lastState[i] = 0xFF * MF_LOW;
}
update();
}
void MFOutputShifter::update()
{
digitalWrite(_latchPin, LOW);
for (uint8_t i = _moduleCount; i > 0; i--) {
shiftOut(_dataPin, _clockPin, MSBFIRST, _lastState[i - 1]); // LSBFIRST, MSBFIRST,
}
digitalWrite(_latchPin, HIGH);
}
// MFOutputShifter.cpp