Sample Code of http://ohurochan.jp/blog/?p=446 Added SerialController to control BitPattern from PC via Serial. Based on https://developer.mbed.org/users/tandk1124/code/LED_BitPattern/
Fork of LED_BitPattern by
Added Control class for SeirialPC. You can change LED Lighting Bit Pattern from PC Serial. Type "1" to set countup. Type "0" to set countdown.
Diff: main.cpp
- Revision:
- 5:683d6fff1ebc
- Parent:
- 4:0cc67e76eaba
--- a/main.cpp Mon Mar 06 15:51:58 2017 +0000 +++ b/main.cpp Tue Mar 07 00:14:42 2017 +0000 @@ -1,10 +1,9 @@ #include "mbed.h" +#include "CtrlBase.hpp" DigitalOut leds[] = {LED1, LED2, LED3, LED4}; int numLeds = sizeof(leds)/sizeof(DigitalOut); -Serial pc(USBTX, USBRX); - class CtrlLEDs{ public: static void ALLOFF(){ @@ -27,16 +26,54 @@ }; }; +class Counter{ + enum { + DOWN = -1, + UP = 1 + }; +public: + Counter(int thresh, bool flg = true): _countSide(flg),_countValue(0), _threshValue(thresh) {}; + void SetCountSide(bool flg){ + _countSide = flg; + printf("Counter: Mode Changed\r\n"); + }; + bool step(){ + _countValue += _countSide ? UP : DOWN; + if( _countSide ){ + return _countValue < _threshValue; + }else{ + return _countValue >= 0; + } + }; + void resetValue(){ _countValue = _countSide ? 0 : _threshValue; }; + int getValue(){ return _countValue; }; + +private: + bool _countSide; + int _countValue; + int _threshValue; +}; + int main() { - int loopCnt = 0; + CtrlPCSerial pc; + CtrlUART uart; + + Counter myCounter(15); CtrlLEDs::ALLOFF(); + char c; + + printf("Press \"0\" to set countdown. \"1\" to set countup.\r\n"); + while(1) { - if(loopCnt<=15){ - CtrlLEDs::ON_Bit(loopCnt); - loopCnt++; - if(loopCnt > 15){ - loopCnt = 0; - } + if(pc.isReadable()){ + c = pc.getc(); + if(c == '0') myCounter.SetCountSide(false); + if(c == '1') myCounter.SetCountSide(true); + }; + + CtrlLEDs::ON_Bit(myCounter.getValue()); + if(!myCounter.step()){ + myCounter.resetValue(); } wait(0.5); }