![](/media/cache/profiles/1006ff12c465532f8c574aeaa4461b16.50x50_q85.jpg)
This is a example application for StarBoard Orange designed by @logic_star. This example can be drive a CHORO Q HYBRID.
Revision 1:03c8bc666945, committed 2010-09-20
- Comitter:
- shintamainjp
- Date:
- Mon Sep 20 02:09:54 2010 +0000
- Parent:
- 0:127b9ca59547
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/appconf.cpp Mon Sep 20 02:09:54 2010 +0000 @@ -0,0 +1,59 @@ +#include "appconf.h" +#include <ConfigFile.h> + +LocalFileSystem fs_local("local"); + +#define CONFIG_FILENAME "/local/SETUP.CFG" + +#define KEY_CHANNEL "Channel" +#define VALUE_CHANNEL_A "A" +#define VALUE_CHANNEL_B "B" +#define VALUE_CHANNEL_C "C" +#define VALUE_CHANNEL_D "D" + +/** + * Initialize a configuration. + * + * @param p A pointer to a configuration structure. + */ +void appconf_init(appconf_t *p) { + /* + * Channel default value is a channel C. + */ + p->channel = ChoroQ::ChC; +} + +/** + * Read a configuration. + * + * @param p A pointer to a configuration structure. + * + * @return Return 0 if read succeed. + */ +int appconf_read(appconf_t *p) { + ConfigFile cfg; + + if (!cfg.read(CONFIG_FILENAME)) { + return -1; + } + + char *key = KEY_CHANNEL; + char value[64]; + if (!cfg.getValue(key, value, sizeof(value))) { + return -2; + } + + if (strcmp(value, VALUE_CHANNEL_A) == 0) { + p->channel = ChoroQ::ChA; + } else if (strcmp(value, VALUE_CHANNEL_B) == 0) { + p->channel = ChoroQ::ChB; + } else if (strcmp(value, VALUE_CHANNEL_C) == 0) { + p->channel = ChoroQ::ChC; + } else if (strcmp(value, VALUE_CHANNEL_D) == 0) { + p->channel = ChoroQ::ChD; + } else { + return -3; + } + + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/appconf.h Mon Sep 20 02:09:54 2010 +0000 @@ -0,0 +1,26 @@ +#ifndef _APPCONF_H_ +#define _APPCONF_H_ + +#include "ChoroQ.h" + +typedef struct { + ChoroQ::Channel channel; +} appconf_t; + +/** + * Initialize a configuration. + * + * @param p A pointer to a configuration structure. + */ +void appconf_init(appconf_t *p); + +/** + * Read a configuration. + * + * @param p A pointer to a configuration structure. + * + * @return Return 0 if read succeed. + */ +int appconf_read(appconf_t *p); + +#endif
--- a/main.cpp Tue Aug 24 11:58:09 2010 +0000 +++ b/main.cpp Mon Sep 20 02:09:54 2010 +0000 @@ -8,21 +8,66 @@ * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems) * http://shinta.main.jp/ */ - + +/* + * Connection map. + * + * +---+----------------+---------+ + * |Pin|Target |Direction| + * +---+----------------+---------+ + * |p21|IR transmitter |OUT | + * +---+----------------+---------+ + */ + +/* + * Include files. + */ + #include <mbed.h> #include <algorithm> -#include <RemoteIR.h> -#include <TransmitterIR.h> +#include <ChoroQ.h> #include <I2CConfig.h> #include <WiiNunchuckReader.h> #include <TextLCD.h> -#include "ChoroQ.h" +#include "appconf.h" + +/* + * Objects. + */ WiiNunchuckReader wn(I2CPort_A::SDA, I2CPort_A::SCL); -ChoroQ cq(p21, ChoroQ::ChC); +ChoroQ cq(p21); TextLCD lcd(p24, p26, p27, p28, p29, p30); BusOut led(LED4, LED3, LED2, LED1); +/** + * Display a splash screen. + */ +void splash(void) { + lcd.cls(); + lcd.locate(0, 0); + lcd.printf("StarBoard Orange"); + lcd.locate(0, 1); + lcd.printf("mbed NXP LPC1768"); + wait(3); + + lcd.cls(); + lcd.locate(0, 0); + lcd.printf("Example app No.3"); + lcd.locate(0, 1); + lcd.printf(" CHORO Q HYBRID "); + wait(3); +} + +/** + * Get an action from a coordinate. + * + * @param x X axis. + * @param y Y axis. + * @param dash State of dash. + * + * @return An action. + */ ChoroQ::Action getAction(const int x, const int y, bool dash) { static const int MAX_X = 200; static const int MIN_X = 20; @@ -37,6 +82,10 @@ return ChoroQ::Stop; } + if ((x == 0) && (y == 0)) { + return ChoroQ::Stop; + } + if (std::abs(px) < 10) { if (py < 0) { if (dash) { @@ -54,26 +103,10 @@ } if (std::abs(py) < 10) { if (px < -20) { -#if 0 return ChoroQ::Left; -#else - if (dash) { - return ChoroQ::UpLeftDash; - } else { - return ChoroQ::UpLeft; - } -#endif } if (20 < px) { -#if 0 return ChoroQ::Right; -#else - if (dash) { - return ChoroQ::UpRightDash; - } else { - return ChoroQ::UpRight; - } -#endif } } if ((10 < px) && (10 < py)) { @@ -107,72 +140,90 @@ return ChoroQ::Stop; } +/** + * Entry point. + */ int main() { + /* + * Splash. + */ + splash(); lcd.cls(); + + /* + * Setup a configuration. + */ + appconf_t conf; + appconf_init(&conf); + appconf_read(&conf); + + /* + * Application loop. + */ while (true) { wn.RequestRead(); ChoroQ::Action ac = getAction(wn.getJoyX(), wn.getJoyY(), (wn.getButtonZ() == 1) ? true : false); - cq.execute(ac); + cq.execute(conf.channel, ac); lcd.locate(0, 0); - lcd.printf("(%3d,%3d) %-6.6s", wn.getJoyX(), wn.getJoyY(), ((wn.getButtonZ() == 1) ? "-Dash-" : " ")); + lcd.printf("JS :(%3d,%3d) %s", wn.getJoyX(), wn.getJoyY(), ((wn.getButtonZ() == 1) ? "D" : " ")); - lcd.locate(0, 1); + char *stattext = ""; switch (ac) { case ChoroQ::Undef: - lcd.printf("%-16.16s", "Undef"); + stattext = "Undef"; break; case ChoroQ::Up: - lcd.printf("%-16.16s", "Up"); + stattext = "Up"; break; case ChoroQ::Down: - lcd.printf("%-16.16s", "Down"); + stattext = "Down"; break; case ChoroQ::Left: - lcd.printf("%-16.16s", "Left"); + stattext = "Left"; break; case ChoroQ::Right: - lcd.printf("%-16.16s", "Right"); + stattext = "Right"; break; case ChoroQ::UpDash: - lcd.printf("%-16.16s", "UpDash"); + stattext = "Up +D"; break; case ChoroQ::UpLeft: - lcd.printf("%-16.16s", "UpLeft"); + stattext = "UpLeft"; break; case ChoroQ::UpRight: - lcd.printf("%-16.16s", "UpRight"); + stattext = "UpRight"; break; case ChoroQ::UpRightDash: - lcd.printf("%-16.16s", "UpRightDash"); + stattext = "UpRight +D"; break; case ChoroQ::UpLeftDash: - lcd.printf("%-16.16s", "UpLeftDash"); + stattext = "UpLeft +D"; break; case ChoroQ::DownLeft: - lcd.printf("%-16.16s", "DownLeft"); + stattext = "DownLeft"; break; case ChoroQ::DownRight: - lcd.printf("%-16.16s", "DownRight"); + stattext = "DownRight"; break; case ChoroQ::DownDash: - lcd.printf("%-16.16s", "DownDash"); + stattext = "Down +D"; break; case ChoroQ::DownLeftDash: - lcd.printf("%-16.16s", "DownLeftDash"); + stattext = "DownLeft +D"; break; case ChoroQ::DownRightDash: - lcd.printf("%-16.16s", "DownRightDash"); + stattext = "DownRight+D"; break; case ChoroQ::Stop: - lcd.printf("%-16.16s", "Stop"); + stattext = "Stop"; break; } + lcd.locate(0, 1); + lcd.printf("Ch.%c:%-11.11s", 'A' + (int)conf.channel, stattext); wait_ms(50); led = led + 1; } - - return EXIT_SUCCESS; }
--- a/mbed.bld Tue Aug 24 11:58:09 2010 +0000 +++ b/mbed.bld Mon Sep 20 02:09:54 2010 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/9114680c05da +http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e
--- a/mylib/ChoroQ.cpp Tue Aug 24 11:58:09 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ -#include "ChoroQ.h" - -const ChoroQ::action_t ChoroQ::list[] = { - {Undef, "\x00"}, - {Up, "\x20"}, - {Down, "\x10"}, - {Left, "\x30"}, - {Right, "\x08"}, - {UpDash, "\x28"}, - {UpLeft, "\x18"}, - {UpRight, "\x38"}, - {UpLeftDash, "\x04"}, - {UpRightDash, "\x24"}, - {DownLeft, "\x14"}, - {DownRight, "\x34"}, - {DownDash, "\x0C"}, - {DownLeftDash, "\x2C"}, - {DownRightDash, "\x1C"}, - {Stop, "\x3C"} -}; - -ChoroQ::ChoroQ(PinName pin, Channel ch) : irtx(pin) { - switch (ch) { - case ChA: - channel = CH_A; - break; - case ChB: - channel = CH_B; - break; - case ChC: - channel = CH_C; - break; - case ChD: - channel = CH_D; - break; - } -} - -ChoroQ::~ChoroQ() { -} - -void ChoroQ::execute(Action action) { - uint8_t *command = getCommand(action); - if (command != NULL) { - uint8_t buf[1]; - buf[0] = command[0] | channel; - irtx.setData(RemoteIR::SONY, buf, 6); - } -} - -uint8_t *ChoroQ::getCommand(Action action) { - const int n = sizeof(list) / sizeof(list[0]); - for (int i = 0; i < n; i++) { - const action_t *p = &list[i]; - if (p->action == action) { - return (uint8_t *)p->command; - } - } - return NULL; -}
--- a/mylib/ChoroQ.h Tue Aug 24 11:58:09 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ -#ifndef _CHORO_Q_H_ -#define _CHORO_Q_H_ - -#include <mbed.h> -#include <inttypes.h> -#include <TransmitterIR.h> - -class ChoroQ { -public: - - typedef enum { - ChA, - ChB, - ChC, - ChD - } Channel; - - ChoroQ(PinName pin, Channel ch); - ~ChoroQ(); - - typedef enum { - Undef, - Up, - Down, - Left, - Right, - UpDash, - UpLeft, - UpRight, - UpRightDash, - UpLeftDash, - DownLeft, - DownRight, - DownDash, - DownLeftDash, - DownRightDash, - Stop - } Action; - - void execute(Action action); - -private: - typedef struct { - Action action; - char *command; - } action_t; - - static const int CH_A = 0x00; - static const int CH_B = 0x02; - static const int CH_C = 0x01; - static const int CH_D = 0x03; - static const action_t list[]; - - TransmitterIR irtx; - int channel; - - uint8_t *getCommand(Action action); -}; - -#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mylib/ChoroQ.lib Mon Sep 20 02:09:54 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/shintamainjp/code/ChoroQ/#97921a2adf78
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mylib/ConfigFile.lib Mon Sep 20 02:09:54 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/shintamainjp/code/ConfigFile/#f6ceafabe9f8
--- a/mylib/RemoteIR.lib Tue Aug 24 11:58:09 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/shintamainjp/code/RemoteIR/#268cc2ab63bd