Yoshitaka Kuwata / stnseg

Dependents:   16segmentLED_test

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Stnseg.h Source File

Stnseg.h

00001 /*
00002   Stnseg.cpp - mbed library for 2/4/8 digit Sixteen (16) segment LED driver.
00003   Copyright 2015 by morecat_lab
00004    
00005   This library is distributed in the hope that it will be useful,
00006   but WITHOUT ANY WARRANTY; without even the implied warranty of
00007   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00008 */
00009 
00010 #ifndef STNSEG_H
00011 #define STNSEG_H
00012 
00013 #include "mbed.h"
00014 #include <Timer.h>
00015 
00016 // the order of segment bit is as follows (16 bit)
00017 // |             |             |               |            |
00018 //  A1, A2, B, C, D1, D2, E, F,  G1, G2, H, I,  J, K, L, M
00019 // note that DP must set independent port
00020 
00021 #define NUM_PAT16_0 0xff09
00022 #define NUM_PAT16_1 0x3000
00023 #define NUM_PAT16_2 0xeec0
00024 #define NUM_PAT16_3 0xfcc0
00025 #define NUM_PAT16_4 0x31c0
00026 #define NUM_PAT16_5 0xddc0
00027 #define NUM_PAT16_6 0xdfc0
00028 #define NUM_PAT16_7 0xf000
00029 #define NUM_PAT16_8 0xffc0
00030 #define NUM_PAT16_9 0xfdc0
00031 #define NUM_PAT16_A 0xf3c0
00032 #define NUM_PAT16_B 0xfc52
00033 #define NUM_PAT16_C 0xcf00
00034 #define NUM_PAT16_D 0xfc12
00035 #define NUM_PAT16_E 0xcf80
00036 #define NUM_PAT16_F 0xf310
00037 
00038 #define NUM_PAT16_MINUS 0x00c0
00039 
00040 /**
00041  * Sixteen segment LED driver library
00042  */
00043 class Stnseg
00044 {
00045 private:
00046     unsigned long _lastUpdateTime;
00047     int _updateInterval;
00048     DigitalOut _dataPin, _clockPin, _latchPin, _dpPin;
00049     BusOut _digPins;
00050     uint32_t _buffer[8];
00051     int _numOfDigs;
00052     int _dig;       // support 4, 6 or 8
00053     bool _zeroSupress;
00054     bool _kcommon;  // Cathode-common flag
00055     Timer timer;
00056 public:
00057     static const uint16_t numConv[16];
00058 
00059     /**
00060      * create an 4 digit sixteen segment driver
00061      *
00062      * @param PinName data (for 74HC959)
00063      * @param PinName clock (for 74HC959)
00064      * @param PinName latch (for 74HC959)
00065      * @param PinName dp Pin No for segment DP
00066      * @param PinName d1 Pin No for dight 1
00067      * @param PinName d2 Pin No for dight 2
00068      * @param PinName d3 Pin No for dight 3
00069      * @param PinName d4 Pin No for dight 4
00070      */
00071     Stnseg(PinName data, PinName clock, PinName latch, PinName dp,
00072          PinName d1,PinName d2, PinName d3, PinName d4);
00073 
00074     /**
00075      * create an 6 digit sixteen segment driver
00076      *
00077      * @param PinName data (for 74HC959)
00078      * @param PinName clock (for 74HC959)
00079      * @param PinName latch (for 74HC959)
00080      * @param PinName dp Pin No for segment DP
00081      * @param PinName d1 Pin No for dight 1
00082      * @param PinName d2 Pin No for dight 2
00083      * @param PinName d3 Pin No for dight 3
00084      * @param PinName d4 Pin No for dight 4
00085      * @param PinName d4 Pin No for dight 5
00086      * @param PinName d4 Pin No for dight 6
00087      */
00088     Stnseg(PinName data, PinName clock, PinName latch, PinName dp,
00089          PinName d1,PinName d2, PinName d3, PinName d4,
00090          PinName d5, PinName d6);
00091 
00092     /**
00093      * create an 8 digit sixteen segment driver
00094      *
00095      * @param PinName data (for 74HC959)
00096      * @param PinName clock (for 74HC959)
00097      * @param PinName latch (for 74HC959)
00098      * @param PinName dp Pin No for segment DP
00099      * @param PinName d1 Pin No for dight 1
00100      * @param PinName d2 Pin No for dight 2
00101      * @param PinName d3 Pin No for dight 3
00102      * @param PinName d4 Pin No for dight 4
00103      * @param PinName d5 Pin No for dight 5
00104      * @param PinName d6 Pin No for dight 6
00105      * @param PinName d7 Pin No for dight 7
00106      * @param PinName d8 Pin No for dight 8
00107      */
00108     Stnseg(PinName data, PinName clock, PinName latch, PinName dp,
00109          PinName d1,PinName d2, PinName d3, PinName d4,
00110          PinName d5,PinName d6, PinName d7, PinName d8);
00111     
00112     /**
00113      * start driver
00114      */
00115     void begin(void);
00116 
00117     /**
00118      * use Kathode Common LED
00119      */
00120     void setKcommon(void);
00121     
00122     /**
00123      * use Anode Common LED (default)
00124      */
00125     void setAcommon(void);
00126     
00127     /**
00128      * get a charcter pattern from a number
00129      *
00130      * @param i number
00131      *
00132      * @returns bit pattern of number i
00133      *
00134      */
00135     uint16_t segCh(char i);
00136 
00137     /**
00138      * turn on DP 
00139      *
00140      * @param d dight
00141      *
00142      */
00143     void setDot(int d);
00144 
00145     /**
00146      * turn off DP 
00147      *
00148      * @param d dight
00149      *
00150      */
00151     void clearDot(int d);
00152     
00153     /**
00154      * write a number to LED 
00155      *
00156      * @param d number
00157      *
00158      */
00159     void writeNum(int n);
00160 
00161     /**
00162      * write a number to 4 dight LED 
00163      *
00164      * @param n number
00165      *
00166      */
00167     void writeNum4(int n);
00168 
00169     /**
00170      * write a number to 6 dight LED 
00171      *
00172      * @param n number
00173      *
00174      */
00175     void writeNum6(int n);
00176 
00177     /**
00178      * write a number to 8 dight LED 
00179      *
00180      * @param n number
00181      *
00182      */
00183     void writeNum8(int n);
00184     
00185     /**
00186      * write numbers to each dight of 2 dight LED 
00187      *
00188      * @param d1 digit 1 number
00189      * @param d2 digit 2 number
00190      *
00191      */
00192     void writeNum(char d1, char d2);
00193 
00194     /**
00195      * write numbers to each dight of 4 dight LED 
00196      *
00197      * @param d1 digit 1 number
00198      * @param d2 digit 2 number
00199      * @param d3 digit 3 number
00200      * @param d4 digit 4 number
00201      *
00202      */
00203     void writeNum(char d1, char d2, char d3, char d4);
00204 
00205     /**
00206      * write numbers to each dight of 8 dight LED 
00207      *
00208      * @param d1 digit 1 number
00209      * @param d2 digit 2 number
00210      * @param d3 digit 3 number
00211      * @param d4 digit 4 number
00212      * @param d5 digit 5 number
00213      * @param d6 digit 6 number
00214      * @param d7 digit 7 number
00215      * @param d8 digit 8 number
00216      *
00217      */
00218     void writeNum(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8);
00219 
00220     /**
00221      * zero supress: tell driver not to display 0 in the left
00222      *
00223      */
00224     void supressZero();
00225 
00226     /**
00227      * control zero supress bit
00228      *
00229      * @param t, 1:supress on, 0:supress off
00230      *
00231      */
00232     void setZeroSupress(bool t);
00233 
00234     /**
00235      * write hex number to LED
00236      *
00237      * @param n (long)number
00238      *
00239      */
00240     void writeHex(long n);
00241 
00242     
00243     /**
00244      * write patterns to each dight of 4 dight LED 
00245      *
00246      * @param d1 digit 1 pattern
00247      * @param d2 digit 2 pattern
00248      * @param d3 digit 3 pattern
00249      * @param d4 digit 4 pattern
00250      *
00251      */
00252     void writeRawData(uint32_t d1, uint32_t d2, uint32_t d3, uint32_t d4);
00253 
00254     /**
00255      * write patterns to each dight of 6 dight LED 
00256      *
00257      * @param d1 digit 1 pattern
00258      * @param d2 digit 2 pattern
00259      * @param d3 digit 3 pattern
00260      * @param d4 digit 4 pattern
00261      * @param d5 digit 5 pattern
00262      * @param d6 digit 6 pattern
00263      *
00264      */
00265     void writeRawData(uint32_t d1, uint32_t d2, uint32_t d3, uint32_t d4, uint32_t d5, uint32_t d6);
00266 
00267     /**
00268      * write patterns to each dight of 8 dight LED 
00269      *
00270      * @param d1 digit 1 pattern
00271      * @param d2 digit 2 pattern
00272      * @param d3 digit 3 pattern
00273      * @param d4 digit 4 pattern
00274      * @param d5 digit 5 pattern
00275      * @param d6 digit 6 pattern
00276      * @param d7 digit 7 pattern
00277      * @param d8 digit 8 pattern
00278      *
00279      */
00280     void writeRawData(uint32_t d1, uint32_t d2, uint32_t d3, uint32_t d4, uint32_t d5, uint32_t d6, uint32_t d7, uint32_t d8);
00281 
00282     /**
00283      * write patterns to a dight 
00284      *
00285      * @param d digit
00286      *
00287      * @param value pattern
00288      *
00289      */
00290     void write(uint8_t d, uint32_t value);
00291 
00292     /**
00293      * Clear LED buffer
00294      */
00295     void clear(void);
00296     
00297     /**
00298      * Turn off LED
00299      */
00300     void turnOff(void);
00301 
00302     /**
00303      * Turn on LED
00304      */
00305     void turnOn(void);
00306 
00307     /**
00308      * Update One dight of LED
00309      */
00310     void updateSeg(void);
00311 
00312     /**
00313      * keep updating LED for specified period
00314      *
00315      * @param ms period (ms)
00316      *
00317      */
00318     void updateWithDelay(int ms);
00319 
00320     /**
00321      * Update LED Once with 1ms delay
00322      */
00323     void updateOnce(void);
00324 };
00325 
00326 #endif  // Stnseg.h