jp112

HM-Mod-Re-8

Sep 29th, 2018
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.67 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 <Switch.h>
  15.  
  16.  
  17. // we use a Pro Mini
  18. // Arduino pin for the LED
  19. // D4 == PIN 4 on Pro Mini
  20. #define LED_PIN 4
  21. // Arduino pin for the config button
  22. // B0 == PIN 8 on Pro Mini
  23. #define CONFIG_BUTTON_PIN 8
  24.  
  25. #define RELAY1_PIN 14
  26. #define RELAY2_PIN 15
  27. #define RELAY3_PIN 16
  28. #define RELAY4_PIN 17
  29. #define RELAY5_PIN 7
  30. #define RELAY6_PIN 9
  31. #define RELAY7_PIN 6
  32. #define RELAY8_PIN 5
  33.  
  34. // number of available peers per channel
  35. #define PEERS_PER_CHANNEL 8
  36.  
  37. // all library classes are placed in the namespace 'as'
  38. using namespace as;
  39.  
  40. // define all device properties
  41. const struct DeviceInfo PROGMEM devinfo = {
  42.     {0x42,0xA2,0xB7},       // Device ID
  43.     "papa42a2b7",           // Device Serial
  44.     {0x00,0xbe},            // Device Model
  45.     0x10,                   // Firmware Version
  46.     as::DeviceType::Switch, // Device Type
  47.     {0x01,0x00}             // Info Bytes
  48. };
  49.  
  50. /**
  51.  * Configure the used hardware
  52.  */
  53. typedef AvrSPI<10,11,12,13> RadioSPI;
  54. typedef AskSin<StatusLed<LED_PIN>,BatterySensor,Radio<RadioSPI,2> > Hal;
  55.  
  56. DEFREGISTER(Reg0,DREG_INTKEY,DREG_LEDMODE,MASTERID_REGS,DREG_LOWBATLIMIT)
  57. class SwList0 : public RegList0<Reg0> {
  58. public:
  59.   SwList0(uint16_t addr) : RegList0<Reg0>(addr) {}
  60.   void defaults () {
  61.     clear();
  62.     lowBatLimit(22);
  63.   }
  64. };
  65.  
  66. // setup the device with channel type and number of channels
  67. class SwitchType : public MultiChannelDevice<Hal,SwitchChannel<Hal,PEERS_PER_CHANNEL,SwList0>,8,SwList0> {
  68. public:
  69.   typedef MultiChannelDevice<Hal,SwitchChannel<Hal,PEERS_PER_CHANNEL,SwList0>,8,SwList0> DevType;
  70.   SwitchType (const DeviceInfo& i,uint16_t addr) : DevType(i,addr) {}
  71.   virtual ~SwitchType () {}
  72.  
  73.   virtual void configChanged () {
  74.     DevType::configChanged();
  75.     uint8_t lowbat = getList0().lowBatLimit();
  76.     DDECLN(lowbat);
  77.     if( lowbat > 0 ) {
  78.       battery().low(lowbat);
  79.     }
  80.   }
  81. };
  82.  
  83. Hal hal;
  84. SwitchType sdev(devinfo,0x20);
  85. ConfigButton<SwitchType> cfgBtn(sdev);
  86. BurstDetector<Hal> bd(hal);
  87.  
  88. void initPeerings (bool first) {
  89.   // create internal peerings - CCU2 needs this
  90.   if( first == true ) {
  91.     HMID devid;
  92.     sdev.getDeviceID(devid);
  93.     for( uint8_t i=1; i<=sdev.channels(); ++i ) {
  94.       Peer ipeer(devid,i);
  95.       sdev.channel(i).peer(ipeer);
  96.     }
  97.   }
  98. }
  99.  
  100.  
  101. void setup () {
  102.   DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER);
  103.   bool first = sdev.init(hal);
  104.   sdev.channel(1).init(RELAY1_PIN);
  105.   sdev.channel(2).init(RELAY2_PIN);
  106.   sdev.channel(3).init(RELAY3_PIN);
  107.   sdev.channel(4).init(RELAY4_PIN);
  108.   sdev.channel(5).init(RELAY5_PIN);
  109.   sdev.channel(6).init(RELAY6_PIN);
  110.   sdev.channel(7).init(RELAY7_PIN);
  111.   sdev.channel(8).init(RELAY8_PIN);
  112.   buttonISR(cfgBtn,CONFIG_BUTTON_PIN);
  113.   initPeerings(first);
  114.   // start burst detection
  115.   bd.enable(sysclock);
  116.   // stay on for 15 seconds after start
  117.   hal.activity.stayAwake(seconds2ticks(15));
  118.   // measure battery every hour
  119.   hal.battery.init(seconds2ticks(60UL*60),sysclock);
  120.   sdev.initDone();
  121. }
  122.  
  123. void loop() {
  124.   bool worked = hal.runready();
  125.   bool poll = sdev.pollRadio();
  126.   if( worked == false && poll == false ) {
  127.     hal.activity.savePower<Sleep<> >(hal);
  128.   }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment