X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=rf433rcv%2Fteensy%2Fexample.c;h=649f6de32f09cdb64e10de2d928d599b83ec9292;hb=f505a1c37ea1c76897991f9882de765e34f0cc98;hp=635b16f613bc97f797305e92f0207f7c655b1aac;hpb=deb2b278d460a716e0e2d8be370b1ee8f8963e34;p=svn42.git diff --git a/rf433rcv/teensy/example.c b/rf433rcv/teensy/example.c index 635b16f..649f6de 100644 --- a/rf433rcv/teensy/example.c +++ b/rf433rcv/teensy/example.c @@ -26,84 +26,156 @@ #include #include #include "usb_rawhid.h" -#include "analog.h" #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n)) +#define RF_SEND_BUFFER_LEN_MAX 128 +volatile uint16_t output_count=0; +volatile uint8_t active_buffer=0; +volatile uint16_t send_buffer=0; +volatile uint8_t capture=0; +volatile uint16_t rf_send=0; //count of bits in rf_send_buffer that should be sent +volatile uint16_t rf_send_reload=0; //count of bits in rf_send_buffer that should be sent +volatile uint8_t rf_send_reload_count=0; // number of repetitions (times rf_send gets reloaded; +uint8_t read_buffer[64]; // buffer for reading usb signals +uint8_t write_buffer[2][64]; // buffer for writing usb signals +uint8_t rf_send_buffer[RF_SEND_BUFFER_LEN_MAX]; // buffer for sending rf433 signals +volatile uint16_t rf_send_buffer_len=0; -volatile uint8_t do_output=0; -uint8_t buffer[64]; -int main(void) +void reset() { - int8_t r; - uint8_t i; - uint16_t val, count=0; - - // set for 16 MHz clock - CPU_PRESCALE(0); - - // Initialize the USB, and then wait for the host to set configuration. - // If the Teensy is powered without a PC connected to the USB port, - // this will wait forever. - usb_init(); - while (!usb_configured()) /* wait */ ; - - // Wait an extra second for the PC's operating system to load drivers - // and do whatever it does to actually be ready for input - _delay_ms(1000); + cli(); + // disable watchdog, if enabled + // disable all peripherals + UDCON = 1; + USBCON = (1< 0) { - // output 4 bits to D0, D1, D2, D3 pins - DDRD = 0x0F; - PORTD = (PORTD & 0xF0) | (buffer[0] & 0x0F); - // ignore the other 63.5 bytes.... - } - // if time to send output, transmit something interesting - if (do_output) { - do_output = 0; - // send a packet, first 2 bytes 0xABCD - buffer[0] = 0xAB; - buffer[1] = 0xCD; - // put A/D measurements into next 24 bytes - for (i=0; i<12; i++) { - val = analogRead(i); - buffer[i * 2 + 2] = val >> 8; - buffer[i * 2 + 3] = val & 255; - } - // most of the packet filled with zero - for (i=26; i<62; i++) { - buffer[i] = 0; - } - // put a count in the last 2 bytes - buffer[62] = count >> 8; - buffer[63] = count & 255; - // send the packet - usb_rawhid_send(buffer, 50); - count++; - } - } + // Initialize the USB, and then wait for the host to set configuration. + // If the Teensy is powered without a PC connected to the USB port, + // this will wait forever. + usb_init(); + while (!usb_configured()) /* wait */ ; + // Wait an extra second for the PC's operating system to load drivers + // and do whatever it does to actually be ready for input + _delay_ms(1000); + while (1) { + // if received data, do something with it + int8_t r = usb_rawhid_recv(read_buffer, 0); + if (r>0) + { + if (read_buffer[0]=='r') //reset + reset(); + else if (read_buffer[0]=='b') //begin capture + capture=1; + else if (read_buffer[0]=='e') //end capture + capture=0; + else if (read_buffer[0]=='f') //fill send buffer + { + int8_t buffer_pos = 1; + while(buffer_pos < r && rf_send_buffer_len2) + // rf_send_reload=rf_send_buffer_len*8-read_buffer[2]; // substract bit offset + //else + usb_rawhid_send(rf_send_buffer, 145); + rf_send=0; + rf_send_reload=rf_send_buffer_len*8; + rf_send_reload_count=read_buffer[1]; + //read_buffer[0]=rf_send_reload; + //read_buffer[1]=rf_send_reload>>8; + //read_buffer[2]=0; + } + } + if (send_buffer) + { + send_buffer=0; + usb_rawhid_send(write_buffer[active_buffer?0:1], 45); + } + } } // This interrupt routine is run approx 61 times per second. -ISR(TIMER0_OVF_vect) +ISR(TIMER0_COMPA_vect) { - static uint8_t count=0; - - // set the do_output variable every 2 seconds - if (++count > 122) { - count = 0; - do_output = 1; - } + PORTF^=2; + if (rf_send) + { + if ( ( rf_send_buffer[rf_send/8] >> ( (rf_send%8)?8-(rf_send%8):0 ) ) & 1) + { + PORTF&=~1; + } else { + PORTF|=1; + } + //rf_send_buffer[rf_send/8]>>=1; + rf_send--; + } else if (rf_send_reload_count) { + rf_send=rf_send_reload; + rf_send_reload_count--; + } else { + PORTF&=~1; + if (capture) { + write_buffer[active_buffer][output_count/8]<<=1; + write_buffer[active_buffer][output_count++/8]|=PINB&1; + if (output_count>=64*8) + { + output_count=0; + active_buffer=active_buffer?0:1; + send_buffer=1; + } + } + } } - - -