jp112

HB-UNI-Sen-DIST-US

Jun 23rd, 2018
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.31 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. // 2018-04-16 jp112sdl Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
  5. //- -----------------------------------------------------------------------------------------------------------------------
  6.  
  7. // define this to read the device id, serial and device type from bootloader section
  8. // #define USE_OTA_BOOTLOADER
  9.  
  10. #define EI_NOTEXTERNAL
  11. #include <EnableInterrupt.h>
  12. #include <AskSinPP.h>
  13. #include <LowPower.h>
  14.  
  15. #include <Register.h>
  16. #include <MultiChannelDevice.h>
  17.  
  18. // Arduino Pro mini 8 Mhz
  19. // Arduino pin for the config button
  20. #define CONFIG_BUTTON_PIN  8
  21. #define LED_PIN            4
  22.  
  23. #define SENSOR_EN_PIN      5 //VCC Pin des Sensors
  24. #define SENSOR_ECHO_PIN    6
  25. #define SENSOR_TRIG_PIN    14 //A0
  26. #define BATT_EN_PIN        15
  27. #define BATT_SENS_PIN      17
  28.  
  29. // number of channels
  30. #define CHANNELS          1
  31. // number of available peers per channel
  32. #define PEERS_PER_CHANNEL 6
  33.  
  34. // all library classes are placed in the namespace 'as'
  35. using namespace as;
  36.  
  37. //Korrekturfaktor der Clock-Ungenauigkeit, wenn keine RTC verwendet wird
  38. #define SYSCLOCK_FACTOR    0.88
  39.  
  40. // define all device properties
  41. const struct DeviceInfo PROGMEM devinfo = {
  42.   {0xF9, 0xD6, 0x01},          // Device ID
  43.   "JPDIST0001",                // Device Serial
  44.   {0xF9, 0xD6},                // Device Model
  45.   0x10,                        // Firmware Version
  46.   0x53,    // Device Type
  47.   {0x01, 0x01}                 // Info Bytes
  48. };
  49.  
  50. /**
  51.    Configure the used hardware
  52. */
  53. typedef AskSin<StatusLed<LED_PIN>, BatterySensorUni<BATT_SENS_PIN, BATT_EN_PIN>, Radio<AvrSPI<10, 11, 12, 13>, 2>> BaseHal;
  54. class Hal : public BaseHal {
  55.   public:
  56.     void init (const HMID& id) {
  57.       BaseHal::init(id);
  58.       battery.init(seconds2ticks(60UL * 60) * SYSCLOCK_FACTOR, sysclock); //battery measure once an hour
  59.       battery.low(22);
  60.       battery.critical(19);
  61.     }
  62.  
  63.     bool runready () {
  64.       return sysclock.runready() || BaseHal::runready();
  65.     }
  66. } hal;
  67.  
  68.  
  69. DEFREGISTER(UReg0, MASTERID_REGS, DREG_LOWBATLIMIT, 0x20, 0x21)
  70. class UList0 : public RegList0<UReg0> {
  71.   public:
  72.     UList0 (uint16_t addr) : RegList0<UReg0>(addr) {}
  73.  
  74.     bool Sendeintervall (uint16_t value) const {
  75.       return this->writeRegister(0x20, (value >> 8) & 0xff) && this->writeRegister(0x21, value & 0xff);
  76.     }
  77.     uint16_t Sendeintervall () const {
  78.       return (this->readRegister(0x20, 0) << 8) + this->readRegister(0x21, 0);
  79.     }
  80.  
  81.     void defaults () {
  82.       clear();
  83.       lowBatLimit(22);
  84.       Sendeintervall(180);
  85.     }
  86. };
  87.  
  88. DEFREGISTER(UReg1)
  89. class UList1 : public RegList1<UReg1> {
  90.   public:
  91.     UList1 (uint16_t addr) : RegList1<UReg1>(addr) {}
  92.     void defaults () {
  93.       //just Skeleton
  94.       clear();
  95.     }
  96. };
  97.  
  98. class MeasureEventMsg : public Message {
  99.   public:
  100.     void init(uint8_t msgcnt, uint8_t channel, uint8_t batlow, uint16_t dist, uint8_t volt) {
  101.       Message::init(0x0e, msgcnt, 0x53, BCAST , batlow ? 0x80 : 0x00, channel & 0xff);
  102.       pload[0] = (dist >> 8) & 0xff;
  103.       pload[1] = dist & 0xff;
  104.       pload[2] = volt & 0xff;
  105.     }
  106. };
  107.  
  108. class MeasureChannel : public Channel<Hal, UList1, EmptyList, List4, PEERS_PER_CHANNEL, UList0>, public Alarm {
  109.     MeasureEventMsg msg;
  110.     uint16_t        distance;
  111.  
  112.   public:
  113.     MeasureChannel () : Channel(), Alarm(0), distance(0) {}
  114.     virtual ~MeasureChannel () {}
  115.  
  116.     void measure() {
  117.       digitalWrite(SENSOR_EN_PIN, HIGH);
  118.       _delay_ms(250);
  119.       digitalWrite(SENSOR_TRIG_PIN, LOW);
  120.       delayMicroseconds(2);
  121.       digitalWrite(SENSOR_TRIG_PIN, HIGH);
  122.       delayMicroseconds(10);
  123.       digitalWrite(SENSOR_TRIG_PIN, LOW);
  124.       distance = pulseIn(SENSOR_ECHO_PIN, HIGH, 26000);
  125.       distance = distance / 58;
  126.       digitalWrite(SENSOR_EN_PIN, LOW);
  127.       DPRINT(F("DISTANCE: ")); DDEC(distance); DPRINTLN(F(" cm"));
  128.  
  129.       //distance = random(600);
  130.     }
  131.  
  132.     virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) {
  133.       measure();
  134.       tick = delay();
  135.       msg.init(device().nextcount(), number(),   device().battery().low(), distance,  device().battery().current());
  136.       device().sendPeerEvent(msg, *this);
  137.       sysclock.add(*this);
  138.     }
  139.  
  140.     uint32_t delay () {
  141.       uint16_t _txMindelay = 20;
  142.       _txMindelay = device().getList0().Sendeintervall();
  143.       if (_txMindelay == 0) _txMindelay = 20;
  144.       return seconds2ticks(_txMindelay  * SYSCLOCK_FACTOR);
  145.     }
  146.  
  147.     void configChanged() {
  148.       //DPRINTLN(F("Config changed List1"));
  149.     }
  150.  
  151.     void setup(Device<Hal, UList0>* dev, uint8_t number, uint16_t addr) {
  152.       Channel::setup(dev, number, addr);
  153.       pinMode(SENSOR_ECHO_PIN, INPUT_PULLUP);
  154.       pinMode(SENSOR_TRIG_PIN, OUTPUT);
  155.       pinMode(SENSOR_EN_PIN, OUTPUT);
  156.       sysclock.add(*this);
  157.     }
  158.  
  159.     uint8_t status () const {
  160.       return 0;
  161.     }
  162.  
  163.     uint8_t flags () const {
  164.       return 0;
  165.     }
  166. };
  167.  
  168. class UType : public MultiChannelDevice<Hal, MeasureChannel, CHANNELS, UList0> {
  169.   public:
  170.     typedef MultiChannelDevice<Hal, MeasureChannel, CHANNELS, UList0> TSDevice;
  171.     UType(const DeviceInfo& info, uint16_t addr) : TSDevice(info, addr) {}
  172.     virtual ~UType () {}
  173.  
  174.     virtual void configChanged () {
  175.       TSDevice::configChanged();
  176.       DPRINT(F("*LOW BAT Limit: "));
  177.       DDECLN(this->getList0().lowBatLimit());
  178.       this->battery().low(this->getList0().lowBatLimit());
  179.       DPRINT(F("*Sendeintervall: ")); DDECLN(this->getList0().Sendeintervall());
  180.     }
  181. };
  182.  
  183. UType sdev(devinfo, 0x20);
  184. ConfigButton<UType> cfgBtn(sdev);
  185.  
  186. void setup () {
  187.   DINIT(57600, ASKSIN_PLUS_PLUS_IDENTIFIER);
  188.   printDeviceInfo();
  189.   sdev.init(hal);
  190.   buttonISR(cfgBtn, CONFIG_BUTTON_PIN);
  191.   sdev.initDone();
  192. }
  193.  
  194. void loop() {
  195.   bool worked = hal.runready();
  196.   bool poll = sdev.pollRadio();
  197.   if ( worked == false && poll == false ) {
  198.     hal.activity.savePower<Sleep<>>(hal);
  199.   }
  200. }
  201.  
  202. void printDeviceInfo() {
  203.   HMID ids;
  204.   sdev.getDeviceID(ids);
  205.  
  206.   uint8_t ser[10];
  207.   sdev.getDeviceSerial(ser);
  208.  
  209.   DPRINT(F("Device Info: "));
  210.   for (int i = 0; i < 10; i++) {
  211.     DPRINT(char(ser[i]));
  212.   }
  213.   DPRINT(" ("); DHEX(ids); DPRINTLN(")");
  214. }
Advertisement
Add Comment
Please, Sign In to add comment