added basic sender features
authorOthmar Gsenger <otti@realraum.at>
Tue, 13 Dec 2011 18:33:51 +0000 (18:33 +0000)
committerOthmar Gsenger <otti@realraum.at>
Tue, 13 Dec 2011 18:33:51 +0000 (18:33 +0000)
rf433rcv/teensy/example.c

index ad07cda..282e0bc 100644 (file)
 #include "usb_rawhid.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 uint8_t send_buffer=0;
-uint8_t read_buffer[64];
-uint8_t write_buffer[2][64];
+volatile uint16_t send_buffer=0;
+volatile uint8_t capture=0;
+volatile uint8_t rf_send=0; //count of bits in rf_send_buffer that should be sent
+volatile uint8_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
+uint8_t rf_send_buffer_len=0;
 
 int main(void)
 {
@@ -59,8 +65,37 @@ int main(void)
   _delay_ms(1000);
   while (1) {
     // if received data, do something with it
-    //r = usb_rawhid_recv(read_buffer, 0);
-    //if (r>0)
+    int8_t r = usb_rawhid_recv(read_buffer, 0);
+    if (r>0)
+    {
+      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 byte_rem = r-1;
+        while(byte_rem && rf_send_buffer_len<RF_SEND_BUFFER_LEN_MAX)
+        {
+          rf_send_buffer[rf_send_buffer_len]=read_buffer[r-byte_rem];
+          rf_send_buffer_len++;
+          byte_rem--;
+        }
+      }
+      else if (read_buffer[0]=='c') // clear send buffer
+      {
+        rf_send_buffer_len=0;
+      }
+      else if (read_buffer[0]=='s') //send
+      {
+        capture=0;
+        if (r>2)
+          rf_send_reload=rf_send_buffer_len*8-read_buffer[2]; // substract bit offset
+        else
+          rf_send_reload=rf_send_buffer_len*8;
+        rf_send_reload_count=read_buffer[1];  
+      }
+    }
     if (send_buffer)
     {
       send_buffer=0;
@@ -73,13 +108,27 @@ int main(void)
 ISR(TIMER0_COMPA_vect)
 {
   PORTF^=2;
-  write_buffer[active_buffer][output_count/8]<<=1;
-  write_buffer[active_buffer][output_count++/8]|=PINB&1;
-  if (output_count>=64*8)
+  if (rf_send)
   {
-    output_count=0;
-    active_buffer=active_buffer?0:1;
-    send_buffer=1;
+    if (rf_send_buffer[rf_send/8] & 0x80)
+    {
+     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;
+  } else 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;
+    }
   }
 }