jp112

HM-Sen-LI-O_fix_600sec

Apr 10th, 2018
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.97 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. #include <Register.h>
  14. //#include <sensors/Bh1750.h>
  15. #include <sensors/Tsl2561.h>
  16.  
  17. #include <MultiChannelDevice.h>
  18.  
  19. // we use a Pro Mini
  20. // Arduino pin for the LED
  21. // D4 == PIN 4 on Pro Mini
  22. #define LED_PIN 4
  23. // Arduino pin for the config button
  24. // B0 == PIN 8 on Pro Mini
  25. #define CONFIG_BUTTON_PIN 8
  26.  
  27. // number of available peers per channel
  28. #define PEERS_PER_CHANNEL 6
  29.  
  30. //seconds between sending messages
  31. uint16_t _txMindelay = 600;
  32.  
  33. // all library classes are placed in the namespace 'as'
  34. using namespace as;
  35.  
  36.  
  37. // define all device properties
  38. const struct DeviceInfo PROGMEM devinfo = {
  39.   {0x34, 0xfd, 0x01},     // Device ID
  40.   "JPLIO00001",           // Device Serial
  41.   {0x00, 0xfd},           // Device Model
  42.   0x01,                   // Firmware Version
  43.   0x53, // Device Type
  44.   {0x01, 0x00}            // Info Bytes
  45. };
  46.  
  47. /**
  48.    Configure the used hardware
  49. */
  50. typedef AvrSPI<10, 11, 12, 13> SPIType;
  51. typedef Radio<SPIType, 2> RadioType;
  52. typedef StatusLed<4> LedType;
  53. typedef AskSin<LedType, BatterySensor, RadioType> BaseHal;
  54. class Hal : public BaseHal {
  55.   public:
  56.     void init (const HMID& id) {
  57.       BaseHal::init(id);
  58.       // measure battery every 1h
  59.       battery.init(seconds2ticks(60UL * 60), sysclock);
  60.       battery.low(22);
  61.       battery.critical(19);
  62.     }
  63.  
  64.     bool runready () {
  65.       return sysclock.runready() || BaseHal::runready();
  66.     }
  67. } hal;
  68.  
  69. DEFREGISTER(LiReg0, MASTERID_REGS, DREG_CYCLICINFOMSGDIS, DREG_LOCALRESETDISABLE, DREG_TRANSMITTRYMAX)
  70. class LiList0 : public RegList0<LiReg0> {
  71.   public:
  72.     LiList0 (uint16_t addr) : RegList0<LiReg0>(addr) {}
  73.     void defaults () {
  74.       clear();
  75.       //cyclicInfoMsgDis(0);
  76.       // intKeyVisible(false);
  77.       // localResetDisable(false);
  78.     }
  79. };
  80.  
  81. DEFREGISTER(LiReg1, CREG_AES_ACTIVE, CREG_TX_MINDELAY, CREG_TX_THRESHOLD_PERCENT)
  82. class LiList1 : public RegList1<LiReg1> {
  83.   public:
  84.     LiList1 (uint16_t addr) : RegList1<LiReg1>(addr) {}
  85.     void defaults () {
  86.       clear();
  87.       aesActive(false);
  88.       txMindelay(8);
  89.       //txThresholdPercent(0);
  90.     }
  91. };
  92.  
  93. class LuxEventMsg : public Message {
  94.   public:
  95.     void init(uint8_t msgcnt, uint32_t lux) {
  96.       Message::init(0xf, msgcnt, 0x53, RPTEN | BCAST, 0x00, 0xc1);
  97.       pload[0] = (lux >> 32)  & 0xff;
  98.       pload[1] = (lux >> 16) & 0xff;
  99.       pload[2] = (lux >> 8) & 0xff;
  100.       pload[3] = (lux) & 0xff;
  101.     }
  102. };
  103.  
  104. class LuxChannel : public Channel<Hal, LiList1, EmptyList, List4, PEERS_PER_CHANNEL, LiList0>, public Alarm {
  105.  
  106.     LuxEventMsg   lmsg;
  107.     uint32_t      lux;
  108.     uint16_t      millis;
  109.  
  110.     //Bh1750<>     bh1750;
  111.     Tsl2561<>      tsl2561;
  112.  
  113.     uint8_t last_flags = 0xff;
  114.  
  115.   public:
  116.     LuxChannel () : Channel(), Alarm(5), lux(0), millis(0) {}
  117.     virtual ~LuxChannel () {}
  118.  
  119.     // here we do the measurement
  120.     void measure () {
  121.       DPRINT("Measure... ");
  122.       //bh1750.measure();
  123.       //lux = bh1750.brightness();
  124.       tsl2561.measure();
  125.       lux = tsl2561.brightness();
  126.       DDEC(lux);
  127.       DPRINTLN(" lux");
  128.     }
  129.  
  130.     uint8_t flags () const {
  131.       uint8_t flags = this->device().battery().low() ? 0x80 : 0x00;
  132.       return flags;
  133.     }
  134.  
  135.     virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) {
  136.       uint8_t msgcnt = device().nextcount();
  137.       // reactivate for next measure
  138.       tick = delay();
  139.       clock.add(*this);
  140.  
  141.       measure();
  142.      
  143.       if (last_flags != flags()) {
  144.         this->changed(true);
  145.         last_flags = flags();
  146.       }
  147.  
  148.       lmsg.init(msgcnt, lux * 100);
  149.       device().sendPeerEvent(lmsg, *this);
  150.     }
  151.  
  152.     uint32_t delay () {
  153.       DPRINT("TX Delay = ");
  154.       DDECLN(_txMindelay);
  155.       return seconds2ticks(_txMindelay);
  156.     }
  157.  
  158.     void setup(Device<Hal, LiList0>* dev, uint8_t number, uint16_t addr) {
  159.       Channel::setup(dev, number, addr);
  160.       sysclock.add(*this);
  161.       //bh1750.init();
  162.       tsl2561.init();
  163.     }
  164.  
  165.     uint8_t status () const {
  166.       return 0;
  167.     }
  168. };
  169.  
  170.  
  171. typedef MultiChannelDevice<Hal, LuxChannel, 1, LiList0> LuxType;
  172.  
  173. LuxType sdev(devinfo, 0x20);
  174. ConfigButton<LuxType> cfgBtn(sdev);
  175.  
  176. void setup () {
  177.   DINIT(57600, ASKSIN_PLUS_PLUS_IDENTIFIER);
  178.   sdev.init(hal);
  179.   buttonISR(cfgBtn, CONFIG_BUTTON_PIN);
  180.   sdev.initDone();
  181. }
  182.  
  183. void loop() {
  184.   bool worked = hal.runready();
  185.   bool poll = sdev.pollRadio();
  186.   if ( worked == false && poll == false ) {
  187.     hal.activity.savePower<Sleep<>>(hal);
  188.   }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment