Revision 31f4a032 scout_avr/src/bom.cpp

View differences:

scout_avr/src/bom.cpp
35 35
// rx global vars
36 36
static uint8_t prev_bom_sig;
37 37
static struct bom_rx_t {
38
    uint8_t bits_rx;
38
    uint8_t count;
39 39
    uint16_t bits;
40
    int last_bit;
40
    int last_time;
41 41

  
42 42
    uint8_t new_data;
43 43
    uint8_t address;
......
114 114
}
115 115

  
116 116
ISR(TIMER4_COMPA_vect) {
117
  if (out_counter) {
118
    if (out_high) {
119
      stop_38kHz_signal();
120
      if (out_msg & 1) {
117
  if (out_high) {
118
    stop_38kHz_signal();
119
    if (out_counter) {
120
      out_counter--;
121
      if ((out_msg >> out_counter) & 1) {
121 122
        OCR4A = TIME_MICROS(1680, 64);
122 123
      } else {
123 124
        OCR4A = TIME_MICROS(680, 64);
124 125
      }
125
      out_msg >>= 1;
126
      out_counter--;
127 126
    } else {
128
      start_38kHz_signal();
129
      OCR4A = TIME_MICROS(320, 64);
127
      stop_data_signal();
128
      out_done = 1;
130 129
    }
131 130
  } else {
132
    stop_data_signal();
133
    out_done = 1;
131
    start_38kHz_signal();
132
    OCR4A = TIME_MICROS(320, 64);
134 133
  }
135 134
}
136 135

  
......
154 153
    case BOM_RIGHT: out_pin_mask = _BV(BOM_EMIT2); break;
155 154
    case BOM_BACK:  out_pin_mask = _BV(BOM_EMIT3); break;
156 155
  }
157
  out_counter = 17;
156
  out_counter = 16;
158 157
  out_msg = sharp_msg_make(0x2A, bom_msg_make(robot_id, dir));
159 158
  out_done = 0;
160 159
  start_data_signal();
......
165 164

  
166 165
static void recv_edge(char is_rising, struct bom_rx_t *rx) {
167 166
  if (is_rising) {
167
    // TODO check 320us? or have 320us timeout on rising edge?
168
  } else {
168 169
    // uses timer 1, assuming prescale 1/8
169 170
    // timer 1 is set up by range_init()
170
    int now = TCNT1 * (F_CPU / 1000000) / 8;
171
    int diff = now - rx->last_bit;
172
    rx->last_bit = now;
173
    rx->bits_rx++;
174
    rx->bits <<= 1;
175
    if (MIN_LOW_PW < diff && diff < MAX_LOW_PW) {
176
      // 0 already in bits
177
    } else if (MIN_HIGH_PW < diff && diff < MAX_HIGH_PW) {
178
      // add 1 to bits
179
      rx->bits |= 1;
180
    } else {
181
      // error, start from beginning
182
      rx->bits_rx = 0;
183
      rx->bits = 0;
184
    }
185
    if (rx->bits_rx == 16) {
186
      // finished!
187
      if ((rx->bits & 3) == 2) { // expansion and check bits
188
        rx->data = (rx->bits >> 2) & 0xff;
189
        rx->address = (rx->bits >> 10) & 0x1f;
190
        rx->new_data = 1;
171
    // this should be in units of microseconds
172
    int now = TCNT1 * (F_CPU / 1000000 / 8);
173
    
174
    if (rx->count) {
175
      int diff = now - rx->last_time;
176
      rx->bits <<= 1;
177
      if (MIN_LOW_PW < diff && diff < MAX_LOW_PW) {
178
        // 0 already in bits
179
      } else if (MIN_HIGH_PW < diff && diff < MAX_HIGH_PW) {
180
        // add 1 to bits
181
        rx->bits |= 1;
182
      } else {
183
        // error, start from beginning
184
        rx->count = 0;
185
        rx->bits = 0;
186
      }
187
      if (rx->count == 16) {
188
        // finished!
189
        //if ((rx->bits & 3) == 2) { // expansion and check bits
190
          rx->data = (rx->bits >> 2) & 0xff;
191
          rx->address = (rx->bits >> 10) & 0x1f;
192
          rx->new_data = 1;
193
        //}
194
        rx->count = 0;
195
        rx->bits = 0;
191 196
      }
192
      rx->bits_rx = 0;
193
      rx->bits = 0;
194 197
    }
195
  } else {
196
    // TODO check 320us? or have 320us timeout on rising edge?
198
    
199
    rx->count++;
200
    rx->last_time = now;
197 201
  }
198 202
}
199 203

  

Also available in: Unified diff