00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Component/ModelManager.H"
00039 #include "Devices/BeoChip.H"
00040 #include "Devices/DeviceOpts.H"
00041 #include "Util/MathFunctions.H"
00042
00043 #include <cstdlib>
00044 #include <iostream>
00045
00046
00047 class MyBeoChipListener : public BeoChipListener
00048 {
00049 public:
00050 MyBeoChipListener()
00051 { rawpwm[0] = 0; rawpwm[1] = 0; rawadc[0] = 0; rawadc[1] = 0; kbd = 0x1f; }
00052
00053 virtual ~MyBeoChipListener()
00054 { }
00055
00056 virtual void event(const BeoChipEventType t, const int valint,
00057 const float valfloat)
00058 {
00059 LDEBUG("Event: %d val = %d, fval = %f", int(t), valint, valfloat);
00060 switch(t)
00061 {
00062 case NONE: break;
00063 case PWM0: rawpwm[0] = valint; break;
00064 case PWM1: rawpwm[1] = valint; break;
00065 case KBD: kbd = valint; break;
00066 case ADC0: rawadc[0] = valint; break;
00067 case ADC1: rawadc[1] = valint; break;
00068 case RESET: LERROR("BeoChip RESET occurred!"); break;
00069 case ECHOREP: LINFO("BeoChip Echo reply received."); break;
00070 case INOVERFLOW: LERROR("BeoChip input overflow!"); break;
00071 case SERIALERROR: LERROR("BeoChip serial error!"); break;
00072 case OUTOVERFLOW: LERROR("BeoChip output overflow!"); break;
00073 default: LERROR("Unknown event %d received!", int(t)); break;
00074 }
00075 }
00076
00077
00078 volatile int rawpwm[2];
00079 volatile int rawadc[2];
00080 volatile int kbd;
00081 };
00082
00083
00084
00085
00086 int main(const int argc, const char* argv[])
00087 {
00088 MYLOGVERB = LOG_INFO;
00089
00090
00091 ModelManager manager("BeoChip test program");
00092
00093
00094 nub::soft_ref<BeoChip> b(new BeoChip(manager));
00095 manager.addSubComponent(b);
00096
00097
00098 if (manager.parseCommandLine(argc, argv, "<serdev> [all|anim|font|"
00099 "flood|rnd|esc|noflow|setServo] servo# servoPos", 1, 4) == false)
00100 return(1);
00101
00102
00103 b->setModelParamVal("BeoChipDeviceName", manager.getExtraArg(0));
00104 std::string task = ""; bool doall = false;
00105 if (manager.numExtraArgs() > 1)
00106 {
00107 task = manager.getExtraArg(1);
00108 doall = (task.compare("all") == 0);
00109 }
00110
00111
00112 if (task.compare("noflow") == 0)
00113 {
00114 b->setModelParamVal("BeoChipUseRTSCTS", false);
00115 LINFO("Disabled RTS/CTS flow control");
00116 }
00117
00118
00119
00120 rutz::shared_ptr<MyBeoChipListener> lis(new MyBeoChipListener);
00121 rutz::shared_ptr<BeoChipListener> lis2; lis2.dynCastFrom(lis);
00122 b->setListener(lis2);
00123
00124
00125 manager.start();
00126
00127 LINFO("Waiting for a bit. Turn BeoChip on if not already on."); sleep(2);
00128 LINFO("Echo request (should bring an echo reply back)...");
00129 b->echoRequest(); sleep(1);
00130
00131
00132 if (task.compare("setServo") == 0)
00133 {
00134 if (manager.numExtraArgs() > 3)
00135 {
00136 int servo = manager.getExtraArgAs<int>(2);
00137 int val = manager.getExtraArgAs<int>(3);
00138 LINFO("Setting servo %i to %i", servo, val);
00139 b->setServoRaw(servo, val);
00140 } else {
00141 LINFO("Need servo and position cmd");
00142 }
00143 manager.stop();
00144 exit(0);
00145 }
00146
00147
00148 if (task.compare("rnd") == 0 || doall)
00149 {
00150 LINFO("Starting random junk flood...");
00151 b->lcdClear();
00152 b->lcdPrintf(0, 1, "Random junk flood"); sleep(1);
00153 for (int i = 0; i < 5000; i ++)
00154 {
00155 int val = randomUpToIncluding(255);
00156 if (val != 0xF9) b->writeByte(val);
00157 }
00158 b->lcdClear();
00159 b->lcdPrintf(0, 1, "Random junk over"); sleep(1);
00160 sleep(30);
00161 b->lcdPrintf(0, 1, "Reset..."); sleep(1);
00162 LINFO("Reset BeoChip...");
00163 b->resetChip(); sleep(1);
00164 }
00165
00166
00167 if (task.compare("anim") == 0 || doall)
00168 {
00169 b->lcdClear();
00170
00171 b->lcdPrintf(0, 0, "%c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c", 8, 1, 2, 3, 4,
00172 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
00173 b->lcdPrintf(0, 3, "%c%c%c%c%c%c%c%c %c%c%c%c%c%c%c%c", 8, 1, 2, 3, 4,
00174 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
00175 for (int i = 0; i < 8; i ++)
00176 {
00177 LINFO("Loading font %d...", i);
00178 b->lcdPrintf(0, 1, "Loading LCD font %d ", i);
00179 b->lcdLoadFont(i);
00180 sleep(3);
00181 }
00182 }
00183
00184
00185 if (task.compare("anim") == 0 || doall)
00186 {
00187 b->lcdClear(); b->lcdLoadFont(1);
00188 for (int i = 1; i < 8; i ++)
00189 {
00190 LINFO("Starting animation %d...", i);
00191 b->lcdClear();
00192 b->lcdPrintf(0, 1, "LCD Animation %d ", i);
00193 b->lcdSetAnimation(i);
00194 sleep(10);
00195 }
00196 }
00197
00198
00199
00200
00201 if (task.compare("flood") == 0 || doall)
00202 {
00203 LINFO("Trying an LCD clear flood...");
00204 b->lcdPrintf(0, 0, "LCD clear flood"); sleep(1);
00205 for (int i = 0; i < 1000; i ++) b->lcdClear();
00206 b->lcdPrintf(0, 0, "Clear flood is over."); sleep(5);
00207
00208 LINFO("Trying an LCD font flood...");
00209 b->lcdPrintf(0, 0, "LCD font flood"); sleep(1);
00210 for (int i = 0; i < 1000; i ++) b->lcdLoadFont(i & 7);
00211 b->lcdPrintf(0, 0, "Font flood is over. "); sleep(15);
00212
00213 LINFO("Trying an LCD font/clear flood...");
00214 b->lcdPrintf(0, 0, "LCD font/clear flood"); sleep(1);
00215 for (int i = 0; i < 1000; i ++) { b->lcdLoadFont(i & 7); b->lcdClear(); }
00216 b->lcdPrintf(0, 0, "Font/clr flood over "); sleep(15);
00217 }
00218
00219
00220 if (task.compare("esc") == 0 || doall)
00221 {
00222 LINFO("Setting all servos to neutral (127)");
00223 for (int i = 0; i < 8; i ++) b->setServoRaw(i, 127);
00224 LINFO("Press [RETURN] when ready for full-swing...");
00225 getchar();
00226 for (int j = 0; j < 10; j ++)
00227 {
00228 LINFO("Setting all servos to full forward (255)");
00229 for (int i = 0; i < 8; i ++) b->setServoRaw(i, 255);
00230 usleep(750000);
00231 LINFO("Setting all servos to full reverse (0)");
00232 for (int i = 0; i < 8; i ++) b->setServoRaw(i, 0);
00233 usleep(750000);
00234 }
00235 LINFO("Setting all servos to neutral (127)");
00236 for (int i = 0; i < 8; i ++) b->setServoRaw(i, 127);
00237 LINFO("Press [RETURN] when done...");
00238 getchar();
00239 }
00240
00241 LINFO("Ok, have a look at the LCD screen now...");
00242
00243
00244 b->lcdLoadFont(1);
00245
00246
00247 b->capturePulse(0, true);
00248 b->capturePulse(1, true);
00249 b->captureAnalog(0, true);
00250 b->captureAnalog(1, true);
00251 b->debounceKeyboard(true);
00252 b->captureKeyboard(true);
00253
00254
00255 LINFO("Starting to play with the servos...");
00256 b->lcdClear();
00257 b->lcdPrintf(0, 0, "test-BeoChip -- 1.00");
00258 b->lcdPrintf(0, 1, "ADC=000/000 DOU=0000");
00259 b->lcdPrintf(0, 2, "PWM=0000/0000 X ");
00260 b->lcdPrintf(0, 3, "Servos=000 KB=00000");
00261
00262
00263 b->setServoRaw(5, 127);
00264 b->setServoRaw(6, 0);
00265 b->setServoRaw(7, 255);
00266
00267
00268 int ani = 0; int dout = 0; bool keepgoing = true;
00269 while(keepgoing)
00270 {
00271 for (int p = 0; p < 256; p ++)
00272 {
00273
00274 b->lcdPrintf(4, 1, "%03d/%03d", lis->rawadc[0], lis->rawadc[1]);
00275
00276
00277 char digout[5]; digout[4] = '\0';
00278 for (int i = 0; i < 4; i ++)
00279 {
00280 digout[i] = (dout>>(3-i))&1 ? '1':'0';
00281 b->setDigitalOut(3-i, (dout>>(3-i))&1?true:false);
00282 }
00283 b->lcdPrintf(16, 1, "%s", digout); dout ++; dout &= 15;
00284
00285
00286 b->lcdPrintf(4, 2, "%04d/%04d", lis->rawpwm[0], lis->rawpwm[1]);
00287
00288
00289 char kb[6]; kb[5] = '\0';
00290 for (int i = 0; i < 5; i ++) kb[i] = (lis->kbd>>(4-i))&1 ? '1':'0';
00291 b->lcdPrintf(15, 3, "%s", kb);
00292
00293
00294 if (kb[0] == '0' && kb[4] == '0') { keepgoing = false; break; }
00295
00296
00297 if ((dout & 7) == 0)
00298 { b->lcdPrintf(17, 2, "%c", ani+8); ani ++; ani &= 7; }
00299
00300
00301 for (int s = 0; s < 5; s ++)
00302 if (s & 1) b->setServoRaw(s, p);
00303 else b->setServoRaw(s, 256 - p);
00304
00305
00306 b->lcdPrintf(7, 3, "%03d", p);
00307
00308
00309 usleep(50000);
00310 }
00311 }
00312
00313 manager.stop();
00314 return 0;
00315 }
00316
00317
00318
00319
00320
00321