Mo Chen / Circular_LED

Dependents:   Seeed_Grove_Circular_LED

Fork of LED_Bar by Seeed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Circular_LED.cpp Source File

Circular_LED.cpp

00001 /*
00002 Circular LED library V1.0
00003 2010 Copyright (c) Seeed Technology Inc. All right reserved.
00004 
00005 Original Author: LG
00006 
00007 Modify: Mihail Stoyanov (mihail.stoyanov@arm.com) for ARM mbed, 2014-07-30
00008 Modify: Mo Chen (mo.chen@arm.com) for ARM mbed, 2017-06-16
00009 User can choose which Io to be used.
00010 This library is free software; you can redistribute it and/or
00011 modify it under the terms of the GNU Lesser General Public
00012 License as published by the Free Software Foundation; either
00013 version 2.1 of the License, or (at your option) any later version.
00014 
00015 This library is distributed in the hope that it will be useful,
00016 but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018 Lesser General Public License for more details.
00019 
00020 You should have received a copy of the GNU Lesser General Public
00021 License along with this library; if not, write to the Free Software
00022 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00023 */
00024 
00025 #include "Circular_LED.h"
00026 
00027 Circular_LED::Circular_LED(PinName pinClk, PinName pinDta) : __pinClk(pinClk), __pinDta(pinDta)
00028 {
00029     __led_state = 0x00;
00030 }
00031 
00032 void Circular_LED::latchData()
00033 {
00034     __pinDta = 0;
00035     wait_us(10);
00036 
00037     for(int i = 0; i < 4; i++) {
00038         __pinDta = 1;
00039         __pinDta = 0;
00040     }
00041 
00042 }
00043 
00044 void Circular_LED::send16bitData(uint16_t data)
00045 {
00046     for(int i = 0; i < 16; i++) {
00047         unsigned int state = data & 0x8000 ? 1 : 0;
00048         __pinDta = state;
00049 
00050         __pinClk = !__pinClk;
00051         // state = __pinClk ? 0 : 1;
00052         // __pinClk = state;
00053 
00054         data <<= 1;
00055     }
00056 }
00057 
00058 void Circular_LED::setState(uint32_t state)
00059 {
00060     uint16_t st;
00061     
00062     __led_state = state;
00063     send16bitData(CMDMODE);
00064 
00065     for (int i = 0; i < 12; i++) {
00066         st = (state & 0x0001) ? ON : SHUT;
00067         send16bitData(st);
00068         state = state>>1;
00069     }
00070     
00071     send16bitData(CMDMODE);
00072     
00073     for (int i = 12; i < 24; i++) {
00074         st = (state & 0x0001) ? ON : SHUT;
00075         send16bitData(st);
00076         state = state >> 1;
00077     }
00078 
00079     latchData();
00080 }
00081 
00082 void Circular_LED::setLevel(uint8_t level)
00083 {
00084 
00085     if (level > 24) {
00086         return;
00087     }
00088     
00089     for (int i = 0; i < 24; i++) {
00090         // Set state bit for each LED
00091         bool st = (i < level);
00092         __led_state = st ? (__led_state | (0x01 << i)) : (__led_state & ~(0x01 << i));
00093     }
00094 
00095     setState(__led_state);
00096 }
00097 
00098 void Circular_LED::setSingleLed(uint8_t num, bool st)
00099 {
00100     if (num > 23) {
00101         return;
00102     }
00103 
00104     __led_state = st ? (__led_state | (0x01<<num)) : (__led_state & ~(0x01<<num));
00105     setState(__led_state);
00106 }