summaryrefslogtreecommitdiff
path: root/n/es-2006/src/rvb_sensor.c
blob: 0315e1c35a1a3d9a51e4d341f02a1164642947e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* rvb_sensor.c */
/* es - Input/Output general purpose board. {{{
 *
 * Copyright (C) 2006 Dufour J�r�my
 *
 * Robot APB Team/Efrei 2004.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "rvb_sensor.h"

#include "io.h"
#include "modules/utils/utils.h" /* regv */
#include "modules/uart/uart.h"   /* */

#define S1RVB 7
#define S0RVB 6
/* Wait time between IO change in ns */
#define RVB_SENSOR_DELAY_IO 125

/** Internal counter */
static volatile uint16_t sensor_value_;
/** Sensor state flag */
static volatile uint8_t sensor_state_;
/** Sensor used actually */
volatile uint8_t sensor_number_;
/** Color of the actual sensor */
volatile uint8_t sensor_color_;
/** Number of overflow interruption since last edge change */
uint8_t sensor_overflow_count_;

volatile uint16_t rvb_sensors_values [9][5];

uint8_t sensor_disable = 0;

/** Update everything for you. */
static void
rvb_sensors_update_state (void)
{
//     uart0_putc ('u');
    if (sensor_state_ == 1)
      {
	/* Disable sensors */
	rvb_sensors_disable ();
	/* Wait a little */
	utils_delay_ns (RVB_SENSOR_DELAY_IO);
	switch (++sensor_number_)
	  {
	  case 1:
	  case 2:
	  case 3:
	  case 4:
	  case 5:
	  case 6:
	  case 7:
// 	    PORTC = ~_BV(sensor_number_);
// 	    break;
	  case 8:
// 	    PORTD &= ~_BV(6);
// 	    break;
	  case 9:
// 	    PORTC = ~_BV(0);
// 	    break;
	    PORTD &= ~_BV(6);
	    break;
	  default:
	    /* We are at the end */
	    sensor_state_ = 0;
	    return;
	  }
	/* Wait for output of sensor to be ready */
	utils_delay_ns (RVB_SENSOR_DELAY_IO);
	/* Start with first color */
	sensor_color_ = 0;
      }
    /* Select the color */
    PORTF = (PORTF & ~0xC0) | (sensor_color_ << 6);
    /* Enable interrupt for IC1 */
    TIMSK |= _BV (TICIE1);
//     uart0_putc ('e');
    // XXX
//     TIMSK |= _BV (TOIE1);
}

/** Counter 1 overflow. */
SIGNAL (SIG_OVERFLOW1)
{
    // XXX For the moment.
    if (++sensor_overflow_count_ == 2)
      {
	sensor_state_ = 1;
	rvb_sensors_update_state ();
      }
}

/** Interrupt on falling edge for Input Capture 1. */
SIGNAL (SIG_INPUT_CAPTURE1)
{
    switch (sensor_state_)
      {
      case 1:
	/* Ignore this capture */
	sensor_state_++;
	break;
      case 2:
	/* Save capture start */
	sensor_value_ = ICR1;
	/* New capture */
	sensor_overflow_count_ = 0;
	/* Enable interruption for overflow */
	sensor_state_++;
// 	TIMSK |= _BV (TOIE1);
	break;
      case 3:
	/* Disable this interruption */
	TIMSK &= ~_BV (TICIE1);
	/* Disable interruption for overflow */
	TIMSK &= ~_BV (TOIE1);
	/* Compute value */
	//TODO
	sensor_value_ = ICR1 - sensor_value_;
	/* Save */
	rvb_sensors_values[sensor_number_ - 1][sensor_color_] = sensor_value_;
	/* Next color */
	sensor_color_++;
	/* If no more colour, next sensor */
	if (sensor_color_ == 4)
	    sensor_state_ = 1;
	rvb_sensors_update_state ();
	break;
      }
}

/** Start updating RVBC values for sensors. */
void
rvb_sensors_start_update (void)
{
//     uart0_putc ('i');
    /* Are we already getting a sensor ? */
    if (!sensor_state_ && !sensor_disable)
      {
	sensor_state_ = 1;
	/* Start with sensor one */
	sensor_number_ = 0;
	/* Select it and launch capture */
	rvb_sensors_update_state ();
      }
}

/** Initialisation. */
void
rvb_sensors_init (void)
{
    /* Disable sensors */
    rvb_sensors_disable ();
    /* Put enable ports in output mode */
    DDRC = 0xff;
    DDRD |= _BV(6);
    /* Put color selector mode in output mode */
    DDRF |= _BV(S0RVB) | _BV(S1RVB);
    /* Initialisation of counter : 
     * Noise canceler, 1 for prescaling and that's all */
    TCCR1B |= _BV(ICNC1) | _BV(CS10);
    /* Nothing to do for the moment */
    sensor_state_ = 0;
}

void
rvb_sensor_watch (uint8_t sensor_num, uint8_t sensor_col)
{
    /* Disable this interruption */
    TIMSK &= ~_BV (TICIE1);
    /* Disable interruption for overflow */
    TIMSK &= ~_BV (TOIE1);
    sensor_disable = 1;
    /* Disable sensors */
    rvb_sensors_disable ();
    /* Wait a little */
    utils_delay_ns (RVB_SENSOR_DELAY_IO);
    switch (++sensor_num)
      {
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
	PORTC = ~_BV(sensor_num);
	break;
      case 8:
	PORTD &= ~_BV(6);
	break;
      case 9:
	PORTC = ~_BV(0);
	break;
      }
    /* Wait for output of sensor to be ready */
    utils_delay_ns (RVB_SENSOR_DELAY_IO);
    /* Select the color */
    PORTF = (PORTF & ~0xC0) | (sensor_col << 6);
}