Advertisement
Guest User

Untitled

a guest
Aug 9th, 2019
637
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. //- -----------------------------------------------------------------------------------------------------------------------
  2. // AskSin++
  3. // 2017-12-14 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 <Blind.h>
  15.  
  16. // we use a Pro Mini
  17. // Arduino pin for the LED
  18. #define LED_PIN 4
  19. // Arduino pin for the config button
  20. // B0 == PIN 8 on Pro Mini
  21. #define CONFIG_BUTTON_PIN 8
  22.  
  23. #define ON_RELAY_PIN 14
  24. #define DIR_RELAY_PIN 15
  25.  
  26. #define ON_RELAY2_PIN 16
  27. #define DIR_RELAY2_PIN 17
  28.  
  29. #define UP_BUTTON_PIN 7
  30. #define DOWN_BUTTON_PIN 6
  31.  
  32. #define UP_BUTTON2_PIN 5
  33. #define DOWN_BUTTON2_PIN 3
  34.  
  35. // number of available peers per channel
  36. #define PEERS_PER_CHANNEL 6
  37.  
  38. // all library classes are placed in the namespace 'as'
  39. using namespace as;
  40.  
  41. // define all device properties
  42. const struct DeviceInfo PROGMEM devinfo = {
  43. {0x00, 0x05, 0xaf}, // Device ID
  44. "AskSinPP1A", // Device Serial
  45. {0x00, 0x05}, // Device Model
  46. 0x24, // Firmware Version
  47. as::DeviceType::BlindActuator, // Device Type
  48. {0x01, 0x00} // Info Bytes
  49. };
  50.  
  51. /**
  52. Configure the used hardware
  53. */
  54. typedef AvrSPI<10, 11, 12, 13> RadioSPI;
  55. typedef AskSin<StatusLed<LED_PIN>, NoBattery, Radio<RadioSPI, 2> > Hal;
  56.  
  57. DEFREGISTER(BlindReg0, MASTERID_REGS, DREG_INTKEY, DREG_CONFBUTTONTIME, DREG_LOCALRESETDISABLE)
  58.  
  59. class BlindList0 : public RegList0<BlindReg0> {
  60. public:
  61. BlindList0 (uint16_t addr) : RegList0<BlindReg0>(addr) {}
  62. void defaults () {
  63. clear();
  64. // intKeyVisible(false);
  65. confButtonTime(0xff);
  66. // localResetDisable(false);
  67. }
  68. };
  69.  
  70.  
  71. class BlChannel : public ActorChannel<Hal, BlindList1, BlindList3, PEERS_PER_CHANNEL, BlindList0, BlindStateMachine> {
  72. private:
  73. uint8_t on_relay_pin;
  74. uint8_t dir_relay_pin;
  75. public:
  76. typedef ActorChannel<Hal, BlindList1, BlindList3, PEERS_PER_CHANNEL, BlindList0, BlindStateMachine> BaseChannel;
  77.  
  78. BlChannel () : on_relay_pin(0), dir_relay_pin(0) {}
  79. virtual ~BlChannel () {}
  80.  
  81. virtual void switchState(uint8_t oldstate, uint8_t newstate, uint32_t stateDelay) {
  82. BaseChannel::switchState(oldstate, newstate, stateDelay);
  83. if ( newstate == AS_CM_JT_RAMPON && stateDelay > 0 ) {
  84. motorUp();
  85. }
  86. else if ( newstate == AS_CM_JT_RAMPOFF && stateDelay > 0 ) {
  87. motorDown();
  88. }
  89. else {
  90. motorStop();
  91. }
  92. }
  93.  
  94. void motorUp () {
  95. digitalWrite(dir_relay_pin, HIGH);
  96. digitalWrite(on_relay_pin, HIGH);
  97. }
  98.  
  99. void motorDown () {
  100. digitalWrite(dir_relay_pin, LOW);
  101. digitalWrite(on_relay_pin, HIGH);
  102. }
  103.  
  104. void motorStop () {
  105. digitalWrite(dir_relay_pin, LOW);
  106. digitalWrite(on_relay_pin, LOW);
  107. }
  108.  
  109. void init (uint8_t op, uint8_t dp) {
  110. on_relay_pin = op;
  111. dir_relay_pin = dp;
  112. pinMode(on_relay_pin, OUTPUT);
  113. pinMode(dir_relay_pin, OUTPUT);
  114. motorStop();
  115. BaseChannel::init();
  116. }
  117. };
  118.  
  119.  
  120. // setup the device with channel type and number of channels
  121. typedef MultiChannelDevice<Hal, BlChannel, 2, BlindList0> BlindType;
  122.  
  123. Hal hal;
  124. BlindType sdev(devinfo, 0x20);
  125. ConfigButton<BlindType> cfgBtn(sdev);
  126. InternalButton<BlindType> btnup(sdev, 1);
  127. InternalButton<BlindType> btndown(sdev, 2);
  128. InternalButton<BlindType> btnup2(sdev, 3);
  129. InternalButton<BlindType> btndown2(sdev, 4);
  130.  
  131. void initPeerings (bool first) {
  132. // create internal peerings - CCU2 needs this
  133. if ( first == true ) {
  134. HMID devid;
  135. sdev.getDeviceID(devid);
  136. Peer p1(devid, 1);
  137. Peer p2(devid, 2);
  138. Peer p3(devid, 3);
  139. Peer p4(devid, 4);
  140. sdev.channel(1).peer(p1, p2);
  141. sdev.channel(2).peer(p3, p4);
  142. }
  143. }
  144.  
  145. void setup () {
  146. DINIT(57600, ASKSIN_PLUS_PLUS_IDENTIFIER);
  147. //storage().setByte(0,0);
  148. bool first = sdev.init(hal);
  149. sdev.channel(1).init(ON_RELAY_PIN, DIR_RELAY_PIN);
  150. sdev.channel(2).init(ON_RELAY2_PIN, DIR_RELAY2_PIN);
  151.  
  152. buttonISR(cfgBtn, CONFIG_BUTTON_PIN);
  153. buttonISR(btnup, UP_BUTTON_PIN);
  154. buttonISR(btndown, DOWN_BUTTON_PIN);
  155. buttonISR(btnup2, UP_BUTTON2_PIN);
  156. buttonISR(btndown2, DOWN_BUTTON2_PIN);
  157.  
  158. initPeerings(first);
  159. sdev.initDone();
  160. }
  161.  
  162. void loop() {
  163. bool worked = hal.runready();
  164. bool poll = sdev.pollRadio();
  165. if ( worked == false && poll == false ) {
  166. hal.activity.savePower<Idle<> >(hal);
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement