Skip to content

Commit

Permalink
optimize code for empty List2 / List3 / List4
Browse files Browse the repository at this point in the history
  • Loading branch information
pa-pa committed Feb 5, 2021
1 parent 9280fd0 commit 5ed4ea8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 37 deletions.
5 changes: 2 additions & 3 deletions AskSinPP.h
Expand Up @@ -228,7 +228,6 @@ class AskSin : public AskSinBase {
BuzzerType buzzer;

void init (const HMID& id) {
srand((unsigned int&)id);
led.init();
buzzer.init();
bool ccinitOK = radio.init();
Expand All @@ -237,8 +236,8 @@ class AskSin : public AskSinBase {
sysclock.init();
// signal start to user
led.set(ccinitOK ? LedStates::welcome : LedStates::failure);
// delay first send by random time
radio.setSendTimeout((rand() % 3500)+1000);
// delay first send by 'random' time
radio.setSendTimeout(id.id2()*20);
}

void initBattery(uint16_t interval,uint8_t low,uint8_t critical) {
Expand Down
80 changes: 47 additions & 33 deletions Channel.h
Expand Up @@ -102,7 +102,9 @@ class Channel {
uint8_t pidx = findpeer();
if( pidx != 0xff ) {
storage().setData(peerAddress(pidx),p);
getList3(pidx).single();
if( hasList3() == true ) {
getList3(pidx).single();
}
return true;
}
return false;
Expand All @@ -117,13 +119,15 @@ class Channel {
uint8_t pidx2 = findpeer();
if( pidx2 != 0xff ) {
storage().setData(peerAddress(pidx2),p2);
if( p1.odd() == true ) {
getList3(pidx1).odd();
getList3(pidx2).even();
}
else {
getList3(pidx2).odd();
getList3(pidx1).even();
if( hasList3() == true ) {
if( p1.odd() == true ) {
getList3(pidx1).odd();
getList3(pidx2).even();
}
else {
getList3(pidx2).odd();
getList3(pidx1).even();
}
}
return true;
}
Expand Down Expand Up @@ -160,9 +164,11 @@ class Channel {
void firstinit () {
storage().clearData(address(),size());
List1Type cl1 = getList1();
List2Type cl2 = getList2();
cl1.defaults();
cl2.defaults();
if( hasList2() ) {
List2Type cl2 = getList2();
cl2.defaults();
}
}

List1Type getList1 () const {
Expand Down Expand Up @@ -216,6 +222,10 @@ class Channel {
return List4Type(liststart);
}

static bool hasList2 () {
return List2Type::size() > 0;
}

static bool hasList3 () {
return List3Type::size() > 0;
}
Expand Down Expand Up @@ -308,35 +318,39 @@ class ActorChannel : public Channel<HalType,List1Type, List3Type,EmptyList,PeerC
}

bool process (const RemoteEventMsg& msg) {
bool lg = msg.isLong();
Peer p(msg.peer());
uint8_t cnt = msg.counter();
List3Type l3 = BaseChannel::getList3(p);
if( l3.valid() == true ) {
// l3.dump();
typename List3Type::PeerList pl = lg ? l3.lg() : l3.sh();
// pl.dump();
if( lg == false || cnt != lastmsgcnt || pl.multiExec() == true ) {
lastmsgcnt = cnt;
StateMachine::remote(pl,cnt);
if( BaseChannel::hasList3() ) {
bool lg = msg.isLong();
Peer p(msg.peer());
uint8_t cnt = msg.counter();
List3Type l3 = BaseChannel::getList3(p);
if( l3.valid() == true ) {
// l3.dump();
typename List3Type::PeerList pl = lg ? l3.lg() : l3.sh();
// pl.dump();
if( lg == false || cnt != lastmsgcnt || pl.multiExec() == true ) {
lastmsgcnt = cnt;
StateMachine::remote(pl,cnt);
}
return true;
}
return true;
}
return false;
}

bool process (const SensorEventMsg& msg) {
bool lg = msg.isLong();
Peer p(msg.peer());
uint8_t cnt = msg.counter();
uint8_t value = msg.value();
List3Type l3 = BaseChannel::getList3(p);
if( l3.valid() == true ) {
// l3.dump();
typename List3Type::PeerList pl = lg ? l3.lg() : l3.sh();
// pl.dump();
StateMachine::sensor(pl,cnt,value);
return true;
if( BaseChannel::hasList3() ) {
bool lg = msg.isLong();
Peer p(msg.peer());
uint8_t cnt = msg.counter();
uint8_t value = msg.value();
List3Type l3 = BaseChannel::getList3(p);
if( l3.valid() == true ) {
// l3.dump();
typename List3Type::PeerList pl = lg ? l3.lg() : l3.sh();
// pl.dump();
StateMachine::sensor(pl,cnt,value);
return true;
}
}
return false;
}
Expand Down
3 changes: 3 additions & 0 deletions ChannelList.h
Expand Up @@ -179,6 +179,9 @@ class EmptyList : public ChannelList<EmptyListData> {
void single () {}
void even () {}
void odd () {}

// allow compiler to optimize/remove code for EmptyList
static uint8_t size () { return 0; }
};


Expand Down
2 changes: 1 addition & 1 deletion MultiChannelDevice.h
Expand Up @@ -481,7 +481,7 @@ class ChannelDevice : public Device<HalType,List0Type> {
ChannelType& c = channel(ch);
if (numlist == 1) {
return c.getList1();
} else if (numlist == 2) {
} else if (c.hasList2() && numlist == 2) {
return c.getList2();
} else if (c.hasList3() && numlist == 3) {
return c.getList3(peer);
Expand Down

0 comments on commit 5ed4ea8

Please sign in to comment.