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