f46449904f89728a1ddd3f93acf2016867f4ae04
[svn42.git] / dart / dart.pde
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3 #include <inttypes.h>
4
5 #define RF433_PIN 10
6 //********************************************************************//
7
8 typedef unsigned char byte;
9
10 typedef struct {
11   byte offset;
12   byte state;
13 } rf_bit_t;
14
15 // offset is number of alphas (0.08ms)
16
17 const rf_bit_t zero_bit[] = { {  4, 1 },
18                               { 16, 0 },
19                               { 20, 1 },
20                               { 32, 0 },
21                               {  0, 0 } };
22
23 const rf_bit_t one_bit[] = { { 12, 1 },
24                              { 16, 0 },
25                              { 28, 1 },
26                              { 32, 0 },
27                              {  0, 0 } };
28
29 const rf_bit_t float_bit[] = { {  4, 1 },
30                                { 16, 0 },
31                                { 28, 1 },
32                                { 32, 0 },
33                                {  0, 0 } };
34
35 const rf_bit_t sync_bit[] = { {   4, 1 },
36                               { 128, 0 },
37                               {   0, 0 } };
38
39 typedef enum { ZERO = 0, ONE , FLOAT , SYNC } adbit_t;
40 typedef byte ad_bit_t;
41 #define WORD_LEN 13
42 typedef ad_bit_t word_t[WORD_LEN];
43
44 const rf_bit_t* bit_defs[] = { zero_bit, one_bit, float_bit, sync_bit };
45
46 byte alpha_cnt = 0;
47 byte bit_cnt = 0;
48 byte chunk_cnt = 0;
49 byte word_cnt = 0;
50 const ad_bit_t* current_word;
51 byte volatile frame_finished = 1;
52
53 #define FRAME_LEN 8
54
55 #define A1_ON  0
56 #define A1_OFF 1
57 #define A2_ON  2
58 #define A2_OFF 3
59
60 #define B1_ON  4
61 #define B1_OFF 5
62 #define B2_ON  6
63 #define B2_OFF 7
64
65 #define C1_ON  8
66 #define C1_OFF 9
67 #define C2_ON  10
68 #define C2_OFF 11
69
70 #define D1_ON  12
71 #define D1_OFF 13
72 #define D2_ON  14
73 #define D2_OFF 15
74
75 const word_t words[]  = { 
76 { ZERO,  ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // A1_ON
77 { ZERO,  ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // A1_OFF
78 { ZERO,  ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // A2_ON
79 { ZERO,  ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // A2_OFF
80
81 { FLOAT, ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // B1_ON
82 { FLOAT, ZERO,  FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // B1_OFF
83 { FLOAT, ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // B2_ON
84 { FLOAT, ZERO,  FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // B2_OFF
85
86 { ZERO,  FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // C1_ON
87 { ZERO,  FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // C1_OFF
88 { ZERO,  FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // C2_ON
89 { ZERO,  FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // C2_OFF
90
91 { FLOAT, FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // D1_ON
92 { FLOAT, FLOAT, FLOAT, FLOAT, ZERO,  ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }, // D1_OFF
93 { FLOAT, FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, FLOAT, SYNC }, // D2_ON
94 { FLOAT, FLOAT, FLOAT, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO, FLOAT, FLOAT, ZERO,  SYNC }  // D2_OFF
95 };
96
97
98 //********************************************************************//
99
100 void start_timer()
101 {
102   // timer 1: 2 ms
103   TCCR1A = 0;                    // prescaler 1:8, WGM = 4 (CTC)
104   TCCR1B = 1<<WGM12 | 1<<CS11;   // 
105   OCR1A = 159;        // (1+159)*8 = 1280 -> 0.08ms @ 16 MHz -> 1*alpha
106 //  OCR1A = 207;        // (1+207)*8 = 1664 -> 0.104ms @ 16 MHz -> 1*alpha
107   TCNT1 = 0;          // reseting timer
108   TIMSK1 = 1<<OCIE1A; // enable Interrupt
109 }
110
111 void stop_timer() // stop the timer
112 {
113   // timer1
114   TCCR1B = 0; // no clock source
115   TIMSK1 = 0; // disable timer interrupt
116 }
117
118 union {
119   byte uint8[4];
120   uint32_t uint32;
121 } rf433_data;
122 byte rf433_hi_cnt=0;
123 byte rf433_lo_cnt=0;
124 byte last_sample=0;
125 int valid=0;
126 ISR(TIMER1_COMPA_vect)
127 {
128   byte sample = digitalRead(RF433_PIN);
129   if (last_sample!=sample && sample==HIGH)
130   {
131     if ( rf433_lo_cnt > 2 && rf433_lo_cnt<6 && rf433_hi_cnt>10 && rf433_hi_cnt < 14)
132     {
133       rf433_data.uint32<<=1;
134       rf433_data.uint32|=1;
135       valid++;
136     } else if (rf433_hi_cnt > 2 && rf433_hi_cnt<6 && rf433_lo_cnt>10 && rf433_lo_cnt < 14) {
137       rf433_data.uint32<<=1;
138       valid++;
139     } else if (rf433_hi_cnt > 2 && rf433_hi_cnt<6 && rf433_lo_cnt>120 && rf433_lo_cnt < 128 && valid >=24) {
140       //rf433_data.uint8[3]=0;
141       //Serial.print(rf433_data.uint32);
142       Serial.print(rf433_data.uint8[0],BYTE);
143       Serial.print(rf433_data.uint8[1],BYTE);
144       Serial.print(rf433_data.uint8[2],BYTE);
145       //Serial.print(rf433_data.uint8[3],BYTE);
146     } else {
147       valid=0;
148       rf433_data.uint32=0;
149     }
150     rf433_hi_cnt=0;
151     rf433_lo_cnt=0;
152   }
153   if (sample == HIGH)
154     rf433_hi_cnt++;
155   else 
156     rf433_lo_cnt++;
157   last_sample=sample;
158 }
159
160 //unsigned long wm_start_[3]={0,0,0};
161 //bool wait_millis(unsigned long *start_time, unsigned long ms)
162 //{
163 //  if (ms == 0)
164 //    return false;
165 //  else if (*start_time > 0)
166 //  {
167 //    if (millis() < *start_time || millis() > (*start_time) + ms)
168 //    {
169 //      *start_time = 0;
170 //      return false;
171 //    }
172 //    else
173 //      return true;
174 //  }
175 //  else
176 //  {
177 //    *start_time=millis();
178 //    return true;
179 //  }
180 //}
181
182 //********************************************************************//
183
184 void setup()
185 {
186   pinMode(RF433_PIN, INPUT);      // set pin to input
187   digitalWrite(RF433_PIN, LOW);  // turn of pullup resistors 
188
189   Serial.begin(57600);
190 //  Serial.println("starting timer");
191   start_timer();
192 }
193
194
195 void loop()
196 {
197 }