Erriez TM1638 library for Arduino  1.2.0
TM1638 button and LED controller library for Arduino
ErriezTM1638.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2018-2020 Erriez
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
79 #ifndef ERRIEZ_TM1638_H_
80 #define ERRIEZ_TM1638_H_
81 
82 #include <Arduino.h>
83 
84 // Commands
85 #define TM1638_CMD_DATA 0x40
86 #define TM1638_CMD_CTRL 0x80
87 #define TM1638_CMD_ADDR 0xc0
88 
89 // Data command bits
90 #define TM1638_DATA_WRITE 0x00
91 #define TM1638_DATA_READ_KEYS 0x02
92 #define TM1638_DATA_AUTO_INC_ADDR 0x00
93 #define TM1638_DATA_FIXED_ADDR 0x04
94 
95 // Control command bits
96 #define TM1638_CTRL_PULSE_1_16 0x00
97 #define TM1638_CTRL_PULSE_2_16 0x01
98 #define TM1638_CTRL_PULSE_4_16 0x02
99 #define TM1638_CTRL_PULSE_10_16 0x03
100 #define TM1638_CTRL_PULSE_11_16 0x04
101 #define TM1638_CTRL_PULSE_12_16 0x05
102 #define TM1638_CTRL_PULSE_13_16 0x06
103 #define TM1638_CTRL_PULSE_14_16 0x07
104 #define TM1638_CTRL_DISPLAY_OFF 0x00
105 #define TM1638_CTRL_DISPLAY_ON 0x08
106 
107 #define TM1638_NUM_GRIDS 16
109 
110 #ifdef __AVR
111 #define TM1638_CLK_LOW() { *portOutputRegister(_clkPort) &= ~_clkBit; }
112 #define TM1638_CLK_HIGH() { *portOutputRegister(_clkPort) |= _clkBit; }
113 #define TM1638_CLK_INPUT() { *portModeRegister(_clkPort) &= ~_clkBit; }
114 #define TM1638_CLK_OUTPUT() { *portModeRegister(_clkPort) |= _clkBit; }
115 
116 #define TM1638_DIO_LOW() { *portOutputRegister(_dioPort) &= ~_dioBit; }
117 #define TM1638_DIO_HIGH() { *portOutputRegister(_dioPort) |= _dioBit; }
118 #define TM1638_DIO_INPUT() { *portModeRegister(_dioPort) &= ~_dioBit; }
119 #define TM1638_DIO_OUTPUT() { *portModeRegister(_dioPort) |= _dioBit; }
120 #define TM1638_DIO_READ() ( *portInputRegister(_dioPort) & _dioBit )
121 
122 #define TM1638_STB_LOW() { *portOutputRegister(_stbPort) &= ~_stbBit; }
123 #define TM1638_STB_HIGH() { *portOutputRegister(_stbPort) |= _stbBit; }
124 #define TM1638_STB_INPUT() { *portModeRegister(_stbPort) &= ~_stbBit; }
125 #define TM1638_STB_OUTPUT() { *portModeRegister(_stbPort) |= _stbBit; }
126 #else
127 #define TM1638_CLK_LOW() { digitalWrite(_clkPin, LOW); }
128 #define TM1638_CLK_HIGH() { digitalWrite(_clkPin, HIGH); }
129 #define TM1638_CLK_INPUT() { pinMode(_clkPin, INPUT); }
130 #define TM1638_CLK_OUTPUT() { pinMode(_clkPin, OUTPUT); }
131 
132 #define TM1638_DIO_LOW() { digitalWrite(_dioPin, LOW); }
133 #define TM1638_DIO_HIGH() { digitalWrite(_dioPin, HIGH); }
134 #define TM1638_DIO_INPUT() { pinMode(_dioPin, INPUT); }
135 #define TM1638_DIO_OUTPUT() { pinMode(_dioPin, OUTPUT); }
136 #define TM1638_DIO_READ() ( digitalRead(_dioPin) )
137 
138 #define TM1638_STB_LOW() { digitalWrite(_stbPin, LOW); }
139 #define TM1638_STB_HIGH() { digitalWrite(_stbPin, HIGH); }
140 #define TM1638_STB_INPUT() { pinMode(_stbPin, INPUT); }
141 #define TM1638_STB_OUTPUT() { pinMode(_stbPin, OUTPUT); }
142 #endif
143 
144 // Delay defines
145 #if F_CPU >= 20000000UL
146 #define TM1638_PIN_DELAY() { delayMicroseconds(1); }
147 #else
148 #define TM1638_PIN_DELAY()
149 #endif
150 
154 class TM1638
155 {
156 public:
157  TM1638(uint8_t clkPin, uint8_t dioPin, uint8_t stbPin,
158  bool displayOn=true, uint8_t brightness=5);
159 
160  virtual void begin();
161  virtual void end();
162  virtual void displayOn();
163  virtual void displayOff();
164  virtual void setBrightness(uint8_t brightness);
165  virtual void clear();
166  virtual void writeData(uint8_t address, uint8_t data);
167  virtual void writeData(uint8_t address, const uint8_t *buf, uint8_t len);
168  virtual uint32_t getKeys();
169 
170 protected:
171 #ifdef __AVR
172  uint8_t _clkPort;
173  uint8_t _dioPort;
174  uint8_t _stbPort;
175 
176  uint8_t _clkBit;
177  uint8_t _dioBit;
178  uint8_t _stbBit;
179 #else
180  uint8_t _clkPin;
181  uint8_t _dioPin;
182  uint8_t _stbPin;
183 #endif
184 
185  bool _displayOn;
186  uint8_t _brightness;
187 
188  virtual void writeDisplayControl();
189  virtual void writeCommand(uint8_t cmd);
190  virtual void writeByte(uint8_t data);
191  virtual uint8_t readByte();
192 };
193 
194 #endif // ERRIEZ_TM1638_H_
uint8_t _brightness
Display brightness for display control register.
Definition: ErriezTM1638.h:186
virtual void writeByte(uint8_t data)
Write byte to TM1638.
TM1638(uint8_t clkPin, uint8_t dioPin, uint8_t stbPin, bool displayOn=true, uint8_t brightness=5)
TM1638 constructor.
virtual void writeDisplayControl()
Write display control register.
TM1638 class.
Definition: ErriezTM1638.h:154
virtual void setBrightness(uint8_t brightness)
Set brightness LED&#39;s.
virtual void clear()
Turn all LED&#39;s off.
uint8_t _dioPin
Data pin.
Definition: ErriezTM1638.h:181
uint8_t _clkPin
Clock pin.
Definition: ErriezTM1638.h:180
virtual void begin()
Initialize TM1638 controller.
virtual void writeData(uint8_t address, uint8_t data)
Write display register.
virtual uint32_t getKeys()
Get key states.
virtual void writeCommand(uint8_t cmd)
Write command to TM1638.
virtual void displayOff()
Turn display off.
virtual void end()
Disable pins.
uint8_t _stbPin
Enable pin.
Definition: ErriezTM1638.h:182
virtual void displayOn()
Turn Display on.
bool _displayOn
Display on and off status for display control register.
Definition: ErriezTM1638.h:185
virtual uint8_t readByte()
Read byte from TM1638.