Advertisement
Guest User

somewhat works but resets arduino

a guest
Jan 24th, 2015
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. -----------------------------------
  2. **********DcsBios.h**********
  3. ------------------------------------
  4. [...]
  5. class Switch2PosSR : PollingInput {
  6.     private:
  7.         void pollInput();
  8.         char* msg_;
  9.         char srinput_;
  10.         char lastState_;
  11.        
  12.     public:
  13.         Switch2PosSR(char* msg, char srinput);
  14. };
  15.  
  16.  
  17. [...]
  18.  
  19.  
  20. -----------------------------------
  21. **********DcsBios.cpp**********
  22. ------------------------------------
  23.  
  24. Switch2PosSR::Switch2PosSR(char* msg, char srinput) {
  25.     msg_ = msg;
  26.     srinput_ = srinput;
  27.    
  28.      
  29.     lastState_ = srinput_;
  30. }
  31. void Switch2PosSR::pollInput() {
  32.    
  33.      
  34.     char state = srinput_;
  35.    
  36.     if (state != lastState_) {
  37.         sendDcsBiosMessage(msg_, state == '1' ? "0" : "1"); // ?
  38.     }
  39.     lastState_ = state;
  40. }
  41.  
  42.  
  43.  
  44.  
  45. -----------------------------
  46. *********.INO FILE:********
  47. ---------------------------------
  48.  
  49.  
  50. #include <DcsBios.h>
  51. #include <Servo.h>
  52.  
  53.  
  54. DcsBios::ActionButton ahcpHudModeToggle("AHCP_HUD_MODE", "TOGGLE", 44);
  55. DcsBios::Switch2PosSR ahcpCicu("AHCP_CICU", 1);
  56.  
  57.  
  58. int ploadPin        = 2;  // Connects to Parallel load pin the 165
  59. int dataPin         = 3; // Connects to the Q7 pin the 165
  60. int clockPin        = 4; // Connects to the Clock pin the 165
  61.  
  62.  
  63. unsigned int pinValues = read_shift_regs();
  64. unsigned int oldPinValues;
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. unsigned int read_shift_regs()
  73. {
  74.     byte bitVal;
  75.     unsigned int bytesVal = 0;
  76.  
  77.     /* Trigger a parallel Load to latch the state of the data lines,
  78.     */
  79.     digitalWrite(ploadPin, LOW);
  80.     delayMicroseconds(5);
  81.     digitalWrite(ploadPin, HIGH);
  82.  
  83.     /* Loop to read each bit value from the serial out line
  84.      * of the SN74HC165N.
  85.     */
  86.     for(int i = 0; i < 8; i++)
  87.     {
  88.         bitVal = digitalRead(dataPin);
  89.  
  90.         /* Set the corresponding bit in bytesVal.
  91.         */
  92.         bytesVal |= (bitVal << ((8-1) - i));
  93.  
  94.         /* Pulse the Clock (rising edge shifts the next bit).
  95.         */
  96.         digitalWrite(clockPin, HIGH);
  97.         delayMicroseconds(5);
  98.         digitalWrite(clockPin, LOW);
  99.     }
  100.  
  101.     return(bytesVal);
  102. }
  103.  
  104. void display_pin_values()
  105. {
  106.  
  107.          if((pinValues & (1<<0)))
  108.             DcsBios::Switch2PosSR ahcpCicu("AHCP_CICU", '1');
  109.         if((pinValues & (1<<1)))
  110.            DcsBios::Switch2PosSR ahcpHudDaynight("AHCP_HUD_DAYNIGHT", '0');
  111.      
  112.     }
  113.  
  114.  
  115. DcsBios::ProtocolParser parser;
  116.  
  117. void setup() {
  118.   Serial.begin(500000);
  119.  
  120. /* Initialize our digital pins...
  121.     */
  122.     pinMode(ploadPin, OUTPUT);
  123.     pinMode(clockPin, OUTPUT);
  124.     pinMode(dataPin, INPUT);
  125.  
  126.     digitalWrite(clockPin, LOW);
  127.     digitalWrite(ploadPin, HIGH);
  128.  
  129.     /* Read in and display the pin states at startup.
  130.     */
  131.     pinValues = read_shift_regs();
  132.    
  133.     oldPinValues = pinValues;
  134.    
  135.    
  136. }
  137.  
  138. void loop() {
  139.  
  140.   // feed incoming data to the parser
  141.   while (Serial.available()) {
  142.       parser.processChar(Serial.read());
  143.   }
  144.     // poll inputs
  145.   DcsBios::PollingInput::pollInputs();
  146.  
  147.  
  148.    pinValues = read_shift_regs();
  149.    if(pinValues != oldPinValues)
  150.    
  151.     display_pin_values();
  152.      
  153.     oldPinValues = pinValues;
  154.    
  155.    
  156.  
  157.    
  158.     }
  159.    
  160.  
  161. void sendDcsBiosMessage(const char* msg, const char* arg) {
  162.   Serial.write(msg);
  163.   Serial.write(' ');
  164.   Serial.write(arg);
  165.   Serial.write('\n');
  166. }
  167.  
  168.  
  169. void onDcsBiosWrite(unsigned int address, unsigned int value) {
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement