Advertisement
Guest User

DcsBios.h

a guest
Jan 30th, 2015
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.32 KB | None | 0 0
  1. #ifndef __DCSBIOS_H
  2. extern unsigned int pinValues;
  3. #define __DCSBIOS_H
  4.  
  5. #include "Arduino.h"
  6. #include <Servo.h>
  7.  
  8. void onDcsBiosWrite(unsigned int address, unsigned int value);
  9. void sendDcsBiosMessage(const char* msg, const char* args);
  10.  
  11. #define DCSBIOS_STATE_WAIT_FOR_SYNC 0
  12. #define DCSBIOS_STATE_ADDRESS_LOW 1
  13. #define DCSBIOS_STATE_ADDRESS_HIGH 2
  14. #define DCSBIOS_STATE_COUNT_LOW 3
  15. #define DCSBIOS_STATE_COUNT_HIGH 4
  16. #define DCSBIOS_STATE_DATA_LOW 5
  17. #define DCSBIOS_STATE_DATA_HIGH 6
  18.  
  19. namespace DcsBios {
  20.  
  21. class ProtocolParser {
  22.     private:
  23.         unsigned char state;
  24.         unsigned int address;
  25.         unsigned int count;
  26.         unsigned int data;
  27.         unsigned char sync_byte_count;
  28.     public:
  29.         void processChar(unsigned char c);
  30.         ProtocolParser();
  31.  
  32. };
  33.  
  34. class ExportStreamListener {
  35.     private:
  36.         virtual void onDcsBiosWrite(unsigned int address, unsigned int value) {}
  37.         virtual void onDcsBiosFrameSync() {}
  38.         ExportStreamListener* nextExportStreamListener;
  39.     public:
  40.         static ExportStreamListener* firstExportStreamListener;
  41.         ExportStreamListener() {
  42.             this->nextExportStreamListener = firstExportStreamListener;
  43.             firstExportStreamListener = this;
  44.         }
  45.         static void handleDcsBiosWrite(unsigned int address, unsigned int value) {
  46.             ExportStreamListener* el = firstExportStreamListener;
  47.             while (el) {
  48.                 el->onDcsBiosWrite(address, value);
  49.                 el = el->nextExportStreamListener;
  50.             }
  51.         }
  52.         static void handleDcsBiosFrameSync() {
  53.             ExportStreamListener* el = firstExportStreamListener;
  54.             while (el) {
  55.                 el->onDcsBiosFrameSync();
  56.                 el = el->nextExportStreamListener;
  57.             }
  58.         }
  59. };
  60.  
  61. class PollingInput {
  62.     private:
  63.         virtual void pollInput();
  64.         PollingInput* nextPollingInput;
  65.     public:
  66.         static PollingInput* firstPollingInput;
  67.         PollingInput() {
  68.             this->nextPollingInput = firstPollingInput;
  69.             firstPollingInput = this;
  70.         }
  71.         static void pollInputs() {
  72.             PollingInput* pi = firstPollingInput;
  73.             while (pi) {
  74.                 pi->pollInput();
  75.                 pi = pi->nextPollingInput;
  76.             }
  77.         }
  78. };
  79.  
  80. class ActionButton : PollingInput {
  81.     private:
  82.         void pollInput();
  83.         char* msg_;
  84.         char* arg_;
  85.         char pin_;
  86.         char lastState_;
  87.     public:
  88.         ActionButton(char* msg, char* arg, char pin);
  89. };
  90.  
  91. class ActionButtonSR : PollingInput {
  92.     private:
  93.         void pollInput();
  94.         char* msg_;
  95.         char* arg_;
  96.         char srinput_;
  97.         int lastState_;
  98.         unsigned int pinValues_;
  99.     public:
  100.         ActionButtonSR(char* msg, char* arg, char srinput);
  101. };
  102.  
  103.  
  104.  
  105.  
  106.  
  107. class Switch2Pos : PollingInput {
  108.     private:
  109.         void pollInput();
  110.         char* msg_;
  111.         char pin_;
  112.         char lastState_;
  113.     public:
  114.         Switch2Pos(char* msg, char pin);
  115. };
  116.  
  117. class Switch2PosSR : PollingInput {
  118.     private:
  119.         void pollInput();
  120.         char* msg_;
  121.         char srinput_;
  122.         int lastState_;
  123.         unsigned int pinValues_;
  124.     public:
  125.         Switch2PosSR(char* msg, char srinput);
  126. };
  127.  
  128. class Switch3Pos : PollingInput {
  129.     private:
  130.         void pollInput();
  131.         char* msg_;
  132.         char pinA_;
  133.         char pinB_;
  134.         char lastState_;
  135.         char readState();
  136.     public:
  137.         Switch3Pos(char* msg, char pinA, char pinB);
  138. };
  139.  
  140.  
  141. class Switch3PosSR : PollingInput {
  142.     private:
  143.         void pollInput();
  144.         char* msg_;
  145.         char srinputA_;
  146.         char srinputB_;
  147.         int lastState_;
  148.         int readState();
  149.         unsigned int pinValues_;
  150.     public:
  151.         Switch3PosSR(char* msg, char srinputA, char srinputB);
  152. };
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163. class SwitchMultiPos : PollingInput {
  164.     private:
  165.         void pollInput();
  166.         char* msg_;
  167.         const byte* pins_;
  168.         char numberOfPins_;
  169.         char lastState_;
  170.         char readState();
  171.     public:
  172.         SwitchMultiPos(char* msg_, const byte* pins, char numberOfPins);
  173. };
  174.  
  175.  
  176.  
  177. class SwitchMultiPosSR : PollingInput {
  178.     private:
  179.         void pollInput();
  180.         char* msg_;
  181.         const byte* SRpins_;
  182.         char numberOfSRPins_;
  183.         char lastState_;
  184.         int readState();
  185.         unsigned int pinValues_;
  186.     public:
  187.         SwitchMultiPosSR(char* msg_, const byte* SRpins, char numberOfSRPins);
  188. };
  189.  
  190.  
  191.  
  192.  
  193.  
  194. class Potentiometer : PollingInput {
  195.     private:
  196.         void pollInput();
  197.         char* msg_;
  198.         char pin_;
  199.         unsigned int lastState_;
  200.     public:
  201.         Potentiometer(char* msg, char pin);
  202. };
  203.  
  204. class RotaryEncoder : PollingInput {
  205.     private:
  206.         void pollInput();
  207.         char readState();
  208.         const char* msg_;
  209.         const char* decArg_;
  210.         const char* incArg_;
  211.         char pinA_;
  212.         char pinB_;
  213.         char lastState_;
  214.         signed char delta_;
  215.     public:
  216.         RotaryEncoder(const char* msg, const char* decArg, const char* incArg, char pinA, char pinB);  
  217. };
  218.  
  219.  
  220.  
  221.  
  222.  
  223. /* ******NON-FUNCTIONING********* */
  224.  
  225.  
  226. class RotaryEncoderSR : PollingInput {
  227.     private:
  228.         void pollInput();
  229.         int readState();
  230.         const char* msg_;
  231.         const char* decArg_;
  232.         const char* incArg_;
  233.         char SRpinA_;
  234.         char SRpinB_;
  235.         char lastState_;
  236.         signed char delta_;
  237.         unsigned int pinValues_;
  238.     public:
  239.         RotaryEncoderSR(const char* msg, const char* decArg, const char* incArg, char SRpinA, char SRpinB);
  240. };
  241.  
  242.  
  243. /* ******NON-FUNCTIONING********* */
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255. class LED : ExportStreamListener {
  256.     private:
  257.         void onDcsBiosWrite(unsigned int address, unsigned int value);
  258.         unsigned char pin_;
  259.         unsigned int address_;
  260.         unsigned int mask_;
  261.     public:
  262.         LED(unsigned int address, unsigned int mask, char pin);
  263. };
  264.  
  265. class ServoOutput : ExportStreamListener {
  266.     private:
  267.         void onDcsBiosWrite(unsigned int address, unsigned int value);
  268.         Servo servo_;
  269.         unsigned int address_;
  270.         char pin_;
  271.         int minPulseWidth_;
  272.         int maxPulseWidth_;
  273.     public:
  274.         ServoOutput(unsigned int address, char pin, int minPulseWidth, int maxPulseWidth);
  275.         ServoOutput(unsigned int address, char pin) { ServoOutput(address, pin, 544, 2400); }
  276. };
  277.  
  278. template < unsigned int LENGTH >
  279. class StringBuffer : ExportStreamListener {
  280.     private:
  281.         void onDcsBiosWrite(unsigned int address, unsigned int value) {
  282.             if ((address >= address_) && (address_ + LENGTH > address)) {
  283.             buffer[address - address_] = ((char*)&value)[0];
  284.             if (address_ + LENGTH > (address+1))
  285.                 buffer[address - address_ + 1] = ((char*)&value)[1];
  286.             }
  287.             dirty_ = true;
  288.         }
  289.         void onDcsBiosFrameSync() {
  290.             if (dirty_) {
  291.                 callback_(buffer);
  292.                 dirty_ = false;
  293.             }
  294.         }
  295.         unsigned int address_;
  296.         bool dirty_;
  297.         void (*callback_)(char*);
  298.     public:
  299.         char buffer[LENGTH+1];
  300.         StringBuffer(unsigned int address, void (*callback)(char*)) {
  301.             callback_ = callback;
  302.             dirty_ = false;
  303.             address_ = address;
  304.             memset(buffer, '\0', LENGTH+1);
  305.         }
  306. };
  307.  
  308.  
  309.  
  310. }
  311. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement