jp112

Custom SwitchChannel

Sep 6th, 2021 (edited)
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class MySwitchChannel : public ActorChannel<Hal,SwitchList1,SwitchList3,PEERS_PER_CHANNEL,SwList0,SwitchStateMachine> {
  2.   class PinControl : public Alarm {
  3.   private:
  4.     uint8_t pin_long;
  5.     uint8_t pin_short;
  6.     bool first;
  7.   public:
  8.     PinControl () : Alarm(0), pin_long(0), pin_short(0), first(true) {}
  9.     virtual ~PinControl () {}
  10.  
  11.     void initPins(uint8_t p_long,uint8_t p_short) {
  12.       pin_long=p_long;
  13.       pin_short=p_short;
  14.       ArduinoPins::setOutput(pin_long);
  15.       ArduinoPins::setOutput(pin_short);
  16.     }
  17.  
  18.     void start() {
  19.       ArduinoPins::setLow(pin_long);
  20.       ArduinoPins::setLow(pin_short);
  21.       sysclock.cancel(*this);
  22.       first = true;
  23.       set(millis2ticks(1000));
  24.       ArduinoPins::setHigh(pin_long);
  25.       sysclock.add(*this);
  26.     }
  27.  
  28.     virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) {
  29.       if (first == true) {
  30.         ArduinoPins::setLow(pin_long);
  31.         ArduinoPins::setHigh(pin_short);
  32.         set(millis2ticks(500));
  33.         clock.add(*this);
  34.       } else {
  35.         ArduinoPins::setLow(pin_short);
  36.       }
  37.       first = false;
  38.     }
  39.   };
  40.  
  41.   PinControl pincontrol;
  42.  
  43. protected:
  44.   typedef ActorChannel<Hal,SwitchList1,SwitchList3,PEERS_PER_CHANNEL,SwList0,SwitchStateMachine> BaseChannel;
  45. public:
  46.   MySwitchChannel () : BaseChannel() {}
  47.   virtual ~MySwitchChannel() {}
  48.  
  49.   void init (uint8_t p_long,uint8_t p_short) {
  50.     pincontrol.initPins(p_long, p_short);
  51.     typename BaseChannel::List1 l1 = BaseChannel::getList1();
  52.     this->set(l1.powerUpAction() == true ? 200 : 0, 0, 0xffff );
  53.     this->changed(true);
  54.   }
  55.  
  56.   uint8_t flags () const {
  57.     return BaseChannel::flags();
  58.   }
  59.  
  60.   virtual void switchState(__attribute__((unused)) uint8_t oldstate,uint8_t newstate,__attribute__((unused)) uint32_t delay) {
  61.     if (( newstate == AS_CM_JT_ON ) || ( newstate == AS_CM_JT_OFF )) {
  62.       pincontrol.start();
  63.     }
  64.     this->changed(true);
  65.   }
  66. };
Advertisement
Add Comment
Please, Sign In to add comment