From f67475e8e423844733bb925cc34871f5aee28627 Mon Sep 17 00:00:00 2001 From: Julian Daube Date: Sun, 10 Feb 2019 23:03:00 +0100 Subject: [PATCH] small changes --- firmware/slider/adc.c | 12 +--- firmware/slider/adc.h | 4 +- firmware/slider/main.c | 126 ++++++++++++++++++++++++++++------------- firmware/slider/main.h | 1 - 4 files changed, 91 insertions(+), 52 deletions(-) diff --git a/firmware/slider/adc.c b/firmware/slider/adc.c index 0f108dc..1e978fe 100644 --- a/firmware/slider/adc.c +++ b/firmware/slider/adc.c @@ -20,13 +20,6 @@ static inline void adc_next() { if (!adc.current_channel) { adc.current_sample = adc.buffer.ptr-1; - - // update app state (remap channels) - app.state.state.slider = adc.buffer.ptr[4]; - app.state.state.poti[0] = adc.buffer.ptr[3]; - app.state.state.poti[1] = adc.buffer.ptr[2]; - app.state.state.poti[2] = adc.buffer.ptr[0]; - app.state.state.poti[3] = adc.buffer.ptr[1]; } } while(!(adc.channel_mask & (1< typedef struct { + byte_t * buffer_start, * buffer_end; + + volatile byte_t * read; + volatile byte_t * write; + volatile byte_t count; + iface_t iface; } comm_t; -comm_t comm; +comm_t comm; + void comm_write(iface_t * iface, byte_t byte) { - // disable interrupt (temporarily) - uint8_t temp = USICR; - USICR &= ~(1<= comm.buffer_end) { + comm.write = comm.buffer_start; + } } -void comm_init() { +inline byte_t comm_read() { + if (!comm.count) + return 0x00; + + byte_t result = *comm.read; + + --comm.count; + if ((++comm.read) >= comm.buffer_end) { + comm.read = comm.buffer_start; + } + + return result; +} + +byte_t comm_buffer[10]; + +void comm_init() +{ + comm.buffer_start = comm_buffer; + comm.buffer_end = comm_buffer + sizeof(comm_buffer); + + // reset buffer + comm.write = comm.buffer_start; + comm.read = comm.buffer_start; + comm.count = 0; + // TODO: setup pins + DDRA |= (1<write(sender, b); - return; - } - - // strip EOM message - sender->write(sender, b&0x7F); +ISR(USI_OVF_vect) +{ + cli(); + // write buffer out if there is data to be written + //if (!comm.count) { + // call interrupt handler + //comm.iface.on_read(&comm.iface, USIDR, comm.iface.callback_data); + //} + + USIDR = 0x02; + sei(); + //USIDR = comm_read(); } #include "main.h" @@ -70,8 +105,18 @@ void on_read(iface_t * sender, byte_t b) { // the public instance of the app buffer struct app app = {}; +adc_sample_t * channel_map[] = { + // update app state (remap channels) + &app.state.state.poti[2], + &app.state.state.poti[3], + &app.state.state.poti[1], + &app.state.state.poti[0], + &app.state.state.slider +}; + // initalizes the application modules -void init() { +void init() +{ // init button module (making the button light up on press) // module also updates app.buffer.button for us button_init(); @@ -80,8 +125,8 @@ void init() { // will sample 0x8F (CHANNEL 8 and 0-3) // this module also updates app.buffer.poti and slider for us adc_init((adc_buffer_t){ - .ptr = app.adc_buffer, - .size = 5, + .ptr = channel_map, + .size = sizeof(channel_map), }, 0x8F); // init communication module @@ -98,21 +143,22 @@ void init() { sei(); } +#include // little debug helper function // FIXME: remove when not needed -void delay(uint16_t d) { - while(d--) { - __asm("nop"); +void delay(uint16_t d) +{ + while(d--) + { + _delay_ms(1); } } -int main() { +int main() +{ init(); - - // start relevant periphials - adc_start(); - + while(1) { // nothing to do here, the application is interrupt-driven } diff --git a/firmware/slider/main.h b/firmware/slider/main.h index 143a2a5..3e8c14a 100644 --- a/firmware/slider/main.h +++ b/firmware/slider/main.h @@ -8,7 +8,6 @@ struct app { slave_t state; - adc_sample_t adc_buffer[5]; }; extern struct app app;