Skip to content

Commit

Permalink
add remote classes to library
Browse files Browse the repository at this point in the history
some more simplifications
  • Loading branch information
pa-pa committed Apr 12, 2017
1 parent 24ba98f commit f7ea371
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 240 deletions.
16 changes: 16 additions & 0 deletions AskSinPP.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ class AskSin {
RadioType radio;
Activity activity;

void init () {
led.init();
radio.init();
radio.enable();
// start the system timer
sysclock.init();
// signal start to user
led.set(LedStates::welcome);
}

bool runready () {
return sysclock.runready();
}

void sendPeer () {}

static void pgm_read(uint8_t* dest,uint16_t adr,uint8_t size) {
for( int i=0; i<size; ++i, ++dest ) {
*dest = pgm_read_byte(adr + i);
Expand Down
4 changes: 4 additions & 0 deletions Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ class ConfigToggleButton : public StateButton<HIGH,LOW,INPUT_PULLUP> {
}
};

#define buttonISR(btn,pin) class btn##ISRHandler { public: static void isr () { btn.check(); } }; \
btn.init(pin); \
enableInterrupt(pin,btn##ISRHandler::isr,CHANGE);

}

#endif
2 changes: 2 additions & 0 deletions Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ class Device {
if( sendtopeer == false ) {
send(msg,getMasterID());
}
// signal that we have send to peer
hal->sendPeer();
}

void writeList (const GenericList& list,const uint8_t* data,uint8_t length) {
Expand Down
8 changes: 8 additions & 0 deletions Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,14 @@ class RemoteEventMsg : public Message {
protected:
RemoteEventMsg() {}
public:
void init(uint8_t msgcnt,uint8_t ch,uint8_t counter,bool lg,bool lowbat) {
uint8_t flags = lg ? 0x40 : 0x00;
if( lowbat == true ) {
flags |= 0x80; // low battery
}
Message::init(0xb,msgcnt,0x40, Message::BIDI,(ch & 0x3f) | flags,counter);
}

Peer peer () const { return Peer(from(),command() & 0x3f); }
uint8_t counter () const { return subcommand(); }
bool isLong () const { return (command() & 0x40) == 0x40; }
Expand Down
128 changes: 128 additions & 0 deletions Remote.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2017-04-12 papa Creative Commons - http:https://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------

#ifndef __REMOTE_H__
#define __REMOTE_H__

#include "Channel.h"
#include "ChannelList.h"
#include "Button.h"
#include "Message.h"

namespace as {

class RemoteList1Data {
public:
uint8_t Unused1 : 4; // 0x04
uint8_t LongPressTime : 4; // 0x04
uint8_t AesActive : 1; // 0x08, s:0, e:1
uint8_t DoublePressTime : 4; // 0x09
uint8_t Unused2 : 4; // 0x09

static uint8_t getOffset(uint8_t reg) {
switch (reg) {
case 0x04: return 0;
case 0x08: return 1;
case 0x09: return 2;
default: break;
}
return 0xff;
}

static uint8_t getRegister(uint8_t offset) {
switch (offset) {
case 0: return 0x04;
case 1: return 0x08;
case 2: return 0x09;
default: break;
}
return 0xff;
}
};

class RemoteList1 : public ChannelList<RemoteList1Data> {
public:
RemoteList1(uint16_t a) : ChannelList(a) {}

uint8_t longPressTime () const { return getByte(0,0xf0,4); }
bool longPressTime (uint8_t value) const { return setByte(0,value,0xf0,4); }

bool aesActive () const { return isBitSet(1,0x01); }
bool aesActive (bool s) const { return setBit(1,0x01,s); }

uint8_t doublePressTime () const { return getByte(2,0x0f,0); }
bool doublePressTime (uint8_t value) const { return setByte(2,value,0x0f,0); }


void defaults () {
longPressTime(1);
aesActive(false);
doublePressTime(0);
}
};

template<class HALTYPE,int PEERCOUNT>
class RemoteChannel : public Channel<HALTYPE,RemoteList1,EmptyList,List4,PEERCOUNT>, public Button {

private:
uint8_t repeatcnt;
volatile bool isr;

public:

typedef Channel<HALTYPE,RemoteList1,EmptyList,List4,PEERCOUNT> BaseChannel;

RemoteChannel () : BaseChannel(), repeatcnt(0), isr(false) {}
virtual ~RemoteChannel () {}

Button& button () { return *(Button*)this; }

uint8_t status () const {
return 0;
}

uint8_t flags () const {
return 0;
}

virtual void state(uint8_t s) {
DHEX(BaseChannel::number());
Button::state(s);
if( s == released ) {
RemoteEventMsg& msg = (RemoteEventMsg&)BaseChannel::device().message();
msg.init(BaseChannel::device().nextcount(),BaseChannel::number(),repeatcnt++,false,BaseChannel::device().battery().low());
BaseChannel::device().sendPeerEvent(msg,*this);
}
else if( s == longpressed ) {
RemoteEventMsg& msg = (RemoteEventMsg&)BaseChannel::device().message();
msg.init(BaseChannel::device().nextcount(),BaseChannel::number(),repeatcnt,true,BaseChannel::device().battery().low());
BaseChannel::device().sendPeerEvent(msg,*this);
}
else if( s == longreleased ) {
repeatcnt++;
}
}

void pinchanged () {
isr = true;
}

bool checkpin () {
bool result = isr;
if( isr == true ) {
isr = false;
Button::check();
}
return result;
}
};

#define remoteISR(device,chan,pin) class device##chan##ISRHandler { public: static void isr () { device.channel(chan).pinchanged(); } }; \
device.channel(chan).button().init(pin); \
enableInterrupt(pin,device##chan##ISRHandler::isr,CHANGE);

}

#endif
16 changes: 4 additions & 12 deletions examples/HM-ES-TX-WM/HM-ES-TX-WM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ void meterISR () {
}

ConfigButton<MeterType> cfgBtn(sdev);
void cfgBtnISR () { cfgBtn.check(); }

void setup () {
DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER);
Expand All @@ -396,11 +395,7 @@ void setup () {
sdev.firstinit();
}

hal.led.init();

cfgBtn.init(CONFIG_BUTTON_PIN);
enableInterrupt(CONFIG_BUTTON_PIN,cfgBtnISR,CHANGE);
hal.radio.init();
buttonISR(cfgBtn,CONFIG_BUTTON_PIN);

#ifdef USE_OTA_BOOTLOADER
sdev.init(hal,OTA_HMID_START,OTA_SERIAL_START);
Expand All @@ -413,22 +408,19 @@ void setup () {
sdev.setSubType(DeviceType::PowerMeter);
sdev.setInfo(0x03,0x01,0x00);

hal.radio.enable();
sysclock.init();

gasISR.attach();
hal.init();

hal.led.set(LedStates::welcome);
// set low voltage to 2.2V
// measure battery every 1h
battery.init(22,seconds2ticks(60UL*60),sysclock);

gasISR.attach();
// add channel 1 to timer to send event
sysclock.add(sdev.channel(1));
}

void loop() {
bool worked = sysclock.runready();
bool worked = hal.runready();
bool poll = sdev.pollRadio();
if( worked == false && poll == false ) {
hal.activity.savePower<Sleep<> >(hal);
Expand Down
15 changes: 3 additions & 12 deletions examples/HM-LC-Dim1PWM-CV/HM-LC-Dim1PWM-CV.ino
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ typedef DimmerDevice<Hal,DimmerChannel<Hal,PEERS_PER_CHANNEL>,3> DimmerType;
DimmerType sdev(0x20,DIMMER_PIN);

ConfigToggleButton<DimmerType> cfgBtn(sdev);
void cfgBtnISR () { cfgBtn.check(); }

void setup () {
DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER);
Expand All @@ -72,11 +71,7 @@ void setup () {
sdev.firstinit();
}

hal.led.init();

cfgBtn.init(CONFIG_BUTTON_PIN);
enableInterrupt(CONFIG_BUTTON_PIN,cfgBtnISR,CHANGE);
hal.radio.init();
buttonISR(cfgBtn,CONFIG_BUTTON_PIN);

#ifdef USE_OTA_BOOTLOADER
sdev.init(hal,OTA_HMID_START,OTA_SERIAL_START);
Expand All @@ -89,17 +84,13 @@ void setup () {
sdev.setSubType(DeviceType::Dimmer);
sdev.setInfo(0x41,0x01,0x00);

hal.radio.enable();

sysclock.init();

hal.led.set(LedStates::welcome);
hal.init();

// TODO - random delay
}

void loop() {
bool worked = sysclock.runready();
bool worked = hal.runready();
bool poll = sdev.pollRadio();
if( worked == false && poll == false ) {
hal.activity.savePower<Idle<true> >(hal);
Expand Down
15 changes: 3 additions & 12 deletions examples/HM-LC-SWX-SM/HM-LC-SWX-SM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ typedef MultiChannelDevice<Hal,SwitchChannel<Hal,PEERS_PER_CHANNEL>,RELAY_COUNT>
SwitchType sdev(0x20);

ConfigToggleButton<SwitchType> cfgBtn(sdev);
void cfgBtnISR () { cfgBtn.check(); }

// if A0 and A1 connected
// we use LOW for ON and HIGH for OFF
Expand All @@ -116,11 +115,7 @@ void setup () {
sdev.channel(i).lowactive(low);
}

hal.led.init();

cfgBtn.init(CONFIG_BUTTON_PIN);
enableInterrupt(CONFIG_BUTTON_PIN,cfgBtnISR,CHANGE);
hal.radio.init();
buttonISR(cfgBtn,CONFIG_BUTTON_PIN);

#ifdef USE_OTA_BOOTLOADER
sdev.init(hal,OTA_HMID_START,OTA_SERIAL_START);
Expand All @@ -144,17 +139,13 @@ void setup () {
sdev.setSubType(DeviceType::Switch);
sdev.setInfo(0x41,0x01,0x00);

hal.radio.enable();

sysclock.init();

hal.led.set(LedStates::welcome);
hal.init();

// TODO - random delay
}

void loop() {
bool worked = sysclock.runready();
bool worked = hal.runready();
bool poll = sdev.pollRadio();
if( worked == false && poll == false ) {
hal.activity.savePower<Idle<>>(hal);
Expand Down
Loading

0 comments on commit f7ea371

Please sign in to comment.