Dynamic Lighting Lib. of 7Seg-LED (add DP) x4. Features: - Unified Anode/Cathode and NPN/PNP sw. - Non-use wait(); Useing Interrupt Timer. - Modulates Brightness with PWM. - Display Hex. - Using BusOut(s) of Digits(4) and Segments(8). - Sinmple Interface of Class.

Files at this revision

API Documentation at this revision

Comitter:
AkinoriHashimoto
Date:
Wed Apr 11 09:18:20 2018 +0000
Child:
1:e89aae394d29
Commit message:
Dynamic Lighting Lib. of 7Seg-LED (add DP) x4.; ; Features:; - Unified Anode/Cathode common 7Segs, and NPN/PNP switchings.; - Using BusOut(s) of Digits(0-3) and Segments(A-G,DP).; - Sinmple Interface of Class.; - Non-use wait(); Useing Inte

Changed in this revision

LED7segX4.cpp Show annotated file Show diff for this revision Revisions of this file
LED7segX4.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LED7segX4.cpp	Wed Apr 11 09:18:20 2018 +0000
@@ -0,0 +1,150 @@
+#include "LED7segX4.h"
+
+LED7segX4::LED7segX4(BusOut &portDig, BusOut &portSeg)
+    : _portDig(portDig), _portSeg(portSeg)
+{
+    return;
+}
+
+LED7segX4::~LED7segX4()
+{
+    return;   
+}
+
+
+void LED7segX4::init(bool revDig, bool revSeg, int timePeriod, int duty)
+{
+    for(int i= 0; i < 5; i++)
+        _seg7[i]= NULL;
+
+    init_def7seg();
+
+    this->setReverse(revDig, revSeg);
+    
+    this->setPWM(timePeriod, duty);
+    
+    return;
+}
+
+void LED7segX4::setReverse(bool dig, bool seg)
+{
+    _revDig= dig;
+    _revSeg= seg;
+    
+    return;
+}
+
+
+void LED7segX4::init_def7seg()
+{
+    _def7seg[0]= 0x3F;   // 0
+    _def7seg[1]= 0x06;
+    _def7seg[2]= 0x5B;
+    _def7seg[3]= 0x4F;
+    _def7seg[4]= 0x66;
+    _def7seg[5]= 0x6D;
+    _def7seg[6]= 0x7D;
+    _def7seg[7]= 0x27;
+    _def7seg[8]= 0x7F;
+    _def7seg[9]= 0x67;   // 9
+    _def7seg[10]= 0x77;  // A
+    _def7seg[11]= 0x7C;  // b
+    _def7seg[12]= 0x39;  // C
+    _def7seg[13]= 0x5E;  // d
+    _def7seg[14]= 0x79;  // E
+    _def7seg[15]= 0x71;  // F
+    _def7seg[16]= 0x00;
+    
+    return;
+}
+
+void LED7segX4::OnSet()
+{
+    static char tmp;
+    
+    if(++_currentDigit >= 4)
+        _currentDigit= 0;
+    
+    // ** SELECT DIGIT.
+    tmp= 0b1000 >> _currentDigit;
+    if(_revDig)
+        tmp ^= 0xff;
+    _portDig = tmp;   // set BusOut
+    
+    
+    // ** SET current PATTERN of 7SEG.
+    tmp= _seg7[_currentDigit];      // 0-9, a-f, 16(NULL)
+    tmp= _def7seg[tmp];             // ex. 6:0x7D
+    
+    // -- set DP
+    if(_dp & (0b0001 << _currentDigit) )
+        tmp |= 0x80;    // DP: b7
+    
+    if(_revSeg)
+        tmp ^= 0xff;
+    _portSeg= tmp;      // set Bus
+    
+    
+    // *** Atach TimeOut for Modulated Brightness ***
+    if(_dutySec != -1)
+        timeout.attach(this, &LED7segX4::OffSet, _dutySec);
+    
+    return;
+}
+
+void LED7segX4::OffSet()
+{
+    static char tmp;
+    
+    tmp= 0b0000;
+    if(_revDig)
+        tmp ^= 0xff;
+    _portDig = tmp;   // set BusOut
+    
+    tmp= 0x00;
+    if(_revSeg)
+        tmp= 0xff;
+    _portSeg= tmp;      // set BusOut
+    
+    return;
+}
+
+
+
+// **********************************************
+//          Public Interface Set Function
+// **********************************************
+void LED7segX4::set7seg(char chr[])
+{
+    // 0~3 REVERCE
+    for(int i= 0; i < 4; i++)
+        _seg7[i]= chr[3-i];
+    
+    return;
+}
+
+void LED7segX4::setDP(char chr)
+{
+    _dp= chr;
+    return;
+}
+
+void LED7segX4::setPWM(int timePeriod, int duty)
+{
+    if(duty <= 0){
+        ticker.detach();        // detach
+        this->OffSet();
+        return;
+    }
+    
+    _dutySec= timePeriod* 1e-6* (duty / 100.0);
+    if(duty >= 100)
+        _dutySec= -1;
+
+    ticker.attach(this, &LED7segX4::OnSet, timePeriod* 1e-6);
+    
+    return;
+}
+
+
+// EOF
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LED7segX4.h	Wed Apr 11 09:18:20 2018 +0000
@@ -0,0 +1,168 @@
+/**
+ *  Dynamic Lighting Lib. of 7Seg-LED (add DP) x4.
+ *
+ *  Features:
+ *   - Unified Anode/Cathode common 7Segs, and NPN/PNP switchings.
+ *   - Using BusOut(s) of Digits(0-3) and Segments(A-G,DP).
+ *   - Sinmple Interface of Class.
+ *   - Non-use wait(); Useing Interrupt Timer: Ticker and Timeut.
+ *   - Modulates Brightness; You Can Control of Periodic-Time and Duty.
+ *   - Display Hex(0-9,A-F) of 4 Digit; Using char[4].
+ */
+
+/** @code
+#include "mbed.h"
+ 
+#include "LED7segX4.h"
+ 
+// port make.
+BusOut pDgit(p21, p22, p23, p24);   // D1, D2, D3, D4
+BusOut pSeg(p11, p12, p13, p14, p15, p16, p17, p18); // A, B, C, D, E, F, G, DP
+
+// Make instance of 7Seg-class.
+LED7segX4 led7seg(pDgit, pSeg);
+
+// for Operation check.
+DigitalOut myled(LED1);
+
+int main() {
+    
+    char val[4];
+    char dp;
+    int duty;
+    
+     // In this case: Anode Common, PNP swiching for Dig-common,
+    // and sink port(BusOut) for seg.
+    led7seg.init(true, true, 1000, 40);
+    
+    val[0]= 0;
+    val[1]= 0;
+    val[2]= 0;
+    val[3]= 0;
+    
+    led7seg.set7seg(val);
+    led7seg.setDP(0b0001);
+    
+    while(true) {
+        myled = !myled;
+        wait_ms(30);
+
+        if(++duty > 100)
+            duty= 30;
+            
+        val[3] += 1;
+        for(int i= 0; i < 3; i++){
+            if(val[3-i] > 15){
+                if(i < 3){
+                    val[3-i]= 0;
+                    val[2-i] += 1;
+                }
+                dp += 1;
+            }
+        }
+        if(val[0]==15 && val[1]==15 && val[2]==15 && val[3]==15){
+            // = FFFF
+            val[0]= 0;
+            val[1]= 0;
+            val[2]= 0;
+            val[3]= 0;
+        }
+        if(dp > 0b1111)
+            dp= 0;
+        
+        led7seg.set7seg(val);
+        led7seg.setDP(dp);
+        led7seg.setPWM(1000, duty);
+        
+    }
+    
+    return 0;
+}
+ * @endcode
+ */
+ 
+#pragma once
+
+#include "mbed.h"
+
+class LED7segX4{
+public:
+
+    /** Constructor
+     *  @param BusOut; _portDig: Bus4 of Digit(3~0)
+     *  @param BusOut; _portSeg: Bus8 of Segment(a~g+dp)
+     */
+    LED7segX4(BusOut &portDig, BusOut &portSeg);
+
+    ~LED7segX4();
+    
+    
+    /** Initialize
+     *  @param bool; dig: Reverce(H/L) Digit(3~0)
+     *  @param bool; seg: Reverce(H/L) Segment(a~g+dp)
+     *  @param int; timePeriod[us]: time of priend in PWM.
+     *  @param int; duty[%]: duty of PWM.
+     */
+    void init(bool revDig= false, bool revSeg= false, int timePeriod= 1000, int duty= 80);
+
+    
+    /** set seg TYPE;
+     *  @param bool; dig: Digit(3~0)
+     *  @param bool; seg: Segment(a~g+dp)
+    */
+    void setReverse(bool dig, bool seg);
+    
+    /** Set 7seg.
+     *  @param chr[4]; D3~D1, 0x00(0)~0x15(F) and 0x16(OFF/NULL).
+    */
+    void set7seg(char chr[]);
+    
+    /** Set DotPoint.
+     *  @param chr; 0b0000~0b1111, bit0~3; TRUE=1.
+    */
+    void setDP(char chr);
+    
+    /** Set PWM (Britness).
+     *  @param int; periodic time [us]. "for Each Digit."
+     *  @param int; duty ratio;1~100[%] in timePeriod.
+     *  ex) In case of 1,000us and 60%, 
+    */
+    void setPWM(int timePeriod, int duty);
+    
+    
+    
+private:
+
+    // ********************** Variable ******************************
+    
+    // pin/port set
+    BusOut &_portDig;
+    BusOut &_portSeg;
+    
+    // TimerIRQ 4 PWM
+    Ticker ticker;      // Duty : Bright
+    Timeout timeout;    // Duty : OFF
+    float _dutySec;     // Duty(Bright) Time-width [s], in 1 period.
+
+    // Define
+    char _def7seg[17];   // Define Segment; 0~f + 16:OFF(Null)
+
+
+    // Local Hold Val
+    char _seg7[5];      // 4+Null           ex) 2.3E5 -> 2, 3, 14, 5
+    char _dp;           // 0b0000~0b1111;   ex) 12.3E. -> 0b0101
+    bool _revDig;       // REVERSE H/L 4 Transistor swDigit
+    bool _revSeg;       //                      AND swSeg
+    int _currentDigit;  // current digit-Num (0~3). This shifts in timeperiod of PWM.
+    
+
+    // ********************** Internal Function *********************
+    void init_def7seg();    // Set/Define of seg pattern.
+    
+    // For DUTY of PWM. ON(Shift) and OFF.
+    void OnSet();
+    void OffSet();
+    
+};
+
+// EOF
\ No newline at end of file