#include <vex/digital.hpp>\r
#include <vex/vexspi.h>\r
\r
-int main(void) {\r
+static bool shouldRun = true;\r
+\r
+static char modeMonitorWA[512];\r
+static char autonWA[512];\r
+static char opconWA[512];\r
+\r
+void auton(void *);\r
+void opcon(void *);\r
+\r
+void modeMonitor(void *unused)\r
+{\r
+ (void)unused;\r
+\r
+ static int ut = 1;\r
+ while (1) {\r
+ chThdSleepMilliseconds(16);\r
+ digital::setLed(2, (ut ^= 1));\r
+\r
+ auto state = spi::getState();\r
+ // if not disabled\r
+ if (!(state & 0x80)) {\r
+ shouldRun = true;\r
+\r
+ // if autonomous\r
+ if (state & 0x40) {\r
+ digital::setLed(3, 1);\r
+ chThdCreateStatic(autonWA, 512, NORMALPRIO - 1, auton, nullptr);\r
+ state = 0x40;\r
+ } else {\r
+ chThdCreateStatic(opconWA, 512, NORMALPRIO - 1, opcon, nullptr);\r
+ state = 0;\r
+ }\r
+\r
+ while ((spi::getState() & (0xC0)) == state)\r
+ chThdSleepMilliseconds(16);\r
+\r
+ // TODO stop all\r
+ shouldRun = false;\r
+ } else digital::setLed(3, 0);\r
+ }\r
+}\r
+\r
+void auton(void *unused)\r
+{\r
+ (void)unused;\r
+\r
+ static int led = 1;\r
+\r
+ while (shouldRun) {\r
+ digital::setLed(1, led);\r
+ led ^= 1;\r
+\r
+ chThdSleepMilliseconds(100);\r
+ }\r
+}\r
+\r
+void opcon(void *unused)\r
+{\r
+ (void)unused;\r
+\r
+ while (shouldRun) {\r
+ lcd::flush();\r
+ lcd::printn(0, 0, spi::getJoystick(1).Ch1);\r
+\r
+ chThdSleepMilliseconds(100);\r
+ }\r
+}\r
+\r
+int main(void)\r
+{\r
// init chibios\r
halInit();\r
chSysInit();\r
\r
// init robot\r
- digital::setMode(1, 1);\r
lcd::init();\r
spi::init();\r
+ \r
+ digital::setMode(1, 1);\r
+ digital::setMode(2, 1);\r
+ digital::setMode(3, 1);\r
\r
- int led = 1; // for led toggle\r
+ chThdCreateStatic(modeMonitorWA, 512, NORMALPRIO - 1, modeMonitor, nullptr);\r
\r
while (1) {\r
// update\r
- lcd::flush();\r
spi::update();\r
-\r
- lcd::printn(0, 0, spi::getJoystick(1).Ch1);\r
-\r
- digital::setLed(1, led);\r
- led ^= 1;\r
-\r
- chThdSleepMilliseconds(100);\r
+ chThdSleepMilliseconds(10);\r
}\r
}\r