Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
YES CHECKSUMS
Browse files Browse the repository at this point in the history
  • Loading branch information
jfly committed Oct 12, 2014
1 parent 9303509 commit 7dbad8e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class StackmatStateReceiver : public Receiver<StackmatState> {
}

void receive(StackmatState state) {
bool validChecksum = state.checksum != state.computedChecksum();
LOG2("StackmatStateReceiver::receive() state.millis: %d validChecksum: %d", state.millis, validChecksum);
bool validChecksum = state.checksum == state.computedChecksum();
LOG2("StackmatStateReceiver::receive() state.millis: %d checksum: %d computedChecksum: %d", state.millis, state.checksum, state.computedChecksum());
if(!validChecksum) {
return;
}
Expand Down
48 changes: 11 additions & 37 deletions src/stackmat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,28 @@ LOG_HANDLE("stackmat")

StackmatSynthesizer::StackmatSynthesizer() {}

#define MILLIS_PER_MINUTE (60*1000)
#define MILLIS_PER_SECOND (1000)
#define MILLIS_PER_DECISECOND (100)
#define MILLIS_PER_CENTISECOND (10)

void StackmatSynthesizer::receive(StackmatState state) {
LOG2("state.millis: %d state.checksum: %d", state.millis, state.checksum);
send(state.commandByte);

int checksum = 64;
int millis = state.millis;

// minutes
int minutes_digit = millis / MILLIS_PER_MINUTE;
millis %= MILLIS_PER_MINUTE;
checksum += minutes_digit;
send('0' + minutes_digit);
send('0' + state.minutesDigit());

// seconds
int seconds = millis / MILLIS_PER_SECOND;
millis %= MILLIS_PER_SECOND;

int dekaseconds_digit = seconds / 10;
checksum += dekaseconds_digit;
send('0' + dekaseconds_digit);

int seconds_digit = seconds % 10;
checksum += seconds_digit;
send('0' + seconds_digit);
send('0' + state.tensSecondsDigit());
send('0' + state.onesSecondsDigit());

// decimal
int deciseconds_digit = millis / MILLIS_PER_DECISECOND;
millis = millis % MILLIS_PER_DECISECOND;
checksum += deciseconds_digit;
send('0' + deciseconds_digit);
send('0' + state.tenthsDigit());

int centiseconds_digit = millis / MILLIS_PER_CENTISECOND;
millis = millis % MILLIS_PER_CENTISECOND;
checksum += centiseconds_digit;
send('0' + centiseconds_digit);
send('0' + state.hundredthsDigit());

if(state.generation == 3) {
int milliseconds_digit = millis;
checksum += milliseconds_digit;
send('0' + milliseconds_digit);
send('0' + state.thousandthsDigit());
}

// checksum
send(checksum);
send(state.checksum);

// LF
send('\n');
Expand All @@ -76,7 +50,7 @@ void StackmatInterpreter::reset() {
}

void StackmatInterpreter::receive(int byte) {
LOG2("StackmatInterpreter::receive(%d)", byte);
LOG2("byte: %d", byte);
if(byte < 0) {
// idle received, parse collected characters
switch(receivedBytesLength) {
Expand All @@ -93,7 +67,7 @@ void StackmatInterpreter::receive(int byte) {
int i = 0;
state.commandByte = receivedBytes[i++];

int minuteDigit = (receivedBytes[i++] - '0');
int minutesDigit = (receivedBytes[i++] - '0');
int tensSecondsDigit = (receivedBytes[i++] - '0');
int onesSecondsDigit = (receivedBytes[i++] - '0');
int tenthsDigit = (receivedBytes[i++] - '0');
Expand All @@ -109,7 +83,7 @@ void StackmatInterpreter::receive(int byte) {
unsigned char cr = receivedBytes[i++];

state.millis = 0;
state.millis += minuteDigit * MILLIS_PER_MINUTE;
state.millis += minutesDigit * MILLIS_PER_MINUTE;
int seconds = 10 * tensSecondsDigit + onesSecondsDigit;
state.millis += seconds * MILLIS_PER_SECOND;
state.millis += tenthsDigit * MILLIS_PER_DECISECOND;
Expand Down
20 changes: 9 additions & 11 deletions src/stackmat.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace fskube {
#define LARGESTSIGNAL_BYTES GEN3SIGNAL_BYTES

#define MILLIS_PER_MINUTE (60*1000)
#define MILLIS_PER_TENSECONDS (10000)
#define MILLIS_PER_SECOND (1000)
#define MILLIS_PER_DECISECOND (100)
#define MILLIS_PER_CENTISECOND (10)
Expand All @@ -26,29 +27,26 @@ struct StackmatState {
unsigned int generation;
unsigned char commandByte;

unsigned int minuteDigit() {
int minutes_digit = millis / MILLIS_PER_MINUTE;
return minutes_digit;
unsigned int minutesDigit() {
return ( millis / MILLIS_PER_MINUTE ) % 10;
}
unsigned int tensSecondsDigit() {
int seconds = millis / MILLIS_PER_SECOND;
return seconds / 10;
return ( ( millis % MILLIS_PER_MINUTE ) / MILLIS_PER_TENSECONDS ) % 10;
}
unsigned int onesSecondsDigit() {
int seconds = millis / MILLIS_PER_SECOND;
return seconds % 10;
return ( millis / MILLIS_PER_SECOND ) % 10;
}
unsigned int tenthsDigit() {
return ( millis % MILLIS_PER_SECOND ) / MILLIS_PER_DECISECOND;
return ( millis / MILLIS_PER_DECISECOND ) % 10;
}
unsigned int hundredthsDigit() {
return ( millis % MILLIS_PER_SECOND ) % MILLIS_PER_DECISECOND;
return ( millis / MILLIS_PER_CENTISECOND ) % 10;
}
unsigned int thousandthsDigit() {
return millis % 1000;
return millis % 10;
}
unsigned char computedChecksum() {
return 64 + minuteDigit() + tensSecondsDigit() +
return 64 + minutesDigit() + tensSecondsDigit() +
onesSecondsDigit() + tenthsDigit() + hundredthsDigit() +
thousandthsDigit();
}
Expand Down
3 changes: 2 additions & 1 deletion test/ExtrapolationTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ def test(self):
# We only extrapolate the signal when we think that the timer is running.
state = fskube.StackmatState()
state.millis = 40
state.checksum = state.computedChecksum()
stackmatSythesizer.receive(state)
state.millis = 500
state.checksum = state.computedChecksum()
stackmatSythesizer.receive(state)
for sample in sampler.data:
fskube.fskube_addSample(sample)
Expand All @@ -42,7 +44,6 @@ def test(self):
sleepSecs = 1
time.sleep(sleepSecs)
currentSignalMillis = fskube.fskube_getState().millis
import pdb; pdb.set_trace()#<<<
self.assertGreaterEqual(currentSignalMillis, lastSignalMillis + sleepSecs*1000)

if __name__ == "__main__":
Expand Down

0 comments on commit 7dbad8e

Please sign in to comment.