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/

Dependencies:   mbed

Fork of LED_BitPattern by Takuma Arai

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.

/media/uploads/tandk1124/bitpattern2log.jpeg

Committer:
tandk1124
Date:
Tue Mar 07 01:51:18 2017 +0000
Revision:
6:3e4a21461691
Parent:
5:683d6fff1ebc
fix

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:fb6bbc10ffa0 1 #include "mbed.h"
tandk1124 5:683d6fff1ebc 2 #include "CtrlBase.hpp"
simon 0:fb6bbc10ffa0 3
tandk1124 2:5a1cb5bdeae6 4 DigitalOut leds[] = {LED1, LED2, LED3, LED4};
tandk1124 2:5a1cb5bdeae6 5 int numLeds = sizeof(leds)/sizeof(DigitalOut);
tandk1124 2:5a1cb5bdeae6 6
tandk1124 2:5a1cb5bdeae6 7 class CtrlLEDs{
tandk1124 2:5a1cb5bdeae6 8 public:
tandk1124 2:5a1cb5bdeae6 9 static void ALLOFF(){
tandk1124 2:5a1cb5bdeae6 10 for(int i = 0 ; i < numLeds ; i++){
tandk1124 2:5a1cb5bdeae6 11 leds[i] = 0;
tandk1124 2:5a1cb5bdeae6 12 }
tandk1124 2:5a1cb5bdeae6 13 };
tandk1124 2:5a1cb5bdeae6 14 static void ON(int idx){ leds[idx] = 1; };
tandk1124 2:5a1cb5bdeae6 15 static void OFF(int idx){ leds[idx] = 0; };
tandk1124 2:5a1cb5bdeae6 16
tandk1124 2:5a1cb5bdeae6 17 static void ON_Bit(int flag) {
tandk1124 2:5a1cb5bdeae6 18 if(numLeds < 4) return;
tandk1124 2:5a1cb5bdeae6 19
tandk1124 2:5a1cb5bdeae6 20 leds[0] = ((flag & 0b1000) > 0) ? 1 : 0;
tandk1124 2:5a1cb5bdeae6 21 leds[1] = ((flag & 0b0100) > 0) ? 1 : 0;
tandk1124 2:5a1cb5bdeae6 22 leds[2] = ((flag & 0b0010) > 0) ? 1 : 0;
tandk1124 2:5a1cb5bdeae6 23 leds[3] = ((flag & 0x0001) > 0) ? 1 : 0;
tandk1124 4:0cc67e76eaba 24
tandk1124 4:0cc67e76eaba 25 printf("0x%04x Lighting.\r\n", flag);
tandk1124 2:5a1cb5bdeae6 26 };
tandk1124 2:5a1cb5bdeae6 27 };
simon 0:fb6bbc10ffa0 28
tandk1124 5:683d6fff1ebc 29 class Counter{
tandk1124 5:683d6fff1ebc 30 enum {
tandk1124 5:683d6fff1ebc 31 DOWN = -1,
tandk1124 5:683d6fff1ebc 32 UP = 1
tandk1124 5:683d6fff1ebc 33 };
tandk1124 5:683d6fff1ebc 34 public:
tandk1124 5:683d6fff1ebc 35 Counter(int thresh, bool flg = true): _countSide(flg),_countValue(0), _threshValue(thresh) {};
tandk1124 5:683d6fff1ebc 36 void SetCountSide(bool flg){
tandk1124 5:683d6fff1ebc 37 _countSide = flg;
tandk1124 5:683d6fff1ebc 38 printf("Counter: Mode Changed\r\n");
tandk1124 5:683d6fff1ebc 39 };
tandk1124 5:683d6fff1ebc 40 bool step(){
tandk1124 5:683d6fff1ebc 41 _countValue += _countSide ? UP : DOWN;
tandk1124 5:683d6fff1ebc 42 if( _countSide ){
tandk1124 5:683d6fff1ebc 43 return _countValue < _threshValue;
tandk1124 5:683d6fff1ebc 44 }else{
tandk1124 5:683d6fff1ebc 45 return _countValue >= 0;
tandk1124 5:683d6fff1ebc 46 }
tandk1124 5:683d6fff1ebc 47 };
tandk1124 5:683d6fff1ebc 48 void resetValue(){ _countValue = _countSide ? 0 : _threshValue; };
tandk1124 5:683d6fff1ebc 49 int getValue(){ return _countValue; };
tandk1124 5:683d6fff1ebc 50
tandk1124 5:683d6fff1ebc 51 private:
tandk1124 5:683d6fff1ebc 52 bool _countSide;
tandk1124 5:683d6fff1ebc 53 int _countValue;
tandk1124 5:683d6fff1ebc 54 int _threshValue;
tandk1124 5:683d6fff1ebc 55 };
tandk1124 5:683d6fff1ebc 56
simon 0:fb6bbc10ffa0 57 int main() {
tandk1124 5:683d6fff1ebc 58 CtrlPCSerial pc;
tandk1124 5:683d6fff1ebc 59 CtrlUART uart;
tandk1124 5:683d6fff1ebc 60
tandk1124 5:683d6fff1ebc 61 Counter myCounter(15);
tandk1124 2:5a1cb5bdeae6 62 CtrlLEDs::ALLOFF();
tandk1124 5:683d6fff1ebc 63 char c;
tandk1124 5:683d6fff1ebc 64
tandk1124 5:683d6fff1ebc 65 printf("Press \"0\" to set countdown. \"1\" to set countup.\r\n");
tandk1124 5:683d6fff1ebc 66
simon 0:fb6bbc10ffa0 67 while(1) {
tandk1124 5:683d6fff1ebc 68 if(pc.isReadable()){
tandk1124 5:683d6fff1ebc 69 c = pc.getc();
tandk1124 5:683d6fff1ebc 70 if(c == '0') myCounter.SetCountSide(false);
tandk1124 5:683d6fff1ebc 71 if(c == '1') myCounter.SetCountSide(true);
tandk1124 5:683d6fff1ebc 72 };
tandk1124 5:683d6fff1ebc 73
tandk1124 5:683d6fff1ebc 74 CtrlLEDs::ON_Bit(myCounter.getValue());
tandk1124 5:683d6fff1ebc 75 if(!myCounter.step()){
tandk1124 5:683d6fff1ebc 76 myCounter.resetValue();
tandk1124 2:5a1cb5bdeae6 77 }
tandk1124 3:af4bdd802643 78 wait(0.5);
simon 0:fb6bbc10ffa0 79 }
simon 0:fb6bbc10ffa0 80 }