jp112

RHS

Apr 2nd, 2018
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.82 KB | None | 0 0
  1. //- -----------------------------------------------------------------------------------------------------------------------
  2. // AskSin++
  3. // 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
  4. //- -----------------------------------------------------------------------------------------------------------------------
  5.  
  6. // define this to read the device id, serial and device type from bootloader section
  7. // #define USE_OTA_BOOTLOADER
  8.  
  9. #define EI_NOTEXTERNAL
  10. #include <EnableInterrupt.h>
  11. #include <AskSinPP.h>
  12. #include <LowPower.h>
  13.  
  14. #include <Register.h>
  15. #include <ThreeState.h>
  16.  
  17. // we use a Pro Mini
  18. // Arduino pin for the LED
  19. // D4 == PIN 4 on Pro Mini
  20. #define LED1_PIN 4
  21. #define LED2_PIN 5
  22. // Arduino pin for the config button
  23. // B0 == PIN 8 on Pro Mini
  24. #define CONFIG_BUTTON_PIN 8
  25.  
  26. #define SENS11_PIN 14
  27. #define SENS12_PIN 15
  28.  
  29. #define SENS21_PIN 16
  30. #define SENS22_PIN 17
  31.  
  32. #define SENS31_PIN 3
  33. #define SENS32_PIN 6
  34.  
  35. #define SENS41_PIN 7
  36. #define SENS42_PIN 9
  37.  
  38. // number of available peers per channel
  39. #define PEERS_PER_CHANNEL 10
  40.  
  41. // all library classes are placed in the namespace 'as'
  42. using namespace as;
  43.  
  44. // define all device properties
  45. const struct DeviceInfo PROGMEM devinfo = {
  46.     {0x09,0x56,0x34},       // Device ID
  47.     "papa222111",           // Device Serial
  48.     {0x00,0xC3},            // Device Model
  49.     0x22,                   // Firmware Version
  50.     as::DeviceType::ThreeStateSensor, // Device Type
  51.     {0x01,0x00}             // Info Bytes
  52. };
  53.  
  54. /**
  55.  * Configure the used hardware
  56.  */
  57. typedef AvrSPI<10,11,12,13> SPIType;
  58. typedef Radio<SPIType,2> RadioType;
  59. typedef DualStatusLed<LED2_PIN,LED1_PIN> LedType;
  60. typedef AskSin<LedType,BatterySensor,RadioType> BaseHal;
  61. class Hal : public BaseHal {
  62. public:
  63.   void init (const HMID& id) {
  64.     BaseHal::init(id);
  65.     // measure battery every 1h
  66.     battery.init(seconds2ticks(60UL*60),sysclock);
  67.   }
  68. } hal;
  69.  
  70. DEFREGISTER(Reg0,DREG_INTKEY,DREG_CYCLICINFOMSG,MASTERID_REGS,DREG_TRANSMITTRYMAX,DREG_SABOTAGEMSG)
  71. class RHSList0 : public RegList0<Reg0> {
  72. public:
  73.   RHSList0(uint16_t addr) : RegList0<Reg0>(addr) {}
  74.   void defaults () {
  75.     clear();
  76.     cycleInfoMsg(true);
  77.     transmitDevTryMax(6);
  78.     sabotageMsg(true);
  79.   }
  80. };
  81.  
  82. DEFREGISTER(Reg1,CREG_AES_ACTIVE,CREG_MSGFORPOS,CREG_EVENTDELAYTIME,CREG_LEDONTIME,CREG_TRANSMITTRYMAX)
  83. class RHSList1 : public RegList1<Reg1> {
  84. public:
  85.   RHSList1 (uint16_t addr) : RegList1<Reg1>(addr) {}
  86.   void defaults () {
  87.     clear();
  88.     msgForPosA(1); // CLOSED
  89.     msgForPosB(2); // OPEN
  90.     msgForPosC(3); // TILTED
  91.     // aesActive(false);
  92.     // eventDelaytime(0);
  93.     ledOntime(100);
  94.     transmitTryMax(6);
  95.   }
  96. };
  97.  
  98. typedef ThreeStateChannel<Hal,RHSList0,RHSList1,DefList4,PEERS_PER_CHANNEL> ChannelType;
  99. typedef ThreeStateDevice<Hal,ChannelType,4,RHSList0> RHSType;
  100.  
  101. RHSType sdev(devinfo,0x20);
  102. ConfigButton<RHSType> cfgBtn(sdev);
  103.  
  104. void setup () {
  105.   DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER);
  106.   sdev.init(hal);
  107.   buttonISR(cfgBtn,CONFIG_BUTTON_PIN);
  108.   const uint8_t posmap[4] = {Position::State::PosB,Position::State::PosC,Position::State::PosA,Position::State::PosB};
  109.  
  110.   sdev.channel(1).init(SENS11_PIN,SENS12_PIN,posmap);
  111.   sdev.channel(2).init(SENS21_PIN,SENS22_PIN,posmap);
  112.   sdev.channel(3).init(SENS31_PIN,SENS32_PIN,posmap);
  113.   sdev.channel(4).init(SENS41_PIN,SENS42_PIN,posmap);
  114.  
  115.   sdev.initDone();
  116. }
  117.  
  118. void loop() {
  119.   bool worked = hal.runready();
  120.   bool poll = sdev.pollRadio();
  121.   if( worked == false && poll == false ) {
  122.     // deep discharge protection
  123.     // if we drop below critical battery level - switch off all and sleep forever
  124.     if( hal.battery.critical() ) {
  125.       // this call will never return
  126.       hal.activity.sleepForever(hal);
  127.     }
  128.     // if nothing to do - go sleep
  129.     hal.activity.savePower<Sleep<> >(hal);
  130.   }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment