Skip to content

Commit

Permalink
allow internal messages without sending
Browse files Browse the repository at this point in the history
  • Loading branch information
pa-pa committed Sep 29, 2017
1 parent 2180f28 commit 4c388da
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 124 deletions.
60 changes: 43 additions & 17 deletions AskSinPP.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef __ASKSINPP_h__
#define __ASKSINPP_h__

#define ASKSIN_PLUS_PLUS_VERSION "2.1.0"
#define ASKSIN_PLUS_PLUS_VERSION "2.1.1"

#define ASKSIN_PLUS_PLUS_IDENTIFIER F("AskSin++ V" ASKSIN_PLUS_PLUS_VERSION " (" __DATE__ " " __TIME__ ")")

Expand Down Expand Up @@ -43,8 +43,49 @@

namespace as {

class AskSinBase {

public:

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);
}
}

static uint16_t crc16 (uint16_t crc,uint8_t d) {
crc ^= d;
for( uint8_t i = 8; i != 0; --i ) {
crc = (crc >> 1) ^ ((crc & 1) ? 0xA001 : 0 );
}
return crc;
}

// get timer count in ticks
static uint32_t byteTimeCvt(uint8_t tTime) {
if( tTime == 0xff ) return 0xffffffff;
const uint16_t c[8] = {1,10,50,100,600,3000,6000,36000};
return decis2ticks( (uint32_t)(tTime & 0x1F) * c[tTime >> 5] );
}

// get timer count in ticks
static uint32_t intTimeCvt(uint16_t iTime) {
if (iTime == 0x00) return 0x00;
if (iTime == 0xffff) return 0xffffffff;

uint8_t tByte;
if ((iTime & 0x1F) != 0) {
tByte = 2;
for (uint8_t i = 1; i < (iTime & 0x1F); i++) tByte *= 2;
} else tByte = 1;

return decis2ticks( (uint32_t)tByte*(iTime>>5) );
}

};

template <class StatusLed,class Battery,class Radio>
class AskSin {
class AskSin : public AskSinBase {
public:
typedef StatusLed LedType;
typedef Battery BatteryType;
Expand Down Expand Up @@ -76,21 +117,6 @@ class AskSin {
void waitTimeout(uint16_t millis) {
radio.waitTimeout(millis);
}

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);
}
}

static uint16_t crc16 (uint16_t crc,uint8_t d) {
crc ^= d;
for( uint8_t i = 8; i != 0; --i ) {
crc = (crc >> 1) ^ ((crc & 1) ? 0xA001 : 0 );
}
return crc;
}

};

}
Expand Down
35 changes: 30 additions & 5 deletions Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ class Device {
return memcmp(serial,tmp,10) == 0;
}

bool isDeviceID(const HMID& id) {
HMID me;
getDeviceID(me);
return id == me;
}

void getDeviceModel (uint8_t* model) {
#ifdef USE_OTA_BOOTLOADER
HalType::pgm_read(model,OTA_MODEL_START,2);
Expand Down Expand Up @@ -212,10 +218,10 @@ class Device {
uint8_t maxsend = 6;
led().set(LedStates::send);
while( result == false && maxsend > 0 ) {
result = radio().write(msg,msg.burstRequired());
DPRINT(F("<- "));
msg.dump();
maxsend--;
result = radio().write(msg,msg.burstRequired());
if( result == true && msg.ackRequired() == true && to.valid() == true ) {
Message response;
if( (result=waitResponse(msg,response,60)) ) { // 600ms
Expand Down Expand Up @@ -368,10 +374,26 @@ class Device {
for( int i=0; i<ch.peers(); ++i ){
Peer p = ch.peer(i);
if( p.valid() == true ) {
typename ChannelType::List4 l4 = ch.getList4(p);
msg.burstRequired( l4.burst() );
send(msg,p);
sendtopeer = true;
if( isDeviceID(p) == true ) {
// we send to ourself - no ack needed
getDeviceID(msg.from());
msg.to(msg.from());
if( msg.ackRequired() == true ) {
msg.clearAck();
this->process(msg);
msg.setAck();
}
else {
this->process(msg);
}
}
else {
// check if burst needed for peer
typename ChannelType::List4 l4 = ch.getList4(p);
msg.burstRequired( l4.burst() );
send(msg,p);
sendtopeer = true;
}
}
}
// if we have no peer - send to master/broadcast
Expand Down Expand Up @@ -424,6 +446,9 @@ class Device {
}

bool requestSignature(const Message& msg) {
if( isDeviceID(msg.from()) == true ) {
return true;
}
AesChallengeMsg signmsg;
signmsg.init(msg,kstore.getIndex());
kstore.challengeKey(signmsg.challenge(),kstore.getIndex());
Expand Down
27 changes: 3 additions & 24 deletions Dimmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ class DimmerStateMachine {
case AS_CM_JT_RAMPOFF: value = lst.rampOffTime(); break;
case AS_CM_JT_OFF: value = lst.offTime(); break;
}
return byteTimeCvt(value);
return AskSinBase::byteTimeCvt(value);
}

uint32_t getDefaultDelay(uint8_t stat) const {
Expand All @@ -685,27 +685,6 @@ class DimmerStateMachine {

bool delayActive () const { return sysclock.get(alarm) > 0; }

// get timer count in ticks
static uint32_t byteTimeCvt(uint8_t tTime) {
if( tTime == 0xff ) return 0xffffffff;
const uint16_t c[8] = {1,10,50,100,600,3000,6000,36000};
return decis2ticks( (uint32_t)(tTime & 0x1F) * c[tTime >> 5] );
}

// get timer count in ticks
static uint32_t intTimeCvt(uint16_t iTime) {
if (iTime == 0x00) return 0x00;
if (iTime == 0xffff) return 0xffffffff;

uint8_t tByte;
if ((iTime & 0x1F) != 0) {
tByte = 2;
for (uint8_t i = 1; i < (iTime & 0x1F); i++) tByte *= 2;
} else tByte = 1;

return decis2ticks( (uint32_t)tByte*(iTime>>5) );
}

void dimUp (const DimmerPeerList& lst) {
uint8_t dx = lst.dimStep();
uint8_t newlevel = level+dx;
Expand Down Expand Up @@ -797,11 +776,11 @@ class DimmerStateMachine {
if( ramp==0 ) {
alarm.destlevel=level;
updateLevel(level);
setState(level==0 ? AS_CM_JT_OFF : AS_CM_JT_ON, intTimeCvt(delay));
setState(level==0 ? AS_CM_JT_OFF : AS_CM_JT_ON, AskSinBase::intTimeCvt(delay));
}
else {
sysclock.cancel(alarm);
alarm.init(intTimeCvt(ramp), level, intTimeCvt(delay));
alarm.init(AskSinBase::intTimeCvt(ramp), level, AskSinBase::intTimeCvt(delay));
sysclock.add(alarm);
}
}
Expand Down
Loading

0 comments on commit 4c388da

Please sign in to comment.