fork of seeed studio 4-digit display for st nucleo board
Dependencies: Data_Clock_Pair Seeed_Chainable_LED Seeed_Four_Digit_Disp Seeed_IR_Temp_Sensor Seeed_Led_Bar
Fork of Seeed_Grove_4_Digit_Display_Clock by
SeeedFourDigitDisp.h@8:09c844708255, 2017-04-21 (annotated)
- Committer:
- tulanthoar
- Date:
- Fri Apr 21 13:47:19 2017 +0000
- Revision:
- 8:09c844708255
- Parent:
- 7:b16b9733d859
- Child:
- 12:a16d86fac131
add fastio library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tulanthoar | 7:b16b9733d859 | 1 | #ifndef SEEED_FOUR_DIGIT_DISP_H |
tulanthoar | 7:b16b9733d859 | 2 | #define SEEED_FOUR_DIGIT_DISP_H |
tulanthoar | 7:b16b9733d859 | 3 | #include "mbed.h" |
tulanthoar | 7:b16b9733d859 | 4 | |
tulanthoar | 7:b16b9733d859 | 5 | class SeeedFourDigitDisp { |
tulanthoar | 7:b16b9733d859 | 6 | private: |
tulanthoar | 7:b16b9733d859 | 7 | int digitTable_[17]; |
tulanthoar | 7:b16b9733d859 | 8 | static const int onByte_ = 0x88; |
tulanthoar | 7:b16b9733d859 | 9 | static const int fixedAddrByte_ = 0x44; |
tulanthoar | 7:b16b9733d859 | 10 | static const int positionBit_ = 0xc0; |
tulanthoar | 7:b16b9733d859 | 11 | static const int nullDigit_ = 16; |
tulanthoar | 7:b16b9733d859 | 12 | static const int colonBit_ = 0x80; |
tulanthoar | 7:b16b9733d859 | 13 | void start_cmd(); |
tulanthoar | 7:b16b9733d859 | 14 | void stop_cmd(); |
tulanthoar | 7:b16b9733d859 | 15 | DigitalOut datPin_; |
tulanthoar | 7:b16b9733d859 | 16 | DigitalOut clkPin_; |
tulanthoar | 7:b16b9733d859 | 17 | void pin_delay(int delay_us = 1); |
tulanthoar | 7:b16b9733d859 | 18 | void send_byte(int byte); |
tulanthoar | 7:b16b9733d859 | 19 | public: |
tulanthoar | 7:b16b9733d859 | 20 | int brightness; |
tulanthoar | 7:b16b9733d859 | 21 | bool colonFlag; |
tulanthoar | 7:b16b9733d859 | 22 | void set_digit(int pos, int digit); |
tulanthoar | 7:b16b9733d859 | 23 | void set_integer(int value); |
tulanthoar | 7:b16b9733d859 | 24 | void clear_display(); |
tulanthoar | 7:b16b9733d859 | 25 | void turn_on(); |
tulanthoar | 7:b16b9733d859 | 26 | SeeedFourDigitDisp (PinName dataOut, PinName clockOut); |
tulanthoar | 7:b16b9733d859 | 27 | }; |
tulanthoar | 7:b16b9733d859 | 28 | |
tulanthoar | 7:b16b9733d859 | 29 | SeeedFourDigitDisp::SeeedFourDigitDisp(PinName dataOut, PinName clockOut) : datPin_(dataOut), clkPin_(clockOut) { |
tulanthoar | 7:b16b9733d859 | 30 | brightness = 7; |
tulanthoar | 7:b16b9733d859 | 31 | colonFlag = false; |
tulanthoar | 7:b16b9733d859 | 32 | const int digits[] = {0x3f, 0x06, 0x5b, 0x4f, |
tulanthoar | 7:b16b9733d859 | 33 | 0x66, 0x6d, 0x7d, 0x07, |
tulanthoar | 7:b16b9733d859 | 34 | 0x7f, 0x6f, 0x77, 0x7c, |
tulanthoar | 7:b16b9733d859 | 35 | 0x39, 0x5e, 0x79, 0x71, |
tulanthoar | 7:b16b9733d859 | 36 | 0x00 |
tulanthoar | 7:b16b9733d859 | 37 | }; //0~9,A,b,C,d,E,F,null |
tulanthoar | 7:b16b9733d859 | 38 | for (int i = 0; i < 17; i++) { |
tulanthoar | 7:b16b9733d859 | 39 | digitTable_[i] = digits[i]; |
tulanthoar | 7:b16b9733d859 | 40 | } |
tulanthoar | 7:b16b9733d859 | 41 | clear_display(); |
tulanthoar | 7:b16b9733d859 | 42 | } |
tulanthoar | 7:b16b9733d859 | 43 | |
tulanthoar | 7:b16b9733d859 | 44 | void SeeedFourDigitDisp::start_cmd() { |
tulanthoar | 7:b16b9733d859 | 45 | datPin_ = 0; |
tulanthoar | 7:b16b9733d859 | 46 | } |
tulanthoar | 7:b16b9733d859 | 47 | |
tulanthoar | 7:b16b9733d859 | 48 | void SeeedFourDigitDisp::stop_cmd() { |
tulanthoar | 7:b16b9733d859 | 49 | clkPin_ = 0; |
tulanthoar | 7:b16b9733d859 | 50 | datPin_ = 0; |
tulanthoar | 7:b16b9733d859 | 51 | pin_delay(); |
tulanthoar | 7:b16b9733d859 | 52 | clkPin_ = 1; |
tulanthoar | 7:b16b9733d859 | 53 | datPin_ = 1; |
tulanthoar | 7:b16b9733d859 | 54 | } |
tulanthoar | 7:b16b9733d859 | 55 | |
tulanthoar | 7:b16b9733d859 | 56 | void SeeedFourDigitDisp::clear_display() { |
tulanthoar | 7:b16b9733d859 | 57 | set_digit(0,nullDigit_); |
tulanthoar | 7:b16b9733d859 | 58 | set_digit(1,nullDigit_); |
tulanthoar | 7:b16b9733d859 | 59 | set_digit(2,nullDigit_); |
tulanthoar | 7:b16b9733d859 | 60 | set_digit(3,nullDigit_); |
tulanthoar | 7:b16b9733d859 | 61 | } |
tulanthoar | 7:b16b9733d859 | 62 | |
tulanthoar | 7:b16b9733d859 | 63 | void SeeedFourDigitDisp::set_digit(int pos, int digit) { |
tulanthoar | 7:b16b9733d859 | 64 | int flaggedDigit = digitTable_[digit] | (colonFlag ? colonBit_ : 0); |
tulanthoar | 7:b16b9733d859 | 65 | start_cmd(); //start signal sent to TM1637 from MCU |
tulanthoar | 7:b16b9733d859 | 66 | send_byte(fixedAddrByte_); |
tulanthoar | 7:b16b9733d859 | 67 | stop_cmd(); |
tulanthoar | 7:b16b9733d859 | 68 | start_cmd(); |
tulanthoar | 7:b16b9733d859 | 69 | send_byte(pos|positionBit_); |
tulanthoar | 7:b16b9733d859 | 70 | send_byte(flaggedDigit); |
tulanthoar | 7:b16b9733d859 | 71 | stop_cmd(); |
tulanthoar | 7:b16b9733d859 | 72 | start_cmd(); |
tulanthoar | 7:b16b9733d859 | 73 | send_byte(onByte_ + brightness); |
tulanthoar | 7:b16b9733d859 | 74 | stop_cmd(); |
tulanthoar | 7:b16b9733d859 | 75 | |
tulanthoar | 7:b16b9733d859 | 76 | } |
tulanthoar | 7:b16b9733d859 | 77 | |
tulanthoar | 7:b16b9733d859 | 78 | void SeeedFourDigitDisp::set_integer(int value) { |
tulanthoar | 7:b16b9733d859 | 79 | clear_display(); |
tulanthoar | 7:b16b9733d859 | 80 | if( value < 0 ) { |
tulanthoar | 7:b16b9733d859 | 81 | colonFlag = true; |
tulanthoar | 7:b16b9733d859 | 82 | set_digit(0, 0); |
tulanthoar | 7:b16b9733d859 | 83 | return; |
tulanthoar | 7:b16b9733d859 | 84 | } |
tulanthoar | 7:b16b9733d859 | 85 | if( value > 9999 ) { |
tulanthoar | 7:b16b9733d859 | 86 | colonFlag = true; |
tulanthoar | 7:b16b9733d859 | 87 | set_digit(0, 15); |
tulanthoar | 7:b16b9733d859 | 88 | return; |
tulanthoar | 7:b16b9733d859 | 89 | } |
tulanthoar | 7:b16b9733d859 | 90 | for (int i = 3; i >= 0; --i) { |
tulanthoar | 7:b16b9733d859 | 91 | int digit = value % 10; |
tulanthoar | 7:b16b9733d859 | 92 | set_digit(i, digit); |
tulanthoar | 7:b16b9733d859 | 93 | value -= digit; |
tulanthoar | 7:b16b9733d859 | 94 | if(value < 10) return; |
tulanthoar | 7:b16b9733d859 | 95 | value /= 10; |
tulanthoar | 7:b16b9733d859 | 96 | } |
tulanthoar | 7:b16b9733d859 | 97 | } |
tulanthoar | 7:b16b9733d859 | 98 | |
tulanthoar | 7:b16b9733d859 | 99 | void SeeedFourDigitDisp::turn_on() { |
tulanthoar | 7:b16b9733d859 | 100 | start_cmd(); |
tulanthoar | 7:b16b9733d859 | 101 | send_byte(onByte_+brightness); |
tulanthoar | 7:b16b9733d859 | 102 | stop_cmd(); |
tulanthoar | 7:b16b9733d859 | 103 | } |
tulanthoar | 7:b16b9733d859 | 104 | |
tulanthoar | 7:b16b9733d859 | 105 | void SeeedFourDigitDisp::pin_delay(int delay_us) { |
tulanthoar | 7:b16b9733d859 | 106 | wait_us(delay_us); |
tulanthoar | 7:b16b9733d859 | 107 | } |
tulanthoar | 7:b16b9733d859 | 108 | |
tulanthoar | 7:b16b9733d859 | 109 | void SeeedFourDigitDisp::send_byte(int byte) { |
tulanthoar | 7:b16b9733d859 | 110 | byte |= 0x100; // bring data high for ack after 8 bits |
tulanthoar | 7:b16b9733d859 | 111 | for (uint8_t i = 0; i < 9; i++) { |
tulanthoar | 7:b16b9733d859 | 112 | pin_delay(); |
tulanthoar | 7:b16b9733d859 | 113 | clkPin_ = 0; |
tulanthoar | 7:b16b9733d859 | 114 | pin_delay(); |
tulanthoar | 7:b16b9733d859 | 115 | datPin_ = byte & 1; |
tulanthoar | 7:b16b9733d859 | 116 | pin_delay(); |
tulanthoar | 7:b16b9733d859 | 117 | byte >>= 1; |
tulanthoar | 7:b16b9733d859 | 118 | clkPin_ = 1; |
tulanthoar | 7:b16b9733d859 | 119 | } |
tulanthoar | 7:b16b9733d859 | 120 | } |
tulanthoar | 7:b16b9733d859 | 121 | |
tulanthoar | 7:b16b9733d859 | 122 | #endif |