Skip to content

Commit

Permalink
use extra HAL class to setup hardware
Browse files Browse the repository at this point in the history
  • Loading branch information
pa-pa committed Feb 16, 2017
1 parent 0866175 commit a3b7192
Show file tree
Hide file tree
Showing 20 changed files with 441 additions and 319 deletions.
1 change: 0 additions & 1 deletion Activity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

namespace as {

Activity activity;

}
24 changes: 13 additions & 11 deletions Activity.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class Idle {
while (!(UCSR0A & (1 << TXC0))); // Wait for the transmission to complete
}

static void powerSave () {
template <class Hal>
static void powerSave (Hal& hal) {
LowPower.idle(SLEEP_FOREVER,ADC_OFF,TIMER2_OFF,TIMER1_ON,TIMER0_OFF,SPI_ON,USART0_ON,TWI_OFF);
}

Expand All @@ -35,7 +36,6 @@ class Idle {
class Sleep : public Idle {
public:
static uint32_t doSleep (uint32_t ticks) {
radio.setIdle();
uint32_t offset = 0;
if( ticks == 0 ) {
LowPower.powerDown(SLEEP_FOREVER,ADC_OFF,BOD_OFF);
Expand All @@ -52,22 +52,24 @@ class Sleep : public Idle {
LowPower.powerDown(SLEEP_500MS,ADC_OFF,BOD_OFF);
offset = millis2ticks(500);
}
radio.wakeup();
return offset;
}

static void powerSave () {
template <class Hal>
static void powerSave (Hal& hal) {
aclock.disable();
uint32_t ticks = aclock.next();
if( aclock.isready() == false ) {
if( ticks == 0 || ticks > millis2ticks(500) ) {
hal.radio.setIdle();
uint32_t offset = doSleep(ticks);
hal.radio.wakeup();
aclock.correct(offset);
aclock.enable();
}
else{
aclock.enable();
Idle::powerSave();
Idle::powerSave(hal);
}
}
else {
Expand Down Expand Up @@ -103,26 +105,26 @@ class Activity : public Alarm {
}
}

template <class Saver>
void savePower () {
template <class Saver,class Hal>
void savePower (Hal& hal) {
if( awake == false ) {
#ifndef NDEBUG
Saver::waitSerial();
#endif
Saver::powerSave();
Saver::powerSave(hal);
}
}

void sleepForever () {
template <class Hal>
void sleepForever (Hal& hal) {
hal.radio.setIdle();
while( true ) {
LowPower.powerDown(SLEEP_FOREVER,ADC_OFF,BOD_OFF);
}
}

};

extern Activity activity;

}

#endif
19 changes: 19 additions & 0 deletions AskSinPP.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,24 @@
#include <Message.h>
#include <Button.h>
#include <Radio.h>
#include <BatterySensor.h>

namespace as {

template <class StatusLed,class Battery,class Radio>
class AskSin {
public:
typedef StatusLed LedType;
typedef Battery BatteryType;
typedef Radio RadioType;

LedType led;
BatteryType battery;
RadioType radio;
Activity activity;

};

}

#endif
21 changes: 15 additions & 6 deletions BatterySensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@

namespace as {


class NoBattery {
public:
uint8_t current () { return 0; }
bool critical () { return false; }
bool low () { return false; }
};


/**
* Use internal bandgap reference to measure battery voltage
*/
Expand Down Expand Up @@ -59,12 +68,12 @@ class BatterySensor : public Alarm {
return m_LastValue < m_LowValue;
}

void init(uint8_t low,uint32_t period) {
void init(uint8_t low,uint32_t period,AlarmClock& clock) {
m_LowValue = low;
m_LastValue = voltage();
m_Period = period;
tick = m_Period;
aclock.add(*this);
clock.add(*this);
}

virtual uint8_t voltage() {
Expand Down Expand Up @@ -102,11 +111,11 @@ class BatterySensorUni : public BatterySensor {
m_SensePin(sens), m_ActivationPin(activation), m_Factor(57) {}
virtual ~BatterySensorUni () {}

void init( uint8_t low,uint32_t period,uint8_t factor=57) {
void init( uint8_t low,uint32_t period,AlarmClock& clock,uint8_t factor=57) {
m_Factor=factor;
pinMode(m_SensePin, INPUT);
pinMode(m_ActivationPin, INPUT);
BatterySensor::init(low,period);
BatterySensor::init(low,period,clock);
}

virtual uint8_t voltage () {
Expand Down Expand Up @@ -142,14 +151,14 @@ class BatterySensorExt : public BatterySensor {
m_SensePin(sens), m_ActivationPin(activation), m_DividerRatio(2), m_RefVoltage(3300) {}
virtual ~BatterySensorExt () {}

void init( uint8_t low,uint32_t period,uint16_t refvolt=3300,uint8_t divider=2) {
void init( uint8_t low,uint32_t period,AlarmClock& clock,uint16_t refvolt=3300,uint8_t divider=2) {
m_DividerRatio=divider;
m_RefVoltage = refvolt;
pinMode(m_SensePin, INPUT);
if (m_ActivationPin < 0xFF) {
pinMode(m_ActivationPin, OUTPUT);
}
BatterySensor::init(low,period);
BatterySensor::init(low,period,clock);
}


Expand Down
11 changes: 6 additions & 5 deletions Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

namespace as {

class Device;
template <class HalType> class Device;
class ActionSetMsg;
class RemoteEventMsg;
class SensorEventMsg;

template<class List1Type,class List3Type,class List4Type,int PeerCount>
template<class HalType,class List1Type,class List3Type,class List4Type,int PeerCount>
class Channel {
Device* dev;
Device<HalType>* dev;
bool change; // the status is changed, we may need to send a status
bool inhi;
uint8_t num ; // channels per device
Expand All @@ -28,11 +28,12 @@ class Channel {
typedef List1Type List1;
typedef List3Type List3;
typedef List4Type List4;
typedef Device<HalType> DeviceType;

public:
Channel () : dev(0), change(false), inhi(false), num(0), addr(0) {}

Device& device () { return *dev; }
DeviceType& device () { return *dev; }

uint8_t number () const { return num; }

Expand All @@ -48,7 +49,7 @@ class Channel {

bool inhibit () const { return inhi; }

void setup(Device* dev,uint8_t number,uint16_t addr) {
void setup(Device<HalType>* dev,uint8_t number,uint16_t addr) {
this->dev = dev;
this->num = number;
this->addr = addr;
Expand Down
Loading

0 comments on commit a3b7192

Please sign in to comment.