-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmxUnifiedIO.cpp
198 lines (169 loc) · 4.99 KB
/
mxUnifiedIO.cpp
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
#include "mxUnifiedIO.h"
mxUnifiedIO::mxUnifiedIO(void)
{
_nConstr=1;
#if defined(MXUNIFIED_ATTINY) && (MXUNIFIED_ATTINY==13 || MXUNIFIED_ATTINY==85)
_nNumPins=5;
#else
_nNumPins=8;
#endif
_dataOut=0;
// Set default MCU pins
// Can be redfined using mxUnifiedIO::specifyPins(uint8_t *arrPins)
// The standard pin definition allows the following devices connected as example
// ATmega328, PCD8544+Max7219 panel:
// P3; D13 - led
// P2: D3 - RST
// P4: D4 - CE
// P5: D5 - DC
// P6: D6 - DIN
// P7: D7 - CLK
// P0: D10 - CS (SS)
// P1: D11 - DIN (MOSI)
// D12 - not connected (MISO)
// P3: D13 - CLK (SCK)
#ifdef ESP8266
_aPins[0]=0;
_aPins[1]=2; // ESP12 LED on pin 2 is active low
_aPins[2]=12;
_aPins[3]=13;
_aPins[4]=14;
_aPins[5]=15;
_aPins[6]=4;
_aPins[7]=5;
#elif defined(MXUNIFIED_ATTINY)
_aPins[0]=0;
_aPins[1]=1;
_aPins[2]=2;
_aPins[3]=3;
_aPins[4]=4;
_aPins[5]=5;
_aPins[6]=6;
_aPins[7]=7;
#else
_aPins[0]=10;
_aPins[1]=11;
_aPins[2]=12;
_aPins[3]=13;
_aPins[4]=4;
_aPins[5]=5;
_aPins[6]=6;
_aPins[7]=7;
#endif
}
void mxUnifiedIO::specifyPins(uint8_t *arrPins)
{ // use these specific MCU pins to map to virtual expanded pins
for(uint8_t n=0; n<_nNumPins; n++)
_aPins[n]=arrPins[n];
}
void mxUnifiedIO::begin(uint8_t nPinSelection) // uint8_t nPinSelection=MXUNIFIED_DEFAULT_PINS
{ // Overwrite in subclasses if desired!
// Call without parameters assumes using the default pins.
// For the base mxUnifiedIO class the default pins are tied directly to the MCU pins
if(nPinSelection==MXUNIFIED_PINS_2TO9 || nPinSelection==MXUNIFIED_PINS_2TO9_13)
for(uint8_t n=0; n<=7; n++)
_aPins[n]=2+n;
if(nPinSelection==MXUNIFIED_PINS_2TO9_13)
_aPins[1]=13;
for(uint8_t n=0; n<_nNumPins; n++)
::pinMode(_aPins[n], OUTPUT);
#if(_MXUNIFIEDIO_DEBUG)
Serial.print(F("mxUnifiedIO::begin("));
//Serial.print(uI2C_address);
Serial.print(F(") constr:"));
Serial.print(_nConstr);
Serial.print(F(", pins:"));
Serial.print(_nNumPins);
Serial.println(F("."));
#endif
}
uint8_t mxUnifiedIO::getNumPins(void)
{ // get the number of pins that are expanded
return(_nNumPins);
}
void mxUnifiedIO::setBit(uint8_t nPin, uint8_t nValue)
{ // set the status of a pin (without actually writing it or ending the transmission yet)
//uint8_t uDataOld=_dataOut; // to return dirty flag when changed
if(nValue) _dataOut |= ((uint32_t)1 << nPin); // cast is needed to avoid shifting to negative 32bit values
else _dataOut &= ~((uint32_t)1 << nPin);
//return(_dataOut!=uDataOld);
}
void mxUnifiedIO::set8Bits(uint8_t nValue)
{ // set bits 0-7 to the specified value (leave upper bits unaltered
//_dataOut=nValue;
_dataOut&=0xFFFFFF00;
_dataOut|=nValue;
}
uint8_t mxUnifiedIO::getBit(uint8_t nPin)
{
return((_dataOut & (uint32_t)1<<nPin)?HIGH:LOW);
}
uint8_t mxUnifiedIO::get8Bits(void)
{
return((uint8_t)_dataOut);
}
void mxUnifiedIO::send8Bits(bool fClosedTransmission) // default: fClosedTransmission=true
{ // Overwrite in subclasses if desired!
uint8_t uByte=(uint8_t)_dataOut;
#if(_MXUNIFIEDIO_DEBUG)
Serial.print(F("mxUnifiedIO::send8Bits("));
Serial.print(_dataOut);
Serial.print(F(", 0x"));
Serial.print(uByte, HEX);
Serial.println(F(")"));
#endif
for(uint8_t n=0; n<=7; n++)
{
::digitalWrite(_aPins[n], uByte&0x01);
uByte>>=1;
}
}
void mxUnifiedIO::sendBits(void)
{ // Overwrite in subclasses if desired!
send8Bits();
}
void mxUnifiedIO::shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
{ // Overwrite in subclasses if desired!
}
void mxUnifiedIO::startTransmission(void)
{ // Overwrite in subclasses if desired!
}
void mxUnifiedIO::endTransmission(void)
{ // Overwrite in subclasses if desired!
}
void mxUnifiedIO::digitalWrite(uint8_t nPin, uint8_t nVal)
{ // Overwrite in subclasses if desired!
#if(_MXUNIFIEDIO_DEBUG)
Serial.print(F("mxUnifiedIO::digitalWrite("));
Serial.print(nPin);
Serial.print(F(","));
Serial.print(nVal);
Serial.println(F(")"));
#endif
if (nPin >= _nNumPins) return;
setBit(nPin, nVal);
::digitalWrite(_aPins[nPin], nVal); // call the regular digitalWrite using the global scope resolution operator
// TODO: use MCU-specific pin manipulation to provide faster switching
}
void mxUnifiedIO::digitalWrite8(uint8_t nVal)
{
set8Bits(nVal);
send8Bits();
}
int mxUnifiedIO::digitalRead(uint8_t nPin)
{
if (nPin >= _nNumPins) return LOW;
#if(_MXUNIFIEDIO_DEBUG)
Serial.print(F("mxUnifiedIO::digitalRead("));
Serial.print(nPin);
Serial.print(F(","));
Serial.print(getBit(nPin));
Serial.println(F(")"));
#endif
return(getBit(nPin));
}
void mxUnifiedIO::pinMode(uint8_t nPin, uint8_t nMode)
{ // Overwrite in subclasses if desired!
if (nPin >= _nNumPins) return;
::pinMode(_aPins[nPin], nMode); // call the regular pinMode using the global scope resolution operator
}