|
| 1 | +#ifndef __DCSBIOS_ROTARYPOTS_H |
| 2 | +#define __DCSBIOS_ROTARYPOTS_H |
| 3 | + |
| 4 | +#include <math.h> |
| 5 | +#include "Arduino.h" |
| 6 | +#include "PollingInput.h" |
| 7 | + |
| 8 | +namespace DcsBios { |
| 9 | + template <unsigned long pollIntervalMs = POLL_EVERY_TIME, bool invert = false> |
| 10 | + class RotarySyncingPotentiometerEWMA : PollingInput, Int16Buffer, public ResettableInput { |
| 11 | + private: |
| 12 | + void resetState() |
| 13 | + { |
| 14 | + lastState_ = (lastState_==0)?-1:0; |
| 15 | + } |
| 16 | + void pollInput() { |
| 17 | + lastState_ = readState(); |
| 18 | + } |
| 19 | + |
| 20 | + inline unsigned int readState() |
| 21 | + { |
| 22 | + return map(analogRead(pin_), invert?1023:0, invert?0:1023, 0, 65535); |
| 23 | + } |
| 24 | + |
| 25 | + const char* msg_; |
| 26 | + char pin_; |
| 27 | + unsigned int lastState_; |
| 28 | + |
| 29 | + unsigned int mask; |
| 30 | + unsigned char shift; |
| 31 | + |
| 32 | + unsigned long lastSendTime; |
| 33 | + |
| 34 | + int (*mapperCallback)(unsigned int, unsigned int); |
| 35 | + |
| 36 | + public: |
| 37 | + RotarySyncingPotentiometerEWMA(const char* msg, char pin, |
| 38 | + unsigned int syncToAddress, unsigned int syncToMask, unsigned char syncToShift, |
| 39 | + int (*mapperCallback)(unsigned int, unsigned int)) : |
| 40 | + PollingInput(pollIntervalMs), Int16Buffer(syncToAddress) { |
| 41 | + msg_ = msg; |
| 42 | + pin_ = pin; |
| 43 | + pinMode(pin_, INPUT); |
| 44 | + lastState_ = (float)readState(); |
| 45 | + |
| 46 | + this->mask = syncToMask; |
| 47 | + this->shift = syncToShift; |
| 48 | + lastSendTime = millis(); |
| 49 | + |
| 50 | + this->mapperCallback = mapperCallback; |
| 51 | + } |
| 52 | + |
| 53 | + void SetControl( const char* msg ) |
| 54 | + { |
| 55 | + msg_ = msg; |
| 56 | + } |
| 57 | + |
| 58 | + void resetThisState() |
| 59 | + { |
| 60 | + this->resetState(); |
| 61 | + } |
| 62 | + |
| 63 | + unsigned int getData() { |
| 64 | + return ((this->Int16Buffer::getData()) & mask) >> shift; |
| 65 | + } |
| 66 | + |
| 67 | + virtual void loop() { |
| 68 | + unsigned int dcsData = getData(); |
| 69 | + |
| 70 | + int requiredAdjustment = mapperCallback(lastState_, dcsData); |
| 71 | + |
| 72 | + //Serial.println("**************"); |
| 73 | + //Serial.write("lastState_:");Serial.println(lastState_); |
| 74 | + //Serial.write("dcsData:");Serial.println(dcsData); |
| 75 | + //Serial.write("requiredAdjustment:");Serial.println(requiredAdjustment); |
| 76 | + |
| 77 | + // Send the adjustment to DCS |
| 78 | + if( requiredAdjustment != 0 ) |
| 79 | + { |
| 80 | + if( millis() - lastSendTime > 100) |
| 81 | + { |
| 82 | + char buff[7]; |
| 83 | + sprintf(buff, "%+d", requiredAdjustment); |
| 84 | + tryToSendDcsBiosMessage(msg_, buff); |
| 85 | + lastSendTime = millis(); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + }; |
| 90 | + typedef RotarySyncingPotentiometerEWMA<> RotarySyncingPotentiometer; |
| 91 | + typedef RotarySyncingPotentiometerEWMA<POLL_EVERY_TIME, true> InvertedRotarySyncingPotentiometer; |
| 92 | +} |
| 93 | + |
| 94 | +#endif |
0 commit comments