Skip to content

Commit

Permalink
New IR tester code in bareblinkOS/main.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
bigjosh committed Oct 20, 2018
1 parent 1d41fb9 commit 703cfe7
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 70 deletions.
18 changes: 10 additions & 8 deletions AS7/blink/bareblinkOS/bareblinkOS/blinkos.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ typedef uint32_t millis_t;
// loopstate is passed in and out of the user program on each pass
// through the main event loop



// I know this is ugly, but keeping them in a single byte lets us pass them by value
// and also lets us OR them together. Efficiency in the updater trumps since it
// runs every millisecond.
Expand Down Expand Up @@ -57,11 +55,6 @@ struct ir_data_buffer_t {

};

// Sends immediately. Blocks until send is complete.
// Higher level should provide some collision control.

void ir_send_userdata( uint8_t face, const uint8_t *data , uint8_t len );


struct loopstate_in_t {

Expand All @@ -87,10 +80,19 @@ struct loopstate_out_t {

};

// Sends immediately. Blocks until send is complete.
// Higher level should provide some collision control.
// No error checking except the intrinsic robustnesses of the framing and the header byte.
// Higher level can decide if it is worth adding more

void ir_send_userdata( uint8_t face, const uint8_t *data , uint8_t len );

// The state record we are sending to userland
extern loopstate_in_t loopstate_in;

// These are provided by blinklib
// Will soon be jump vectors

void setupEntry();

//void setupEntry();
void loopEntry( loopstate_in_t const *loopstate_in , loopstate_out_t *loopstate_out);
149 changes: 88 additions & 61 deletions AS7/blink/bareblinkOS/bareblinkOS/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,92 +11,119 @@

void setupEntry() {

asm("nop");
// Blank

};

uint8_t step;
millis_t nextstep;
#define COOKIE_BYTE 0b10110111 // Non repeating bit pattern to detect errors.

millis_t nextsend;
uint8_t cookie_byte_buffer = COOKIE_BYTE; // Must be allocated in RAM so we can pass a pointer to it to senddata()

uint8_t message[] = { 'J' , 'o' , 's' , 'h' };

uint8_t errorFlag[ IR_FACE_COUNT];
#define TX_BLIND_SEND_TIME_MS 250 // How often to do a blind send when no RX has happened recently to trigger ping pong

void loopEntry( loopstate_in_t const *loopstate_in , loopstate_out_t *loopstate_out) {
#define MISSED_RX_SHOW_TIME_MS 500 // How long to show blue when a RX timeout happens
#define RX_TIMEOUT_RX 200 // If we do not see a message in this long, then show a timeout

millis_t now = loopstate_in->millis;
// Note these all init to 0's, which is what we want.

if ( loopstate_in->buttonstate.bitflags & BUTTON_BITFLAG_PRESSED ) {
struct face_t {

millis_t next_tx_ms; // Next time to transmit
millis_t last_rx_ms; // Last time a good packet seen

uint8_t errorFlag;
};

loopstate_out->colors[step] = pixelColor_t( 0 , 0 , 0 , 1 );
face_t faces[IR_FACE_COUNT];

step++;
void loopEntry( loopstate_in_t const *loopstate_in , loopstate_out_t *loopstate_out) {

if (step>= PIXEL_FACE_COUNT ) {
step=0;
}
millis_t now_ms = loopstate_in->millis;

// Clear out any errors on button press

loopstate_out->colors[step] = pixelColor_t( 23 , 10, 0 , 1 );
if ( loopstate_in->buttonstate.bitflags & BUTTON_BITFLAG_PRESSED ) {

errorFlag[step]=0;

nextstep = loopstate_in->millis + 200;
}


for( uint8_t f=0; f< IR_FACE_COUNT; f++ ) {

if ( faces[f].errorFlag ) {

faces[f].errorFlag=0;

}

}

}


for( uint8_t f=0; f< IR_FACE_COUNT; f++ ){

// Look for received packet....

if ( loopstate_in->ir_data_buffers[f].ready_flag ) {

if ( (loopstate_in->ir_data_buffers[f].len == 4 )) {

const uint8_t *d = loopstate_in->ir_data_buffers[f].data;

if ( d[0] =='J' && d[1] =='o' && d[2] == 's' && d[3] == 'h' ) {

// Good packet!
loopstate_out->colors[f] = pixelColor_t( 0 , 20, 0 , 1 );

} else {
if ( (loopstate_in->ir_data_buffers[f].len == 1 ) && loopstate_in->ir_data_buffers[f].data[0] == COOKIE_BYTE ) {

errorFlag[f] = 2;

}

} else {
// Bad len
// Got a good packet!

faces[f].last_rx_ms = now_ms;

faces[f].next_tx_ms = now_ms; // pong

} else {

// Either len or cookie was wrong, so data transmission error

faces[f].errorFlag = 1;

errorFlag[f] = 1;


}

}
}

if (errorFlag[f]==1) {
// Update shown color

if (faces[f].errorFlag) {

// Red on error
loopstate_out->colors[f] = pixelColor_t( 20 , 0, 0 , 1 );

} else {

millis_t elapsed_ms = now_ms - faces[f].last_rx_ms; // Elapsed time since last RX

if (elapsed_ms < 200) {

// Less than 200ms since last good RX, so show green
loopstate_out->colors[f] = pixelColor_t( 0 , 20, 0 , 1 );

} else if ( elapsed_ms < 500 ) {

// More then 200ms since last good RX, so show red for 300ms
loopstate_out->colors[f] = pixelColor_t( 0 , 0, 20 , 1 );

} else if (errorFlag[f]==2) {


loopstate_out->colors[f] = pixelColor_t( 0 , 0, 20 , 1 );

} else {

// Been long enough that we assume no one is there
loopstate_out->colors[f] = pixelColor_t( 0 , 0, 0 , 1 );

}

}

}
}


if ( faces[f].next_tx_ms < now_ms ) {

// Time to send on this face

ir_send_userdata( f , &cookie_byte_buffer , 1 );

// Schedule a blind send in case we don't see a ping soon
faces[f].next_tx_ms = now_ms + TX_BLIND_SEND_TIME_MS;

} // for(f)

}



if ( nextsend < now ) {

ir_send_userdata( 4 , message , 4 );

nextsend = now + 200;
}


}
1 change: 0 additions & 1 deletion libraries/blinkos/src/blinkos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ void run(void) {

if (sleepTimer.isExpired()) {
sleep();

}


Expand Down

0 comments on commit 703cfe7

Please sign in to comment.