6 #ifndef _MOCK_IO_ABSTRACTION_H_
7 #define _MOCK_IO_ABSTRACTION_H_
45 uint16_t *writeValues;
53 RawIntHandler intHandler;
61 for(
int i=0;i<16;i++) pinModes[i] = 0xff;
64 this->numberOfCycles = numberOfCycles;
65 readValues =
new uint16_t[numberOfCycles];
66 writeValues =
new uint16_t[numberOfCycles];
80 for(
int i=0; i<numberOfCycles; i++) {
97 bitWrite(writeValues[runLoopCalls], pin, value != 0);
101 checkPinInRange(pin);
102 if(pinModes[pin] != INPUT && pinModes[pin] != INPUT_PULLUP) error =
READ_NOT_INPUT;
103 return bitRead(readValues[runLoopCalls], pin);
109 void attachInterrupt(pinid_t pin, RawIntHandler interruptHandler, uint8_t mode)
override {
110 this->intHandler = interruptHandler;
112 this->intMode = mode;
117 uint16_t currentWritten = writeValues[runLoopCalls];
120 runLoopCalls = runLoopCalls % numberOfCycles;
122 writeValues[runLoopCalls] = currentWritten;
128 checkPinInRange(pin);
131 checkPinsAre(OUTPUT, 0, 7);
132 writeValues[runLoopCalls] = (writeValues[runLoopCalls] & 0xff00) | portVal;
135 checkPinsAre(OUTPUT, 8, 15);
136 writeValues[runLoopCalls] = (writeValues[runLoopCalls] & 0x00ff) | (portVal << 8);
141 checkPinInRange(pin);
144 checkPinsAre(INPUT, 0, 7);
145 return readValues[runLoopCalls];
148 checkPinsAre(INPUT, 8, 15);
149 return readValues[runLoopCalls] >> 8;
176 return intPin == pin && intMode == mode && intHandler != NULL;
179 void checkPinInRange(
int pin) {
182 void checkPinsAre(uint8_t mode, uint8_t start, uint8_t end) {
183 for(
int i = start; i < end; ++i) {
187 else if(mode == INPUT) {
188 if(pinModes[i] != INPUT && pinModes[i] != INPUT_PULLUP) error =
READ_NOT_INPUT;
211 this->delegate = delegate;
219 bitWrite(writeVals, pin, value);
228 writeVals &= 0xffffff00L;
229 writeVals |= (uint32_t)portVal;
232 writeVals &= 0xffff00ffL;
233 writeVals |= ((uint32_t)portVal<<8);
236 writeVals &= 0xff00ffffL;
237 writeVals |= ((uint32_t)portVal<<16);
240 writeVals &= 0x00ffffffL;
241 writeVals |= ((uint32_t)portVal<<24);
248 serlogF(SER_DEBUG,
"Port write ");
249 uint32_t val = writeVals;
250 for(
int i=0;i<ports;i++) {
251 printHexZeroPad(val);
254 bool ret = delegate->
runLoop();
255 serlogF(SER_DEBUG,
"read ");
256 for(
int i=0;i<ports;i++) {
257 printHexZeroPad(delegate->
readPort(i * 8));
259 serlogF(SER_DEBUG,
"EOL");
263 void printHexZeroPad(uint8_t val) {
264 serlog3(SER_DEBUG, hexchar(val / 16), hexchar(val % 16),
' ');
267 char hexchar(uint8_t ch) {
268 return ch < 10 ? ch +
'0' : (ch-10) +
'A';
Using basic IoFacilities allows one to abstract away the use of IoExpanders, such that the switching ...
MockIoError
Definition: MockIoAbstraction.h:21
@ READ_NOT_INPUT
Definition: MockIoAbstraction.h:27
@ PIN_TOO_HIGH
Definition: MockIoAbstraction.h:25
@ NO_ERROR
Definition: MockIoAbstraction.h:23
@ WRITE_NOT_OUTPUT
Definition: MockIoAbstraction.h:29
Definition: BasicIoAbstraction.h:38
virtual void pinDirection(pinid_t pin, uint8_t mode)
virtual void writePort(pinid_t pin, uint8_t portVal)
virtual uint8_t readValue(pinid_t pin)
virtual void writeValue(pinid_t pin, uint8_t value)
virtual void attachInterrupt(pinid_t pin, RawIntHandler interruptHandler, uint8_t mode)
virtual bool runLoop()
Definition: BasicIoAbstraction.h:147
virtual uint8_t readPort(pinid_t pin)
Definition: MockIoAbstraction.h:204
uint8_t readPort(pinid_t pin) override
Definition: MockIoAbstraction.h:245
void attachInterrupt(pinid_t pin, RawIntHandler interruptHandler, uint8_t mode) override
Definition: MockIoAbstraction.h:224
void writeValue(pinid_t pin, uint8_t value) override
Definition: MockIoAbstraction.h:218
bool runLoop() override
Definition: MockIoAbstraction.h:247
void pinDirection(pinid_t pin, uint8_t mode) override
Definition: MockIoAbstraction.h:216
uint8_t readValue(pinid_t pin) override
Definition: MockIoAbstraction.h:222
void writePort(pinid_t pin, uint8_t portVal) override
Definition: MockIoAbstraction.h:226
Definition: MockIoAbstraction.h:41
void pinDirection(pinid_t pin, uint8_t mode) override
Definition: MockIoAbstraction.h:88
MockIoError getErrorMode()
Definition: MockIoAbstraction.h:166
void writeValue(pinid_t pin, uint8_t value) override
Definition: MockIoAbstraction.h:93
virtual uint8_t readPort(pinid_t pin)
Definition: MockIoAbstraction.h:140
void attachInterrupt(pinid_t pin, RawIntHandler interruptHandler, uint8_t mode) override
Definition: MockIoAbstraction.h:109
bool isIntRegisteredAs(uint8_t pin, uint8_t mode)
Definition: MockIoAbstraction.h:175
uint16_t getWrittenValue(int runLoopNo)
Definition: MockIoAbstraction.h:163
uint8_t readValue(pinid_t pin) override
Definition: MockIoAbstraction.h:100
RawIntHandler getInterruptFunction()
Definition: MockIoAbstraction.h:172
bool runLoop() override
Definition: MockIoAbstraction.h:115
uint16_t getWrittenValuesForRunLoop(int runLoop)
Definition: MockIoAbstraction.h:157
void resetIo()
Definition: MockIoAbstraction.h:79
void setValueForReading(int runLoopNo, uint16_t val)
Definition: MockIoAbstraction.h:160
void clearError()
Definition: MockIoAbstraction.h:169
void writePort(pinid_t pin, uint8_t portVal) override
Definition: MockIoAbstraction.h:126
int getNumberOfRunLoops()
Definition: MockIoAbstraction.h:154