diff --git a/firmware/slider/CMakeLists.txt b/firmware/slider/CMakeLists.txt index b3f6d58..c59228a 100644 --- a/firmware/slider/CMakeLists.txt +++ b/firmware/slider/CMakeLists.txt @@ -7,5 +7,5 @@ project(slider_firmware C) add_subdirectory(SliderCommunication) -add_avr_executable(slider_firmware main.c) +add_avr_executable(slider_firmware led.c main.c adc.c ) target_link_libraries(slider_firmware DSPLAB_SliderCommunication) diff --git a/firmware/slider/adc.c b/firmware/slider/adc.c new file mode 100644 index 0000000..2d420b3 --- /dev/null +++ b/firmware/slider/adc.c @@ -0,0 +1,75 @@ +/* + * adc.c + * + * Created on: 08.02.2019 + * Author: julian + */ + +#include "adc.h" +#include +#include + +#include "led.h" + +// global adc instance +volatile adc_t adc; + +static inline void adc_next() { + do { + adc.current_channel = (adc.current_channel+1)%8; + + if (!adc.current_channel) { + adc.current_sample = adc.buffer.ptr-1; + + if (adc.interrupt) + adc.interrupt(&adc.buffer); + } + } while(!(adc.channel_mask & (1< + +typedef uint16_t adc_sample_t; + +typedef struct { + adc_sample_t * ptr; + uint8_t size; +} adc_buffer_t; + +typedef void (*adc_interrupt_t)(volatile adc_buffer_t * buf); + +typedef struct adc { + adc_buffer_t buffer; + + uint8_t current_channel; + adc_sample_t * current_sample; + uint8_t channel_mask; + + adc_interrupt_t interrupt; +} adc_t; + +// the adc instance +extern volatile adc_t adc; + +enum { + ADC_CHANNEL_0 = 0x01, + ADC_CHANNEL_1 = 0x02, + ADC_CHANNEL_2 = 0x04, + ADC_CHANNEL_3 = 0x08, + ADC_CHANNEL_4 = 0x10, + ADC_CHANNEL_5 = 0x20, + ADC_CHANNEL_6 = 0x40, + ADC_CHANNEL_7 = 0x80, + ADC_CHANNEL_ALL = 0xFF, +}; + + +void adc_init(adc_buffer_t buffer, uint8_t channel_mask); + +void adc_on_done(adc_interrupt_t interrupt); + +void adc_start(); +void adc_stop(); + +#endif /* ADC_H_ */ diff --git a/firmware/slider/led.c b/firmware/slider/led.c new file mode 100644 index 0000000..800f6a5 --- /dev/null +++ b/firmware/slider/led.c @@ -0,0 +1,14 @@ +/* + * led.c + * + * Created on: 09.02.2019 + * Author: julian + */ + + +#include "led.h" + +void led_init() { + LED_DDR |= (1< + +/* led driver */ +#define LED_DDR DDRB +#define LED_REG PORTB +#define LED_PIN 0 + +static inline void led_toggle() { + LED_REG ^= (1< #include +#include "adc.h" +#include "led.h" -/* adc driver */ -void adc_init() { - ADMUX = 0; // set channel, set reference to vdd - ADCSRA = (1< + +void on_read(iface_t * sender, byte_t b) { + if (!IS_EOM(b)) { + sender->write(sender, b); + return; + } + + // strip EOM message + sender->write(sender, b&0x7F); +} + +struct { + volatile message_t buffer; + adc_sample_t adc_buffer[5]; +} app = {}; + + +ISR(PCINT1_vect) { + if(BUTTON_REG&(1<ptr[4]; + + app.buffer.poti[0] = buf->ptr[3]; + app.buffer.poti[1] = buf->ptr[2]; + app.buffer.poti[2] = buf->ptr[0]; + app.buffer.poti[3] = buf->ptr[1]; +} + int main() { init(); + adc_on_done(adc_int); - while(1) {} + // start relevant perephials + adc_start(); + + while(1) { + delay(app.buffer.poti[0]>>4); + } }