A library to drive the Grove Circular LED by Seeed Studio: https://www.seeedstudio.com/Grove-Circular-LED-p-1353.html
Dependents: Seeed_Grove_Circular_LED
Fork of LED_Bar by
Revision 1:30d1850db92a, committed 2017-06-16
- Comitter:
- mochen
- Date:
- Fri Jun 16 16:09:25 2017 +0000
- Parent:
- 0:edcb13b58b4f
- Commit message:
- Initial commit.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Circular_LED.cpp Fri Jun 16 16:09:25 2017 +0000 @@ -0,0 +1,106 @@ +/* +Circular LED library V1.0 +2010 Copyright (c) Seeed Technology Inc. All right reserved. + +Original Author: LG + +Modify: Mihail Stoyanov (mihail.stoyanov@arm.com) for ARM mbed, 2014-07-30 +Modify: Mo Chen (mo.chen@arm.com) for ARM mbed, 2017-06-16 +User can choose which Io to be used. +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "Circular_LED.h" + +Circular_LED::Circular_LED(PinName pinClk, PinName pinDta) : __pinClk(pinClk), __pinDta(pinDta) +{ + __led_state = 0x00; +} + +void Circular_LED::latchData() +{ + __pinDta = 0; + wait_us(10); + + for(int i = 0; i < 4; i++) { + __pinDta = 1; + __pinDta = 0; + } + +} + +void Circular_LED::send16bitData(uint16_t data) +{ + for(int i = 0; i < 16; i++) { + unsigned int state = data & 0x8000 ? 1 : 0; + __pinDta = state; + + __pinClk = !__pinClk; + // state = __pinClk ? 0 : 1; + // __pinClk = state; + + data <<= 1; + } +} + +void Circular_LED::setState(uint32_t state) +{ + uint16_t st; + + __led_state = state; + send16bitData(CMDMODE); + + for (int i = 0; i < 12; i++) { + st = (state & 0x0001) ? ON : SHUT; + send16bitData(st); + state = state>>1; + } + + send16bitData(CMDMODE); + + for (int i = 12; i < 24; i++) { + st = (state & 0x0001) ? ON : SHUT; + send16bitData(st); + state = state >> 1; + } + + latchData(); +} + +void Circular_LED::setLevel(uint8_t level) +{ + + if (level > 24) { + return; + } + + for (int i = 0; i < 24; i++) { + // Set state bit for each LED + bool st = (i < level); + __led_state = st ? (__led_state | (0x01 << i)) : (__led_state & ~(0x01 << i)); + } + + setState(__led_state); +} + +void Circular_LED::setSingleLed(uint8_t num, bool st) +{ + if (num > 23) { + return; + } + + __led_state = st ? (__led_state | (0x01<<num)) : (__led_state & ~(0x01<<num)); + setState(__led_state); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Circular_LED.h Fri Jun 16 16:09:25 2017 +0000 @@ -0,0 +1,82 @@ +/* +LED bar library V1.0 +2010 Copyright (c) Seeed Technology Inc. All right reserved. + +Original Author: LG +Modify: Loovee, 2014-2-26 +User can choose which Io to be used. + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "mbed.h" + +#ifndef Circular_LED_H +#define Circular_LED_H + +#define CMDMODE 0x0000 // Work on 8-bit mode +#define ON 0x00ff // 8-bit 1 data +#define SHUT 0x0000 // 8-bit 0 data + +/** + * The Circular_LED interface + */ +class Circular_LED +{ + +public: + Circular_LED(PinName pinClk, PinName pinDta); + + /** + * Set the on/off state for all LEDs based on a bitmap. + * @param state The bitmap. For example, if state is 0x05, then led 0 and led 3 will be on, and the others will be off. + */ + void setState(uint32_t state); + + /** + * Set level, frm 0 to 24. + * @param level Level 0 means all leds off while level 5 means 5led on and the other will off + */ + void setLevel(uint8_t level); + + /** + * Control a single led + * @param num which led + * @param st 1: on 0: off + */ + void setSingleLed(uint8_t num, bool st); + +private: + /** + * Pin for clock + */ + DigitalOut __pinClk; + + /** + * Pin for data + */ + DigitalOut __pinDta; + + /** + * LED State + */ + uint32_t __led_state; + + void send16bitData(uint16_t data); + void latchData(void); + +}; + +#endif \ No newline at end of file
--- a/LED_Bar.cpp Wed Jul 30 10:27:21 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,93 +0,0 @@ -/* -LED bar library V1.0 -2010 Copyright (c) Seeed Technology Inc. All right reserved. - -Original Author: LG - -Modify: Mihail Stoyanov (mihail.stoyanov@arm.com) for ARM mbed, 2014-07-30 -User can choose which Io to be used. -This library is free software; you can redistribute it and/or -modify it under the terms of the GNU Lesser General Public -License as published by the Free Software Foundation; either -version 2.1 of the License, or (at your option) any later version. - -This library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public -License along with this library; if not, write to the Free Software -Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#include "LED_Bar.h" - -LED_Bar::LED_Bar(PinName pinClk, PinName pinDta) : __pinClk(pinClk), __pinDta(pinDta) -{ - __led_state = 0x00; -} - -void LED_Bar::latchData() -{ - __pinDta = 0; - wait_us(10); - - for(int i=0; i<4; i++) { - __pinDta = 1; - __pinDta = 0; - } - -} - -void LED_Bar::send16bitData(unsigned int data) -{ - for(int i=0; i<16; i++) { - unsigned int state = data & 0x8000 ? 1 : 0; - __pinDta = state; - - state = __pinClk ? 0 : 1; - __pinClk = state; - - data <<= 1; - } -} - -void LED_Bar::ledIndexBit(unsigned int index_bits) -{ - - send16bitData(CMDMODE); - - for (int i=0; i<12; i++) { - unsigned int state = (index_bits&0x0001) ? ON : SHUT; - send16bitData(state); - - index_bits = index_bits>>1; - } - - latchData(); -} - -void LED_Bar::setLevel(int level) -{ - - if(level>10)return; - - send16bitData(CMDMODE); - - for(int i=0; i<12; i++) { - unsigned int state1 = (i<level) ? ON : SHUT; - - send16bitData(state1); - } - - latchData(); -} - -void LED_Bar::setSingleLed(int num, int st) -{ - if(num>10)return; - __led_state = st ? (__led_state | (0x01<<num)) : (__led_state & ~(0x01<<num)); - ledIndexBit(__led_state); - -}
--- a/LED_Bar.h Wed Jul 30 10:27:21 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,82 +0,0 @@ -/* -LED bar library V1.0 -2010 Copyright (c) Seeed Technology Inc. All right reserved. - -Original Author: LG -Modify: Loovee, 2014-2-26 -User can choose which Io to be used. - -This library is free software; you can redistribute it and/or -modify it under the terms of the GNU Lesser General Public -License as published by the Free Software Foundation; either -version 2.1 of the License, or (at your option) any later version. - -This library is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public -License along with this library; if not, write to the Free Software -Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -*/ - -#include "mbed.h" - -#ifndef LED_Bar_H -#define LED_Bar_H - -#define CMDMODE 0x0000 // Work on 8-bit mode -#define ON 0x00ff // 8-bit 1 data -#define SHUT 0x0000 // 8-bit 0 data - -/** - * The LED_Bar interface - */ -class LED_Bar -{ - -public: - LED_Bar(PinName pinClk, PinName pinDta); - - /** - * Set led single bit, a bit contrl a led - * @param index_bits which bit. if 0x05, then led 0 and led 3 will on, the others will off - */ - void ledIndexBit(unsigned int index_bits); - - /** - * Set level, frm 0 to 10. - * @param level Level 0 means all leds off while level 5 means 5led on and the other will off - */ - void setLevel(int level); - - /** - * Control a single led - * @param num which led - * @param st 1: on 0: off - */ - void setSingleLed(int num, int st); - -private: - /** - * Pin for clock - */ - DigitalOut __pinClk; - - /** - * Pin for data - */ - DigitalOut __pinDta; - - /** - * LED State - */ - unsigned int __led_state; - - void send16bitData(unsigned int data); - void latchData(void); - -}; - -#endif \ No newline at end of file