summaryrefslogtreecommitdiff
path: root/n/es-2006/src/main.c
blob: e95db1419dbf95b59530fb073104422fe3d55ed5 (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
/* main.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 "common.h"
#include "io.h"
#include "modules/uart/uart.h"
#include "modules/proto/proto.h"
#include "modules/utils/utils.h"
#include "modules/utils/byte.h"

#include "timer_main.h"	/* main timer */
#include "sensor_rvb.h" /* RVB sensors management */
#include "es_config.h"  /* timer/counter 1 */


/* Statistics for RVB sensors */
uint8_t sensor_rvb_stat_enable[RVB_MAX_SENSOR] = { 0 };
uint8_t sensor_rvb_stats[RVB_MAX_SENSOR] = { 0 };

/** Call when we receive some data from proto (uart) */
void
proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
{
    uint8_t compt;
    uint16_t temp_16b;
    /* This macro combine command and size in one integer. */
#define c(cmd, size) (cmd << 8 | size)
    switch (c (cmd, size))
      {
	/* Reset */
      case c ('z', 0):
	utils_reset ();
	break;
	/* RVB sensors stats */
      case c ('S', 3):
	temp_16b = v8_to_v16 (args[0], args[1]);
	for (compt = 0; compt < RVB_MAX_SENSOR; compt++)
	    if (temp_16b & _BV(compt))
		sensor_rvb_stat_enable[compt] = sensor_rvb_stats[compt] =
		    args[2];
	break;
//       case c ('s', 2):
// 	sensor_rvb_watch (args[0], args[1]);
// 	break;
	/* Unknown commands */
      default:
	proto_send0 ('?');
	return;
      }
    /* When no error acknoledge. */
    proto_send (cmd, size, args);
#undef c
}

int
main (void)
{
    uint8_t compt;

    /* Serial port */
    uart0_init ();
    /* Main timer init */
    timer_main_init ();
    /* Init timer/counter 1 for RVB & PWM
     * /!\ Must be called before RVB/PWM init ! */
    timer1_init ();
    /* Init RVB sensors system */
    sensor_rvb_init ();

    /* Enable interrupts */
    sei ();

    /* We are ready ! */
    proto_send0 ('z');

    while (1)
      {
	/* Wait 4.44 ms */
	timer_main_wait ();
	
	/* RVB Sensors stats */
	for (compt = 0; compt < RVB_MAX_SENSOR; compt++)
	    if (sensor_rvb_stat_enable[compt] && !--sensor_rvb_stats[compt])
	  {
	    /* Re init stats system for this sensor */
	    sensor_rvb_stats[compt] = sensor_rvb_stat_enable[compt];
	    proto_send4w ('S', sensor_rvb_values[compt][0],
			  sensor_rvb_values[compt][1], sensor_rvb_values[compt][2],
			  sensor_rvb_values[compt][3]);
	  }
	/* Update RVB sensors data. */
	sensor_rvb_start_capture ();

	/* Get data for serial port */
	while (uart0_poll ())
	    proto_accept (uart0_getc ());
      }
}