SmartWheels self-driving race car. Designed for NXP Cup. Uses FRDM-KL25Z, area-scan camera, and simple image processing to detect and navigate any NXP spec track.

Dependencies:   TSI USBDevice mbed-dev

Fork of SmartWheels by haofan Zheng

Committer:
hazheng
Date:
Tue Apr 18 20:51:06 2017 +0000
Branch:
Drift
Revision:
82:992ba6f31e24
Parent:
81:32bd7a25a699
Finished state switching.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hazheng 80:c85cb93713b3 1 #include "StateManager.h"
hazheng 80:c85cb93713b3 2 #include "States.h"
hazheng 80:c85cb93713b3 3 #include "ArduTouch.h"
hazheng 80:c85cb93713b3 4
hazheng 82:992ba6f31e24 5 #include "ArduUTFT.h"
hazheng 82:992ba6f31e24 6
hazheng 82:992ba6f31e24 7 #include "StandbyState.h"
hazheng 82:992ba6f31e24 8 #include "RunningState.h"
hazheng 82:992ba6f31e24 9 #include "TestingState.h"
hazheng 82:992ba6f31e24 10
hazheng 80:c85cb93713b3 11
hazheng 80:c85cb93713b3 12 static States* stat_manger_curr_stat = NULL;
hazheng 80:c85cb93713b3 13
hazheng 80:c85cb93713b3 14 void state_manager_touch_pos(int16_t x, int16_t y)
hazheng 80:c85cb93713b3 15 {
hazheng 80:c85cb93713b3 16 stat_manger_curr_stat->TouchPosCallback(x, y);
hazheng 80:c85cb93713b3 17 }
hazheng 80:c85cb93713b3 18
hazheng 80:c85cb93713b3 19 void state_manager_touch_irq()
hazheng 80:c85cb93713b3 20 {
hazheng 80:c85cb93713b3 21 stat_manger_curr_stat->TouchIrqCallback();
hazheng 80:c85cb93713b3 22 }
hazheng 80:c85cb93713b3 23
hazheng 80:c85cb93713b3 24 void state_manager_set_current_state(States* stat)
hazheng 80:c85cb93713b3 25 {
hazheng 80:c85cb93713b3 26 stat_manger_curr_stat = stat;
hazheng 80:c85cb93713b3 27 if(stat_manger_curr_stat)
hazheng 80:c85cb93713b3 28 {
hazheng 80:c85cb93713b3 29 if(stat_manger_curr_stat->HasTouchPosFunction())
hazheng 80:c85cb93713b3 30 {
hazheng 80:c85cb93713b3 31 ardu_touch_set_pos_function(&state_manager_touch_pos);
hazheng 80:c85cb93713b3 32 }
hazheng 80:c85cb93713b3 33
hazheng 80:c85cb93713b3 34 if(stat_manger_curr_stat->HasTouchIrqFunction())
hazheng 80:c85cb93713b3 35 {
hazheng 80:c85cb93713b3 36 ardu_touch_set_irq_function(&state_manager_touch_irq);
hazheng 80:c85cb93713b3 37 }
hazheng 81:32bd7a25a699 38
hazheng 81:32bd7a25a699 39 stat_manger_curr_stat->DrawUserInterface();
hazheng 80:c85cb93713b3 40 }
hazheng 80:c85cb93713b3 41 }
hazheng 80:c85cb93713b3 42
hazheng 80:c85cb93713b3 43 void state_manager_clear_current_state()
hazheng 80:c85cb93713b3 44 {
hazheng 80:c85cb93713b3 45 if(stat_manger_curr_stat)
hazheng 80:c85cb93713b3 46 {
hazheng 82:992ba6f31e24 47 //delete stat_manger_curr_stat;
hazheng 80:c85cb93713b3 48 stat_manger_curr_stat = NULL;
hazheng 80:c85cb93713b3 49 }
hazheng 80:c85cb93713b3 50
hazheng 80:c85cb93713b3 51 ardu_touch_set_irq_function(NULL);
hazheng 80:c85cb93713b3 52 ardu_touch_set_pos_function(NULL);
hazheng 80:c85cb93713b3 53 }
hazheng 80:c85cb93713b3 54
hazheng 80:c85cb93713b3 55 void state_manager_update(float deltaTime)
hazheng 80:c85cb93713b3 56 {
hazheng 80:c85cb93713b3 57 if(stat_manger_curr_stat)
hazheng 80:c85cb93713b3 58 {
hazheng 80:c85cb93713b3 59 stat_manger_curr_stat->Update(deltaTime);
hazheng 80:c85cb93713b3 60 }
hazheng 80:c85cb93713b3 61 }
hazheng 80:c85cb93713b3 62
hazheng 82:992ba6f31e24 63 void state_manager_switch_state(uint8_t state)
hazheng 82:992ba6f31e24 64 {
hazheng 82:992ba6f31e24 65 static StandbyState standby;
hazheng 82:992ba6f31e24 66 static RunningState running;
hazheng 82:992ba6f31e24 67 static TestingState testing;
hazheng 82:992ba6f31e24 68
hazheng 82:992ba6f31e24 69 state_manager_clear_current_state();
hazheng 82:992ba6f31e24 70
hazheng 82:992ba6f31e24 71 //ardu_utft_print("X", 20, 20);
hazheng 82:992ba6f31e24 72 switch(state)
hazheng 82:992ba6f31e24 73 {
hazheng 82:992ba6f31e24 74 case STANDBY_STATE:
hazheng 82:992ba6f31e24 75 state_manager_set_current_state(&standby);
hazheng 82:992ba6f31e24 76 break;
hazheng 82:992ba6f31e24 77 case RUNNING_STATE:
hazheng 82:992ba6f31e24 78 state_manager_set_current_state(&running);
hazheng 82:992ba6f31e24 79 break;
hazheng 82:992ba6f31e24 80 case TESTING_STATE:
hazheng 82:992ba6f31e24 81 state_manager_set_current_state(&testing);
hazheng 82:992ba6f31e24 82 break;
hazheng 82:992ba6f31e24 83 default:
hazheng 82:992ba6f31e24 84
hazheng 82:992ba6f31e24 85 break;
hazheng 82:992ba6f31e24 86 }
hazheng 82:992ba6f31e24 87 //ardu_utft_print("X", 30, 30);
hazheng 80:c85cb93713b3 88 }