IoAbstraction
DfRobotInputAbstraction.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 https://www.thecoderscorner.com (Dave Cherry).
3  * This product is licensed under an Apache license, see the LICENSE file in the top-level directory.
4  */
5 
6 #ifndef _DF_ROBOT_INPUT_ABSTRACTION_H
7 #define _DF_ROBOT_INPUT_ABSTRACTION_H
8 
9 #include "BasicIoAbstraction.h"
11 
20 #define DF_KEY_RIGHT 0
21 #define DF_KEY_LEFT 1
22 #define DF_KEY_UP 2
23 #define DF_KEY_DOWN 3
24 #define DF_KEY_SELECT 4
25 
27  float right;
28  float up;
29  float down;
30  float left;
31  float select;
32 };
33 
34 #ifndef ALLOWABLE_RANGE
35 #define ALLOWABLE_RANGE 0.01F
36 #endif // ALLOWABLE_RANGE
37 
38 #if defined(IOA_USE_MBED) || defined(BUILD_FOR_PICO_CMAKE)
39 #define pgmAsFloat(x) ((float)(*x))
40 #define A0 26
41 #else
42 #define pgmAsFloat(x) ((float)pgm_read_float_near(x))
43 #endif
56 private:
57  pinid_t analogPin;
58  uint8_t readCache;
59  float lastReading;
60  const DfRobotAnalogRanges* analogRanges;
61  AnalogDevice* device;
62 
63 public:
71  DfRobotInputAbstraction(const DfRobotAnalogRanges& ranges, pinid_t pin = A0) {
72  analogRanges = &ranges;
73  analogPin = pin;
74  device = internalAnalogIo();
75  initAbstraction();
76  }
77 
78  void initAbstraction() {
79  device->initPin(analogPin, DIR_IN);
80  lastReading = device->getCurrentValue(analogPin);
81  readCache = mapAnalogToPin(lastReading);
82  }
83 
93  DfRobotInputAbstraction(const DfRobotAnalogRanges* ranges, pinid_t pin, AnalogDevice* device) {
94  analogRanges = ranges;
95  analogPin = pin;
96  this->device = device;
97  initAbstraction();
98  }
99 
100  uint8_t readValue(pinid_t pin) override {
101  return bitRead(readCache, pin);
102  }
103 
104  uint8_t readPort(pinid_t port) override {
105  return readCache;
106  }
107 
108  bool runLoop() override {
109  auto newReading = device->getCurrentFloat(analogPin);
110  if(abs(newReading - lastReading) > ALLOWABLE_RANGE) {
111  readCache = mapAnalogToPin(newReading);
112  }
113  lastReading = newReading;
114  return true;
115  }
116 
117  uint8_t mapAnalogToPin(float reading) {
118  uint8_t ret = 0xff;
119  if(reading < pgmAsFloat(&analogRanges->right)) ret = DF_KEY_RIGHT;
120  else if(reading < pgmAsFloat(&analogRanges->up)) ret = DF_KEY_UP;
121  else if(reading < pgmAsFloat(&analogRanges->down)) ret = DF_KEY_DOWN;
122  else if(reading < pgmAsFloat(&analogRanges->left)) ret = DF_KEY_LEFT;
123  else if(reading < pgmAsFloat(&analogRanges->select)) ret = DF_KEY_SELECT;
124 
125  if(ret == 0xff)
126  return 0;
127  else
128  return 1 << ret;
129  }
130 
131  // we ignore all non-input methods, as this is input only
132 
133  void pinDirection(pinid_t pin, uint8_t mode) override {
135  }
136 
137  void writeValue(pinid_t pin, uint8_t value) override {
139  }
140 
141  void writePort(pinid_t pin, uint8_t portVal) override {
143  }
144 };
145 
146 #ifndef IOA_USE_MBED
147 
151 const PROGMEM DfRobotAnalogRanges dfRobotAvrRanges { 0.0488F, 0.2441F, 0.4394F, 0.6347F, 0.8300F};
152 
156 const PROGMEM DfRobotAnalogRanges dfRobotV1AvrRanges { 0.0488F, 0.1904F, 0.3710F, 0.5419F, 0.7714F};
157 
158 
166 inline IoAbstractionRef inputFromDfRobotShield(uint8_t pin = A0, AnalogDevice* device = nullptr) {
167  device = internalAnalogIo();
168  return new DfRobotInputAbstraction(&dfRobotAvrRanges, pin, device);
169 }
170 
178 inline IoAbstractionRef inputFromDfRobotShieldV1(uint8_t pin = A0, AnalogDevice* device = nullptr) {
179  device = internalAnalogIo();
180  return new DfRobotInputAbstraction(&dfRobotV1AvrRanges, pin, device);
181 }
182 
183 #endif
184 
185 #endif
Contains a series of helper classes for dealing with analog devices, these are compatible across a wi...
AnalogDevice * internalAnalogIo()
Provides the core IoAbstraction interface and Arduino implementation of that interface.
const PROGMEM DfRobotAnalogRanges dfRobotV1AvrRanges
Definition: DfRobotInputAbstraction.h:156
IoAbstractionRef inputFromDfRobotShield(uint8_t pin=A0, AnalogDevice *device=nullptr)
Definition: DfRobotInputAbstraction.h:166
IoAbstractionRef inputFromDfRobotShieldV1(uint8_t pin=A0, AnalogDevice *device=nullptr)
Definition: DfRobotInputAbstraction.h:178
const PROGMEM DfRobotAnalogRanges dfRobotAvrRanges
Definition: DfRobotInputAbstraction.h:151
Definition: DfRobotInputAbstraction.h:26
Definition: AnalogDeviceAbstraction.h:31
virtual void initPin(pinid_t pin, AnalogDirection direction)=0
virtual unsigned int getCurrentValue(pinid_t pin)=0
Definition: BasicIoAbstraction.h:38
Definition: DfRobotInputAbstraction.h:55
DfRobotInputAbstraction(const DfRobotAnalogRanges *ranges, pinid_t pin, AnalogDevice *device)
Definition: DfRobotInputAbstraction.h:93
void writeValue(pinid_t pin, uint8_t value) override
Definition: DfRobotInputAbstraction.h:137
void pinDirection(pinid_t pin, uint8_t mode) override
Definition: DfRobotInputAbstraction.h:133
DfRobotInputAbstraction(const DfRobotAnalogRanges &ranges, pinid_t pin=A0)
Definition: DfRobotInputAbstraction.h:71
bool runLoop() override
Definition: DfRobotInputAbstraction.h:108
void writePort(pinid_t pin, uint8_t portVal) override
Definition: DfRobotInputAbstraction.h:141
uint8_t readPort(pinid_t port) override
Definition: DfRobotInputAbstraction.h:104
uint8_t readValue(pinid_t pin) override
Definition: DfRobotInputAbstraction.h:100