Skip to content

Commit

Permalink
- move channel count to Device class
Browse files Browse the repository at this point in the history
- start SoftPWM
  • Loading branch information
pa-pa committed Sep 18, 2017
2 parents c7523f9 + c6e165c commit 2834397
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 95 deletions.
26 changes: 22 additions & 4 deletions Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct DeviceInfo {
uint8_t DeviceModel[2];
uint8_t Firmware;
uint8_t DeviceType;
uint8_t DeviceInfo[3];
uint8_t DeviceInfo[2];
};


Expand All @@ -79,9 +79,10 @@ class Device {
KeyStore kstore;

const DeviceInfo& info;
uint8_t numChannels;

public:
Device (const DeviceInfo& i,uint16_t addr,List0& l) : hal(0), list0(l), msgcount(0), lastmsg(0), kstore(addr), info(i) {
Device (const DeviceInfo& i,uint16_t addr,List0& l,uint8_t ch) : hal(0), list0(l), msgcount(0), lastmsg(0), kstore(addr), info(i), numChannels(ch) {
// TODO init seed
}
virtual ~Device () {}
Expand All @@ -95,6 +96,19 @@ class Device {

Message& message () { return msg; }

void channels (uint8_t num) {
numChannels = num;
}

uint8_t channels () const {
return numChannels;
}

bool hasChannel (uint8_t number) const {
return number != 0 && number <= channels();
}


bool isRepeat(const Message& m) {
if( m.isRepeated() && lastdev == m.from() && lastmsg == m.count() ) {
return true;
Expand Down Expand Up @@ -143,7 +157,9 @@ class Device {
}

bool isDeviceSerial (const uint8_t* serial) {
return memcmp_P(serial,info.Serial,10);
uint8_t tmp[10];
getDeviceSerial(tmp);
return memcmp(serial,tmp,10) == 0;
}

void getDeviceModel (uint8_t* model) {
Expand All @@ -155,7 +171,9 @@ class Device {
}

void getDeviceInfo (uint8_t* di) {
memcpy_P(di,info.DeviceInfo,sizeof(info.DeviceInfo));
// first byte is number of channels
*di = this->channels();
memcpy_P(di+1,info.DeviceInfo,sizeof(info.DeviceInfo));
}

HMID getMasterID () {
Expand Down
6 changes: 3 additions & 3 deletions Led.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Led : public Alarm, public LedStates {
}
}

void ledOn (uint8_t ticks) {
void ledOn (uint32_t ticks) {
if( active() == false && ticks > 0 ) {
current.length = 2;
current.duration = 1;
Expand Down Expand Up @@ -138,7 +138,7 @@ class StatusLed : public LedStates {

void init () { led1.init(LEDPIN1); }
bool active () const { return led1.active(); }
void ledOn (uint8_t ticks) { led1.ledOn(ticks); }
void ledOn (uint32_t ticks) { led1.ledOn(ticks); }
void set(Mode stat) { led1.set(stat,single); }
void ledOn () { led1.ledOn(); }
void ledOff () { led1.ledOff(); }
Expand All @@ -154,7 +154,7 @@ class DualStatusLed : public LedStates {
DualStatusLed () {}
void init () { led1.init(LEDPIN1); led2.init(LEDPIN2); }
bool active () const { return led1.active() || led2.active(); }
void ledOn (uint8_t ticks) { led1.ledOn(ticks); led2.ledOn(ticks); }
void ledOn (uint32_t ticks) { led1.ledOn(ticks); led2.ledOn(ticks); }
void set(Mode stat) { led1.set(stat,dual1); led2.set(stat,dual2); }
void ledOn () { led1.ledOn(); led2.ledOn(); }
void ledOff () { led1.ledOff(); led2.ledOff(); }
Expand Down
56 changes: 23 additions & 33 deletions MultiChannelDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ class ChannelDevice : public Device<HalType> {

List0Type list0;
ChannelType* devchannels[ChannelCount];
uint8_t numChannels;
uint8_t cfgChannel;
GenericList cfgList;

public:

typedef Device<HalType> DeviceType;

ChannelDevice (const DeviceInfo& i,uint16_t addr) : Device<HalType>(i,addr,list0), list0(addr + this->keystore().size()), numChannels(ChannelCount), cfgChannel(0xff) {}
ChannelDevice (const DeviceInfo& i,uint16_t addr) : Device<HalType>(i,addr,list0,ChannelCount), list0(addr + this->keystore().size()), cfgChannel(0xff) {}

virtual ~ChannelDevice () {}

Expand All @@ -46,14 +45,23 @@ class ChannelDevice : public Device<HalType> {

void layoutChannels () {
uint16_t addr = list0.address() + list0.size();
for( uint8_t i=0; i<channels(); ++i ) {
for( uint8_t i=0; i<this->channels(); ++i ) {
devchannels[i]->setup(this,i+1,addr);
addr += devchannels[i]->size();
}
}

void channels (uint8_t num) {
DeviceType::channels(min(num,ChannelCount));
}

uint8_t channels () const {
return DeviceType::channels();
}


void dumpSize () {
ChannelType& ch = channel(channels());
ChannelType& ch = channel(this->channels());
DPRINT("Address Space: ");DDEC(this->keystore().address());DPRINT(" - ");DDECLN((uint16_t)(ch.address() + ch.size()));
}

Expand All @@ -66,7 +74,7 @@ class ChannelDevice : public Device<HalType> {
crc = HalType::crc16(crc,list0.getRegister(i));
}
// add number of channels
for( uint8_t c=1; c<=channels(); ++c ) {
for( uint8_t c=1; c<=this->channels(); ++c ) {
ChannelType& ch = channel(c);
// add register list 1
GenericList l = ch.getList1();
Expand All @@ -93,24 +101,6 @@ class ChannelDevice : public Device<HalType> {
return list0;
}

void getDeviceInfo (uint8_t* info) {
DeviceType::getDeviceInfo(info);
// patch real channel count into device info
*info = channels();
}

void channels (uint8_t num) {
numChannels = min(num,ChannelCount);
}

uint8_t channels () const {
return numChannels;
}

bool hasChannel (uint8_t number) const {
return number != 0 && number <= channels();
}

void init (HalType& hal) {
layoutChannels();
dumpSize();
Expand All @@ -130,7 +120,7 @@ class ChannelDevice : public Device<HalType> {
void firstinit () {
this->keystore().defaults(); // init aes key infrastructure
list0.defaults();
for( uint8_t i=0; i<channels(); ++i ) {
for( uint8_t i=0; i<this->channels(); ++i ) {
devchannels[i]->firstinit();
}
}
Expand Down Expand Up @@ -161,7 +151,7 @@ class ChannelDevice : public Device<HalType> {

bool pollRadio () {
bool worked = DeviceType::pollRadio();
for( uint8_t i=1; i<=channels(); ++i ) {
for( uint8_t i=1; i<=this->channels(); ++i ) {
ChannelType& ch = channel(i);
if( ch.changed() == true ) {
this->sendInfoActuatorStatus(this->getMasterID(),this->nextcount(),ch);
Expand All @@ -175,7 +165,7 @@ class ChannelDevice : public Device<HalType> {
if( getList0().aesActive() == true ) {
return true;
}
for( uint8_t i=1; i<=channels(); ++i) {
for( uint8_t i=1; i<=this->channels(); ++i) {
if( channel(i).aesActive() == true ) {
return true;
}
Expand All @@ -194,7 +184,7 @@ class ChannelDevice : public Device<HalType> {

bool validSignature(uint8_t ch,Message& msg) {
#ifdef USE_AES
if( (ch==0 && aesActive()) || (hasChannel(ch)==true && channel(ch).aesActive()==true) ) {
if( (ch==0 && aesActive()) || (this->hasChannel(ch)==true && channel(ch).aesActive()==true) ) {
return this->requestSignature(msg);
}
#endif
Expand Down Expand Up @@ -225,7 +215,7 @@ class ChannelDevice : public Device<HalType> {
else if ( msubc == AS_CONFIG_PEER_ADD ) {
const ConfigPeerAddMsg& pm = msg.configPeerAdd();
bool success = false;
if( hasChannel(pm.channel()) == true ) {
if( this->hasChannel(pm.channel()) == true ) {
if( validSignature(pm.channel(),msg) == true ) {
ChannelType& ch = channel(pm.channel());
if( pm.peers() == 1 ) {
Expand All @@ -248,7 +238,7 @@ class ChannelDevice : public Device<HalType> {
else if ( msubc == AS_CONFIG_PEER_REMOVE ) {
const ConfigPeerRemoveMsg& pm = msg.configPeerRemove();
bool success = false;
if( hasChannel(pm.channel()) == true ) {
if( this->hasChannel(pm.channel()) == true ) {
if( validSignature(pm.channel(),msg) == true ) {
ChannelType& ch = channel(pm.channel());
success = ch.deletepeer(pm.peer1());
Expand All @@ -268,7 +258,7 @@ class ChannelDevice : public Device<HalType> {
// CONFIG_PEER_LIST_REQ
else if( msubc == AS_CONFIG_PEER_LIST_REQ ) {
const ConfigPeerListReqMsg& pm = msg.configPeerListReq();
if( hasChannel(pm.channel()) == true ) {
if( this->hasChannel(pm.channel()) == true ) {
this->sendInfoPeerList(msg.from(),msg.count(),channel(pm.channel()));
}
}
Expand Down Expand Up @@ -353,7 +343,7 @@ class ChannelDevice : public Device<HalType> {
else {
bool ack=false;
const ActionMsg& pm = msg.action();
if( hasChannel(pm.channel())==true ) {
if( this->hasChannel(pm.channel())==true ) {
ChannelType& ch = channel(pm.channel());
if( validSignature(pm.channel(),msg)==true ) {
switch( mcomm ) {
Expand Down Expand Up @@ -429,7 +419,7 @@ class ChannelDevice : public Device<HalType> {
}

uint8_t channelForPeer (const Peer& p) {
for( uint8_t x=1; x<=channels(); ++x ) {
for( uint8_t x=1; x<=this->channels(); ++x ) {
ChannelType& ch = channel(x);
for( uint8_t y=0; y<ch.peers(); ++y ) {
if( ch.peer(y) == p ) {
Expand All @@ -443,7 +433,7 @@ class ChannelDevice : public Device<HalType> {
GenericList findList(uint8_t ch,const Peer& peer,uint8_t numlist) {
if (numlist == 0) {
return list0;
} else if (hasChannel(ch) == true) {
} else if (this->hasChannel(ch) == true) {
ChannelType& c = channel(ch);
if (numlist == 1) {
return c.getList1();
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Required Arduino libraries:
- EnableInterrupt - https://github.com/GreyGnome/EnableInterrupt
- Low-Power - https://github.com/rocketscream/Low-Power.git

Required for different sensor examples:
- Sensirion - https://github.com/spease/Sensirion.git

## Enable AES Support

To enable the AES signature support **USE_AES** needs to be
Expand Down
6 changes: 4 additions & 2 deletions bootloader/avr/prepota.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash

HEXFILE=$1
QUAL=`date +%Y%m%d%H%M`
EQ3=`basename $1 .hex`_$QUAL.eq3

crc=0xFFFF
function crc16 {
Expand Down Expand Up @@ -37,7 +39,7 @@ function toout {
done
}


(
OUT=0

while read LINE
Expand All @@ -63,4 +65,4 @@ for (( i=$OUT; i < 16#6FFE; i++ )); do toout "FF" 1; done
crc16 0
crc16 0
printf "%02X%02X" $(( $crc & 0xff )) $(( $crc >> 8 ))

) > $EQ3
2 changes: 1 addition & 1 deletion examples/HM-ES-TX-WM/HM-ES-TX-WM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const struct DeviceInfo PROGMEM devinfo = {
{0x00,0xde}, // Device Model
0x11, // Firmware Version
as::DeviceType::PowerMeter, // Device Type
{0x02,0x01,0x00} // Info Bytes
{0x01,0x00} // Info Bytes
};

/**
Expand Down
2 changes: 1 addition & 1 deletion examples/HM-LC-Dim1PWM-CV/HM-LC-Dim1PWM-CV.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const struct DeviceInfo PROGMEM devinfo = {
{0x00,0x67}, // Device Model
0x25, // Firmware Version
as::DeviceType::Dimmer, // Device Type
{0x03,0x01,0x00} // Info Bytes
{0x01,0x00} // Info Bytes
};

/**
Expand Down
2 changes: 1 addition & 1 deletion examples/HM-LC-SWX-SM/HM-LC-SWX-SM.ino
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const struct DeviceInfo PROGMEM devinfo = {
{HM_LC_SW4_SM}, // Device Model
0x16, // Firmware Version
as::DeviceType::Switch, // Device Type
{0x04,0x01,0x00} // Info Bytes
{0x01,0x00} // Info Bytes
};

/**
Expand Down
2 changes: 1 addition & 1 deletion examples/HM-RC-4/HM-RC-4.ino
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const struct DeviceInfo PROGMEM devinfo = {
{0x00,0x08}, // Device Model
0x11, // Firmware Version
as::DeviceType::Remote, // Device Type
{0x04,0x00,0x00} // Info Bytes
{0x00,0x00} // Info Bytes
};

/**
Expand Down
2 changes: 1 addition & 1 deletion examples/HM-RC-8/HM-RC-8.ino
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const struct DeviceInfo PROGMEM devinfo = {
{0x00,0xda}, // Device Model
0x01, // Firmware Version
as::DeviceType::Remote, // Device Type
{0x08,0x00,0x00} // Info Bytes
{0x00,0x00} // Info Bytes
};

/**
Expand Down
26 changes: 14 additions & 12 deletions examples/HM-RC-P1/HM-RC-P1.ino
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,9 @@
// define this to read the device id, serial and device type from bootloader section
// #define USE_OTA_BOOTLOADER

// define all device properties
#define DEVICE_ID HMID(0x00,0x1a,0x00)
#define DEVICE_SERIAL "HMRC001A00"
#define DEVICE_MODEL 0x00,0x1a
#define DEVICE_FIRMWARE 0x11
#define DEVICE_TYPE DeviceType::Remote
#define DEVICE_INFO 0x01,0x00,0x00

#define EI_NOTEXTERNAL
#include <EnableInterrupt.h>
#include <AskSinPP.h>
#include <TimerOne.h>
#include <LowPower.h>

#include <MultiChannelDevice.h>
Expand All @@ -36,6 +28,16 @@
// all library classes are placed in the namespace 'as'
using namespace as;

// define all device properties
const struct DeviceInfo PROGMEM devinfo = {
{0x00,0x1a,0x00}, // Device ID
"HMRC001A00", // Device Serial
{0x00,0x1a}, // Device Model
0x11, // Firmware Version
as::DeviceType::Remote, // Device Type
{0x00,0x00} // Info Bytes
};

/**
* Configure the used hardware
*/
Expand All @@ -47,8 +49,8 @@ class Hal : public HalType {
// extra clock to count button press events
AlarmClock btncounter;
public:
void init () {
HalType::init();
void init (const HMID& id) {
HalType::init(id);
// get new battery value after 50 key press
battery.init(50,btncounter);
battery.low(22);
Expand All @@ -68,7 +70,7 @@ typedef RemoteChannel<Hal,PEERS_PER_CHANNEL> ChannelType;
typedef MultiChannelDevice<Hal,ChannelType,1> RemoteType;

Hal hal;
RemoteType sdev(0x20);
RemoteType sdev(devinfo,0x20);
ConfigButton<RemoteType> cfgBtn(sdev);

void setup () {
Expand Down
Loading

0 comments on commit 2834397

Please sign in to comment.