This is a example application for StarBoard Orange designed by @logic_star. This example can be drive a CHORO Q HYBRID.

Dependencies:   mbed

Committer:
shintamainjp
Date:
Mon Sep 20 02:09:54 2010 +0000
Revision:
1:03c8bc666945
Parent:
0:127b9ca59547

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shintamainjp 0:127b9ca59547 1 /**
shintamainjp 0:127b9ca59547 2 * StarBoard Orange - Example application No.3 (Version 0.0.1)
shintamainjp 0:127b9ca59547 3 * Drive a CHORO Q HYBRID with wii nunchuk
shintamainjp 0:127b9ca59547 4 *
shintamainjp 0:127b9ca59547 5 * See also ... http://mbed.org/users/shintamainjp/notebook/starboard_example3_ja/
shintamainjp 0:127b9ca59547 6 * See also ... http://mbed.org/users/shintamainjp/notebook/starboard_example3_en/
shintamainjp 0:127b9ca59547 7 *
shintamainjp 0:127b9ca59547 8 * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
shintamainjp 0:127b9ca59547 9 * http://shinta.main.jp/
shintamainjp 0:127b9ca59547 10 */
shintamainjp 1:03c8bc666945 11
shintamainjp 1:03c8bc666945 12 /*
shintamainjp 1:03c8bc666945 13 * Connection map.
shintamainjp 1:03c8bc666945 14 *
shintamainjp 1:03c8bc666945 15 * +---+----------------+---------+
shintamainjp 1:03c8bc666945 16 * |Pin|Target |Direction|
shintamainjp 1:03c8bc666945 17 * +---+----------------+---------+
shintamainjp 1:03c8bc666945 18 * |p21|IR transmitter |OUT |
shintamainjp 1:03c8bc666945 19 * +---+----------------+---------+
shintamainjp 1:03c8bc666945 20 */
shintamainjp 1:03c8bc666945 21
shintamainjp 1:03c8bc666945 22 /*
shintamainjp 1:03c8bc666945 23 * Include files.
shintamainjp 1:03c8bc666945 24 */
shintamainjp 1:03c8bc666945 25
shintamainjp 0:127b9ca59547 26 #include <mbed.h>
shintamainjp 0:127b9ca59547 27 #include <algorithm>
shintamainjp 1:03c8bc666945 28 #include <ChoroQ.h>
shintamainjp 0:127b9ca59547 29 #include <I2CConfig.h>
shintamainjp 0:127b9ca59547 30 #include <WiiNunchuckReader.h>
shintamainjp 0:127b9ca59547 31 #include <TextLCD.h>
shintamainjp 1:03c8bc666945 32 #include "appconf.h"
shintamainjp 1:03c8bc666945 33
shintamainjp 1:03c8bc666945 34 /*
shintamainjp 1:03c8bc666945 35 * Objects.
shintamainjp 1:03c8bc666945 36 */
shintamainjp 0:127b9ca59547 37
shintamainjp 0:127b9ca59547 38 WiiNunchuckReader wn(I2CPort_A::SDA, I2CPort_A::SCL);
shintamainjp 1:03c8bc666945 39 ChoroQ cq(p21);
shintamainjp 0:127b9ca59547 40 TextLCD lcd(p24, p26, p27, p28, p29, p30);
shintamainjp 0:127b9ca59547 41 BusOut led(LED4, LED3, LED2, LED1);
shintamainjp 0:127b9ca59547 42
shintamainjp 1:03c8bc666945 43 /**
shintamainjp 1:03c8bc666945 44 * Display a splash screen.
shintamainjp 1:03c8bc666945 45 */
shintamainjp 1:03c8bc666945 46 void splash(void) {
shintamainjp 1:03c8bc666945 47 lcd.cls();
shintamainjp 1:03c8bc666945 48 lcd.locate(0, 0);
shintamainjp 1:03c8bc666945 49 lcd.printf("StarBoard Orange");
shintamainjp 1:03c8bc666945 50 lcd.locate(0, 1);
shintamainjp 1:03c8bc666945 51 lcd.printf("mbed NXP LPC1768");
shintamainjp 1:03c8bc666945 52 wait(3);
shintamainjp 1:03c8bc666945 53
shintamainjp 1:03c8bc666945 54 lcd.cls();
shintamainjp 1:03c8bc666945 55 lcd.locate(0, 0);
shintamainjp 1:03c8bc666945 56 lcd.printf("Example app No.3");
shintamainjp 1:03c8bc666945 57 lcd.locate(0, 1);
shintamainjp 1:03c8bc666945 58 lcd.printf(" CHORO Q HYBRID ");
shintamainjp 1:03c8bc666945 59 wait(3);
shintamainjp 1:03c8bc666945 60 }
shintamainjp 1:03c8bc666945 61
shintamainjp 1:03c8bc666945 62 /**
shintamainjp 1:03c8bc666945 63 * Get an action from a coordinate.
shintamainjp 1:03c8bc666945 64 *
shintamainjp 1:03c8bc666945 65 * @param x X axis.
shintamainjp 1:03c8bc666945 66 * @param y Y axis.
shintamainjp 1:03c8bc666945 67 * @param dash State of dash.
shintamainjp 1:03c8bc666945 68 *
shintamainjp 1:03c8bc666945 69 * @return An action.
shintamainjp 1:03c8bc666945 70 */
shintamainjp 0:127b9ca59547 71 ChoroQ::Action getAction(const int x, const int y, bool dash) {
shintamainjp 0:127b9ca59547 72 static const int MAX_X = 200;
shintamainjp 0:127b9ca59547 73 static const int MIN_X = 20;
shintamainjp 0:127b9ca59547 74 static const int MAX_Y = 200;
shintamainjp 0:127b9ca59547 75 static const int MIN_Y = 20;
shintamainjp 0:127b9ca59547 76 int px = ((x - MIN_X) * 100) / (MAX_X - MIN_X);
shintamainjp 0:127b9ca59547 77 int py = ((y - MIN_Y) * 100) / (MAX_Y - MIN_Y);
shintamainjp 0:127b9ca59547 78 px = std::max(0, std::min(100, px)) - 50; // Range of a value is -50 to +50
shintamainjp 0:127b9ca59547 79 py = std::max(0, std::min(100, py)) - 50; // Range of a value is -50 to +50
shintamainjp 0:127b9ca59547 80
shintamainjp 0:127b9ca59547 81 if ((std::abs(px) <= 10) && (std::abs(py) <= 10)) {
shintamainjp 0:127b9ca59547 82 return ChoroQ::Stop;
shintamainjp 0:127b9ca59547 83 }
shintamainjp 0:127b9ca59547 84
shintamainjp 1:03c8bc666945 85 if ((x == 0) && (y == 0)) {
shintamainjp 1:03c8bc666945 86 return ChoroQ::Stop;
shintamainjp 1:03c8bc666945 87 }
shintamainjp 1:03c8bc666945 88
shintamainjp 0:127b9ca59547 89 if (std::abs(px) < 10) {
shintamainjp 0:127b9ca59547 90 if (py < 0) {
shintamainjp 0:127b9ca59547 91 if (dash) {
shintamainjp 0:127b9ca59547 92 return ChoroQ::DownDash;
shintamainjp 0:127b9ca59547 93 } else {
shintamainjp 0:127b9ca59547 94 return ChoroQ::Down;
shintamainjp 0:127b9ca59547 95 }
shintamainjp 0:127b9ca59547 96 } else {
shintamainjp 0:127b9ca59547 97 if (dash) {
shintamainjp 0:127b9ca59547 98 return ChoroQ::UpDash;
shintamainjp 0:127b9ca59547 99 } else {
shintamainjp 0:127b9ca59547 100 return ChoroQ::Up;
shintamainjp 0:127b9ca59547 101 }
shintamainjp 0:127b9ca59547 102 }
shintamainjp 0:127b9ca59547 103 }
shintamainjp 0:127b9ca59547 104 if (std::abs(py) < 10) {
shintamainjp 0:127b9ca59547 105 if (px < -20) {
shintamainjp 0:127b9ca59547 106 return ChoroQ::Left;
shintamainjp 0:127b9ca59547 107 }
shintamainjp 0:127b9ca59547 108 if (20 < px) {
shintamainjp 0:127b9ca59547 109 return ChoroQ::Right;
shintamainjp 0:127b9ca59547 110 }
shintamainjp 0:127b9ca59547 111 }
shintamainjp 0:127b9ca59547 112 if ((10 < px) && (10 < py)) {
shintamainjp 0:127b9ca59547 113 if (dash) {
shintamainjp 0:127b9ca59547 114 return ChoroQ::UpRightDash;
shintamainjp 0:127b9ca59547 115 } else {
shintamainjp 0:127b9ca59547 116 return ChoroQ::UpRight;
shintamainjp 0:127b9ca59547 117 }
shintamainjp 0:127b9ca59547 118 }
shintamainjp 0:127b9ca59547 119 if ((px < -10) && (10 < py)) {
shintamainjp 0:127b9ca59547 120 if (dash) {
shintamainjp 0:127b9ca59547 121 return ChoroQ::UpLeftDash;
shintamainjp 0:127b9ca59547 122 } else {
shintamainjp 0:127b9ca59547 123 return ChoroQ::UpLeft;
shintamainjp 0:127b9ca59547 124 }
shintamainjp 0:127b9ca59547 125 }
shintamainjp 0:127b9ca59547 126 if ((px < -10) && (py < -10)) {
shintamainjp 0:127b9ca59547 127 if (dash) {
shintamainjp 0:127b9ca59547 128 return ChoroQ::DownLeftDash;
shintamainjp 0:127b9ca59547 129 } else {
shintamainjp 0:127b9ca59547 130 return ChoroQ::DownLeft;
shintamainjp 0:127b9ca59547 131 }
shintamainjp 0:127b9ca59547 132 }
shintamainjp 0:127b9ca59547 133 if ((10 < px) && (py < -10)) {
shintamainjp 0:127b9ca59547 134 if (dash) {
shintamainjp 0:127b9ca59547 135 return ChoroQ::DownRightDash;
shintamainjp 0:127b9ca59547 136 } else {
shintamainjp 0:127b9ca59547 137 return ChoroQ::DownRight;
shintamainjp 0:127b9ca59547 138 }
shintamainjp 0:127b9ca59547 139 }
shintamainjp 0:127b9ca59547 140 return ChoroQ::Stop;
shintamainjp 0:127b9ca59547 141 }
shintamainjp 0:127b9ca59547 142
shintamainjp 1:03c8bc666945 143 /**
shintamainjp 1:03c8bc666945 144 * Entry point.
shintamainjp 1:03c8bc666945 145 */
shintamainjp 0:127b9ca59547 146 int main() {
shintamainjp 1:03c8bc666945 147 /*
shintamainjp 1:03c8bc666945 148 * Splash.
shintamainjp 1:03c8bc666945 149 */
shintamainjp 1:03c8bc666945 150 splash();
shintamainjp 0:127b9ca59547 151 lcd.cls();
shintamainjp 1:03c8bc666945 152
shintamainjp 1:03c8bc666945 153 /*
shintamainjp 1:03c8bc666945 154 * Setup a configuration.
shintamainjp 1:03c8bc666945 155 */
shintamainjp 1:03c8bc666945 156 appconf_t conf;
shintamainjp 1:03c8bc666945 157 appconf_init(&conf);
shintamainjp 1:03c8bc666945 158 appconf_read(&conf);
shintamainjp 1:03c8bc666945 159
shintamainjp 1:03c8bc666945 160 /*
shintamainjp 1:03c8bc666945 161 * Application loop.
shintamainjp 1:03c8bc666945 162 */
shintamainjp 0:127b9ca59547 163 while (true) {
shintamainjp 0:127b9ca59547 164 wn.RequestRead();
shintamainjp 0:127b9ca59547 165
shintamainjp 0:127b9ca59547 166 ChoroQ::Action ac = getAction(wn.getJoyX(), wn.getJoyY(), (wn.getButtonZ() == 1) ? true : false);
shintamainjp 1:03c8bc666945 167 cq.execute(conf.channel, ac);
shintamainjp 0:127b9ca59547 168
shintamainjp 0:127b9ca59547 169 lcd.locate(0, 0);
shintamainjp 1:03c8bc666945 170 lcd.printf("JS :(%3d,%3d) %s", wn.getJoyX(), wn.getJoyY(), ((wn.getButtonZ() == 1) ? "D" : " "));
shintamainjp 0:127b9ca59547 171
shintamainjp 1:03c8bc666945 172 char *stattext = "";
shintamainjp 0:127b9ca59547 173 switch (ac) {
shintamainjp 0:127b9ca59547 174 case ChoroQ::Undef:
shintamainjp 1:03c8bc666945 175 stattext = "Undef";
shintamainjp 0:127b9ca59547 176 break;
shintamainjp 0:127b9ca59547 177 case ChoroQ::Up:
shintamainjp 1:03c8bc666945 178 stattext = "Up";
shintamainjp 0:127b9ca59547 179 break;
shintamainjp 0:127b9ca59547 180 case ChoroQ::Down:
shintamainjp 1:03c8bc666945 181 stattext = "Down";
shintamainjp 0:127b9ca59547 182 break;
shintamainjp 0:127b9ca59547 183 case ChoroQ::Left:
shintamainjp 1:03c8bc666945 184 stattext = "Left";
shintamainjp 0:127b9ca59547 185 break;
shintamainjp 0:127b9ca59547 186 case ChoroQ::Right:
shintamainjp 1:03c8bc666945 187 stattext = "Right";
shintamainjp 0:127b9ca59547 188 break;
shintamainjp 0:127b9ca59547 189 case ChoroQ::UpDash:
shintamainjp 1:03c8bc666945 190 stattext = "Up +D";
shintamainjp 0:127b9ca59547 191 break;
shintamainjp 0:127b9ca59547 192 case ChoroQ::UpLeft:
shintamainjp 1:03c8bc666945 193 stattext = "UpLeft";
shintamainjp 0:127b9ca59547 194 break;
shintamainjp 0:127b9ca59547 195 case ChoroQ::UpRight:
shintamainjp 1:03c8bc666945 196 stattext = "UpRight";
shintamainjp 0:127b9ca59547 197 break;
shintamainjp 0:127b9ca59547 198 case ChoroQ::UpRightDash:
shintamainjp 1:03c8bc666945 199 stattext = "UpRight +D";
shintamainjp 0:127b9ca59547 200 break;
shintamainjp 0:127b9ca59547 201 case ChoroQ::UpLeftDash:
shintamainjp 1:03c8bc666945 202 stattext = "UpLeft +D";
shintamainjp 0:127b9ca59547 203 break;
shintamainjp 0:127b9ca59547 204 case ChoroQ::DownLeft:
shintamainjp 1:03c8bc666945 205 stattext = "DownLeft";
shintamainjp 0:127b9ca59547 206 break;
shintamainjp 0:127b9ca59547 207 case ChoroQ::DownRight:
shintamainjp 1:03c8bc666945 208 stattext = "DownRight";
shintamainjp 0:127b9ca59547 209 break;
shintamainjp 0:127b9ca59547 210 case ChoroQ::DownDash:
shintamainjp 1:03c8bc666945 211 stattext = "Down +D";
shintamainjp 0:127b9ca59547 212 break;
shintamainjp 0:127b9ca59547 213 case ChoroQ::DownLeftDash:
shintamainjp 1:03c8bc666945 214 stattext = "DownLeft +D";
shintamainjp 0:127b9ca59547 215 break;
shintamainjp 0:127b9ca59547 216 case ChoroQ::DownRightDash:
shintamainjp 1:03c8bc666945 217 stattext = "DownRight+D";
shintamainjp 0:127b9ca59547 218 break;
shintamainjp 0:127b9ca59547 219 case ChoroQ::Stop:
shintamainjp 1:03c8bc666945 220 stattext = "Stop";
shintamainjp 0:127b9ca59547 221 break;
shintamainjp 0:127b9ca59547 222 }
shintamainjp 1:03c8bc666945 223 lcd.locate(0, 1);
shintamainjp 1:03c8bc666945 224 lcd.printf("Ch.%c:%-11.11s", 'A' + (int)conf.channel, stattext);
shintamainjp 0:127b9ca59547 225
shintamainjp 0:127b9ca59547 226 wait_ms(50);
shintamainjp 0:127b9ca59547 227 led = led + 1;
shintamainjp 0:127b9ca59547 228 }
shintamainjp 0:127b9ca59547 229 }