2 * Copyright Droids Corporation (2009)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Revision : $Id: state.c,v 1.5 2009-11-08 17:25:00 zer0 Exp $
26 #include <aversive/wait.h>
27 #include <aversive/error.h>
32 #include <encoders_spi.h>
35 #include <scheduler.h>
36 #include <clock_time.h>
40 #include <control_system_manager.h>
41 #include <blocking_detection_manager.h>
46 #include "../common/i2c_commands.h"
53 #define STMCH_DEBUG(args...) DEBUG(E_USER_ST_MACH, args)
54 #define STMCH_NOTICE(args...) NOTICE(E_USER_ST_MACH, args)
55 #define STMCH_ERROR(args...) ERROR(E_USER_ST_MACH, args)
57 /* shorter aliases for this file */
58 #define INIT I2C_COBBOARD_MODE_INIT
59 #define MANUAL I2C_COBBOARD_MODE_MANUAL
60 #define HARVEST I2C_COBBOARD_MODE_HARVEST
61 #define EXIT I2C_COBBOARD_MODE_EXIT
63 static struct i2c_cmd_cobboard_set_mode mainboard_command;
64 static struct vt100 local_vt100;
65 static volatile uint8_t prev_state;
66 static volatile uint8_t changed = 0;
68 uint8_t state_debug = 0;
70 void state_dump_sensors(void)
72 STMCH_DEBUG("TODO\n");
75 void state_debug_wait_key_pressed(void)
79 printf_P(PSTR("press a key\r\n"));
80 while(!cmdline_keypressed());
83 /* set a new state, return 0 on success */
84 int8_t state_set_mode(struct i2c_cmd_cobboard_set_mode *cmd)
87 prev_state = mainboard_command.mode;
88 memcpy(&mainboard_command, cmd, sizeof(mainboard_command));
89 STMCH_DEBUG("%s mode=%d", __FUNCTION__, mainboard_command.mode);
93 /* check that state is the one in parameter and that state did not
95 uint8_t state_check(uint8_t mode)
98 if (mode != mainboard_command.mode)
104 /* force quit when CTRL-C is typed */
105 c = cmdline_getchar();
108 if (vt100_parser(&local_vt100, c) == KEY_CTRL_C) {
109 mainboard_command.mode = EXIT;
115 uint8_t state_get_mode(void)
117 return mainboard_command.mode;
120 /* manual mode, arm position is sent from mainboard */
121 static void state_do_manual(void)
123 if (!state_check(MANUAL))
125 STMCH_DEBUG("%s mode=%d", __FUNCTION__, state_get_mode());
126 while (state_check(MANUAL));
130 static void state_do_init(void)
132 if (!state_check(INIT))
135 STMCH_DEBUG("%s mode=%d", __FUNCTION__, state_get_mode());
136 while (state_check(INIT));
139 /* harvest columns elts from area */
140 static void state_do_harvest(void)
142 if (!state_check(HARVEST))
144 STMCH_DEBUG("%s mode=%d", __FUNCTION__, state_get_mode());
145 while (state_check(HARVEST));
147 /* main state machine */
148 void state_machine(void)
150 while (state_get_mode() != EXIT) {
158 void state_init(void)
160 vt100_init(&local_vt100);
161 mainboard_command.mode = HARVEST;
162 cobboard.cob_count = 0;