void fsm_set_state(state_t new_state) { /* only do this if the FSM has been locked! */ if( fsm_mutex == MUTEX_LOCKED ) { state = new_state; switch( state ) { case STATE_RESET: case STATE_1: default: /* Initialize the LEDs */ LED_init(); /* Initialize the USART2 for x-over internet communication */ USART2_init(state1_USART2_callback_fn); /* Initialize the USART3 for x-over internet communication */ USART3_init(state1_USART3_callback_fn); /* Turn on just the blue LED */ LED_update( LED_ORANGE_OFF | LED_RED_OFF | LED_BLUE_ON | LED_GREEN_OFF ); break; case STATE_2: /* Turn on the orange LED only */ LED_update( LED_ORANGE_ON | LED_RED_OFF | LED_BLUE_OFF | LED_GREEN_OFF ); //Set up the USART interrupts to buffer the received data from the PING USART3_init(state2_USART3_callback_fn); USART2_init(state2_USART2_callback_fn); //Send a ping message to the server Wifly_Send_Ping(); //Start up a 1 second timer - wait 1 second then print message_reported = 0; systick_init(receive_message_systick_callback); set_systick_time(1); break; } } }
void main(void) { /* Set up the USART2 9600-8N1 and to call USART2_callback_fn when new data arrives */ USART2_init(USART2_callback_fn); /* Set up the USART3 9600-8N1 and to call USART2_callback_fn when new data arrives */ USART3_init(USART3_callback_fn); /* Set up and initialize the LEDs. */ LED_init(); /* Configure user pushbutton and call pushbutton_callback_fn when button press-released */ userbutton_init(userbutton_callback_fn); /* Initialize the systick timer with its callback function */ systick_init(systick_callback_fn); set_systick_disabled(); /* initialize the finite state machine */ fsm_init(); //Test ping the server Wifly_Send_Ping(); /* Enable interrupts - do this after initializing the system */ __asm (" cpsie i \n" ); //Put the wifi module into command mode // USART3_putchar('$'); // USART3_putchar('$'); // USART3_putchar('$'); // USART2_putchar('$'); // USART2_putchar('$'); // USART2_putchar('$');// USART2_putstr("$$$\n\r\0"); /* Wait here forever - everything is now interrupt driven */ while(1) { ;;; } }
int main() { int which_to_update = 6; // start greater than 5 so we get data uint32_t data[5]; // Array to hold ADC data // Initialize all the things LED_init(); systick_init(400000); // Timer goes off 10 times per second USART2_init(); USART3_init(); button_init(); ADC_init(); servo_init(); DMA_init(); /* Enable interrupts */ __asm (" cpsie i \n" ); /* Main program loop */ while(1) { // State specific behavior (every time) switch (mode_state) { case CONFIGURE_S: // Don't do anything break; case COMMAND_S: { /* Send in two cases: * a) we're not waiting for a packet * b) the update flag is set (every second, because sometimes packets get dropped * and we don't want to wait forever) * * When we send a byte, we'll send whichever is next in the sequence (the current one * is stored in the local variable (in main) which_to_update). When it goes over 5, * we read in all the data for the next round of packets. */ if (!waiting_to_recv_packet || send_update_f) { // if (send_update_f) { if (which_to_update > 5) { // finished updating ADC_read(data); which_to_update = 1; } // We will be waiting for a packet back, so set this ahead of time waiting_to_recv_packet=1; // A new packet will be inbound, so reset the offset to 0 (in case it got messed up before) recv_offset = 0; update_server(which_to_update, data); which_to_update++; send_update_f = 0; } break; } case CLIENT_S: { /* * Update the servos on the high tick of this flag. That is set in systick, and happens 10 times * per second */ if (update_servos_from_server_f) { // We're about to receive a response packet, so we reset the recv_offset to 0 recv_offset = 0; waiting_to_recv_packet = 1; update_servos(); update_servos_from_server_f = 0; } break; } default: break; } // Every time, regardless of state: // If we received a packet, print it if (received_new_packet) { // switch (recv_msg.pingmsg.type) { // case TYPE_PING: // print_string("[PING,id="); // printUnsignedDecimal(recv_msg.pingmsg.id); // print_string("]\n"); // break; // case TYPE_UPDATE: // print_string("[UPDATE,id="); // printUnsignedDecimal(recv_msg.respmsg.id); // print_string(",average="); // printUnsignedDecimal(recv_msg.respmsg.average); // print_string(",{"); // for (int i=0; i<CLASS_SIZE_MAX; i++) { // print_string(" "); // printUnsignedDecimal(recv_msg.respmsg.values[i]); // } // print_string("}]\n\r"); // break; // default: // break; // } // Reset the flag received_new_packet = 0; // If we're in client mode, set the servo values to those from the server if (mode_state == CLIENT_S) { if (recv_msg.respmsg.type == TYPE_UPDATE) { for (int i=1; i<=5; i++) servo_update(i, recv_msg.respmsg.values[i]); } } } // After switching states, update leds if (update_leds_f) { switch (mode_state) { case CONFIGURE_S: LED_update(LED_BLUE_ON|LED_ORANGE_OFF); // Configuration: // $$$ (escape sequence) // set ip dhcp 1 (get IP address with dhcp) // set ip host 172.16.1.10 (set remote IP) // set ip remote 8004 // set wlan join 1 (try to connect to stored access point) // set wlan auth 4 (set to WPA2-PSK) // set wlan phrase ENGS62wifi // set wlan ssid ENGS62 // save // reboot break; case CLIENT_S: LED_update(LED_BLUE_OFF|LED_ORANGE_ON); break; case COMMAND_S: LED_update(LED_BLUE_ON|LED_ORANGE_ON); waiting_to_recv_packet = 0; break; } update_leds_f = 0; } // If in debug mode, print ADC data to the console if (DEBUG && test_flag) { test_flag = 0; uint32_t data[5]; // Initialize the data array to 0 for clarity for (int i=0; i<5; i++) { data[i] = 0; } ADC_read(data); for (int i=0; i<5; i++) { printUnsignedDecimal((uint16_t)data[i]); print_string("\n"); print_string("\r"); } print_string("-----------\n"); } } /* We'll never reach this line */ return 0; }
void fsm_set_state(state_t new_state) { /* only do this if the FSM has been locked! */ if( fsm_mutex == MUTEX_LOCKED ) { state = new_state; switch( state ) { case STATE_RESET: case STATE_1: default: /* Initialize the LEDs */ LED_init(); /* Initialize the USART2 for x-over internet communication */ USART2_init(state1_USART2_callback_fn); /* Initialize the USART3 for x-over internet communication */ USART3_init(state1_USART3_callback_fn); /* Turn on just the blue LED */ LED_update( LED_ORANGE_OFF | LED_RED_OFF | LED_BLUE_ON | LED_GREEN_OFF ); break; case STATE_2: /* Turn on the orange LED only */ LED_update( LED_ORANGE_ON | LED_RED_OFF | LED_BLUE_OFF | LED_GREEN_OFF ); //Set up the USART interrupts to buffer the received data from the PING USART3_init(state2_USART3_callback_fn); USART2_init(state2_USART2_callback_fn); //Send a ping message to the server Wifly_Send_Ping(); //Start up a 1 second timer - wait 1 second then print message_reported = 0; systick_init(receive_message_systick_callback); set_systick_time(1); break; case STATE_INIT: //Set up the USART interrupts to handle the initialization correctly USART3_init(state_INIT_USART3_callback_fn); USART2_init(state_INIT_USART2_callback_fn); // USART2_putstr("Initializing!\n\r\0"); init_input = '\0'; USART3_putstr("EXIT\n\r\0"); // out_of_CMD = 1; // break; case STATE_INIT_a: // USART2_putstr("$$$\n\r\0"); init_input = '\0'; USART3_putstr("$$$\n\r\0"); break; case STATE_INIT_b: USART2_putstr("set wlan join 1\n\r\0"); init_input = '\0'; USART3_putstr("set wlan join 1\n\r\0"); break; case STATE_INIT_c: USART2_putstr("set wlan auth 4\n\r\0"); init_input = '\0'; USART3_putstr("set wlan auth 4\n\r\0"); break; case STATE_INIT_d: USART2_putstr("set wlan ssid ENGS62\n\r\0"); init_input = '\0'; USART3_putstr("set wlan ssid ENGS62\n\r\0"); break; case STATE_INIT_e: USART2_putstr("set wlan phrase Engs62connect\n\r\0"); init_input = '\0'; USART3_putstr("set wlan phrase Engs62connect\n\r\0"); break; case STATE_INIT_f: USART2_putstr("set ip dhcp 1"); init_input = '\0'; USART3_putstr("set ip dhcp 1"); break; case STATE_INIT_g: USART2_putstr("set ip addr 129.170.66.33\n\r\0"); init_input = '\0'; USART3_putstr("set ip addr 129.170.66.33\n\r\0"); break; case STATE_INIT_h: USART2_putstr("set ip remote 8880\n\r\0"); init_input = '\0'; USART3_putstr("set ip remote 8880\n\r\0"); break; case STATE_INIT_i: USART2_putstr("set ip protocol 0\n\r\0"); init_input = '\0'; USART3_putstr("set ip protocol 0\n\r\0"); break; case STATE_INIT_j: USART2_putstr("save\n\r\0"); init_input = '\0'; USART3_putstr("save\n\r\0"); has_been_initialized = 1; break; } } }