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.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LED7segX4.cpp Source File

LED7segX4.cpp

00001 #include "LED7segX4.h"
00002 
00003 LED7segX4::LED7segX4(BusOut &portDig, BusOut &portSeg)
00004     : _portDig(portDig), _portSeg(portSeg)
00005 {
00006     return;
00007 }
00008 
00009 LED7segX4::~LED7segX4()
00010 {
00011     return;   
00012 }
00013 
00014 
00015 void LED7segX4::init(bool revDig, bool revSeg, int timePeriod, int duty)
00016 {
00017     for(int i= 0; i < 5; i++)
00018         _seg7[i]= NULL;
00019 
00020     init_def7seg();
00021 
00022     this->setReverse(revDig, revSeg);
00023     
00024     this->setPWM(timePeriod, duty);
00025     
00026     return;
00027 }
00028 
00029 void LED7segX4::setReverse(bool dig, bool seg)
00030 {
00031     _revDig= dig;
00032     _revSeg= seg;
00033     
00034     return;
00035 }
00036 
00037 
00038 void LED7segX4::init_def7seg()
00039 {
00040     _def7seg[0]= 0x3F;   // 0
00041     _def7seg[1]= 0x06;
00042     _def7seg[2]= 0x5B;
00043     _def7seg[3]= 0x4F;
00044     _def7seg[4]= 0x66;
00045     _def7seg[5]= 0x6D;
00046     _def7seg[6]= 0x7D;
00047     _def7seg[7]= 0x27;
00048     _def7seg[8]= 0x7F;
00049     _def7seg[9]= 0x6F;   // 9
00050     _def7seg[10]= 0x77;  // A
00051     _def7seg[11]= 0x7C;  // b
00052     _def7seg[12]= 0x39;  // C
00053     _def7seg[13]= 0x5E;  // d
00054     _def7seg[14]= 0x79;  // E
00055     _def7seg[15]= 0x71;  // F
00056     _def7seg[16]= 0x00;
00057     
00058     return;
00059 }
00060 
00061 void LED7segX4::OnSet()
00062 {
00063     static char tmp;
00064     
00065     if(++_currentDigit >= 4)
00066         _currentDigit= 0;
00067     
00068     // ** SELECT DIGIT.
00069     tmp= 0b1000 >> _currentDigit;
00070     if(_revDig)
00071         tmp ^= 0xff;
00072     _portDig = tmp;   // set BusOut
00073     
00074     
00075     // ** SET current PATTERN of 7SEG.
00076     tmp= _seg7[_currentDigit];      // 0-9, a-f, 16(NULL)
00077     tmp= _def7seg[tmp];             // ex. 6:0x7D
00078     
00079     // -- set DP
00080     if(_dp & (0b0001 << _currentDigit) )
00081         tmp |= 0x80;    // DP: b7
00082     
00083     if(_revSeg)
00084         tmp ^= 0xff;
00085     _portSeg= tmp;      // set Bus
00086     
00087     
00088     // *** Atach TimeOut for Modulated Brightness ***
00089     if(_dutySec != -1)
00090         timeout.attach(this, &LED7segX4::OffSet, _dutySec);
00091     
00092     return;
00093 }
00094 
00095 void LED7segX4::OffSet()
00096 {
00097     static char tmp;
00098     
00099     tmp= 0b0000;
00100     if(_revDig)
00101         tmp ^= 0xff;
00102     _portDig = tmp;   // set BusOut
00103     
00104     tmp= 0x00;
00105     if(_revSeg)
00106         tmp= 0xff;
00107     _portSeg= tmp;      // set BusOut
00108     
00109     return;
00110 }
00111 
00112 
00113 
00114 // **********************************************
00115 //          Public Interface Set Function
00116 // **********************************************
00117 void LED7segX4::set7seg(char chr[])
00118 {
00119     // 0~3 REVERCE
00120     for(int i= 0; i < 4; i++)
00121         _seg7[i]= chr[3-i];
00122     
00123     return;
00124 }
00125 
00126 void LED7segX4::setDP(char chr)
00127 {
00128     _dp= chr;
00129     return;
00130 }
00131 
00132 void LED7segX4::setPWM(int timePeriod, int duty)
00133 {
00134     if(duty <= 0){
00135         ticker.detach();        // detach
00136         this->OffSet();
00137         return;
00138     }
00139     
00140     _dutySec= timePeriod* 1e-6* (duty / 100.0);
00141     if(duty >= 100)
00142         _dutySec= -1;
00143 
00144     ticker.attach(this, &LED7segX4::OnSet, timePeriod* 1e-6);
00145     
00146     return;
00147 }
00148 
00149 
00150 // EOF