Erriez TM1638 library for Arduino  1.2.0
TM1638 button and LED controller library for Arduino
ErriezTM1638.cpp
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 
33 #include "ErriezTM1638.h"
34 
43 TM1638::TM1638(uint8_t clkPin, uint8_t dioPin, uint8_t stbPin, bool displayOn, uint8_t brightness) :
44  _displayOn(displayOn), _brightness(brightness & 0x07)
45 {
46 #ifdef __AVR
47  // Calculate bit and port register for fast pin read and writes (AVR targets only)
48  _clkPort = digitalPinToPort(clkPin);
49  _dioPort = digitalPinToPort(dioPin);
50  _stbPort = digitalPinToPort(stbPin);
51 
52  _clkBit = digitalPinToBitMask(clkPin);
53  _dioBit = digitalPinToBitMask(dioPin);
54  _stbBit = digitalPinToBitMask(stbPin);
55 #else
56  // Use the slow digitalRead() and digitalWrite() functions for non-AVR targets
57  _dioPin = dioPin;
58  _clkPin = clkPin;
59  _stbPin = stbPin;
60 #endif
61 }
62 
67 {
68  // Initialize pins
72 
73  // Set pin mode
77 
78  // Write _displayOn and _brightness to display control register
80 
81  // Data write with auto address increment
83 }
84 
89 {
93 
97 }
98 
103 {
104  _displayOn = true;
105 
107 }
108 
113 {
114  _displayOn = false;
115 
117 }
118 
124 void TM1638::setBrightness(uint8_t brightness)
125 {
126  if (brightness <= 7) {
127  _brightness = brightness;
128 
130  }
131 }
132 
137 {
138  TM1638_STB_LOW();
139  writeByte((uint8_t)(TM1638_CMD_ADDR | 0x00));
140  for (uint8_t i = 0; i < TM1638_NUM_GRIDS; i++) {
141  writeByte(0x00);
142  }
143  TM1638_STB_HIGH();
144 }
145 
151 void TM1638::writeData(uint8_t address, uint8_t data)
152 {
153  if (address <= TM1638_NUM_GRIDS) {
154  // Write byte to display register
155  TM1638_STB_LOW();
156  writeByte((uint8_t)(TM1638_CMD_ADDR | address));
157  writeByte(data);
158  TM1638_STB_HIGH();
159  }
160 }
161 
173 void TM1638::writeData(uint8_t address, const uint8_t *buf, uint8_t len)
174 {
175  if ((address + len) <= TM1638_NUM_GRIDS) {
176  // Write buffer to display registers
177  TM1638_STB_LOW();
178  writeByte((uint8_t)(TM1638_CMD_ADDR | address));
179  for (uint8_t i = 0; i < len; i++) {
180  writeByte(buf[i]);
181  }
182  TM1638_STB_HIGH();
183  }
184 }
185 
190 uint32_t TM1638::getKeys()
191 {
192  uint32_t keys = 0;
193 
194  // Read 4 Bytes key-scan registers
195  TM1638_STB_LOW();
197  for (uint8_t i = 0; i < 4; i++) {
198  keys |= ((uint32_t)readByte() << (i * 8));
199  }
200  TM1638_STB_HIGH();
201 
202  return keys;
203 }
204 
205 // -------------------------------------------------------------------------------------------------
212 {
213  writeCommand((uint8_t)(TM1638_CMD_CTRL |
215  _brightness));
216 }
217 
221 void TM1638::writeCommand(uint8_t cmd)
222 {
223  TM1638_STB_LOW();
224  writeByte(cmd);
225  TM1638_STB_HIGH();
226 }
227 
232 void TM1638::writeByte(uint8_t data)
233 {
234  for (uint8_t i = 0; i < 8; i++) {
235  TM1638_CLK_LOW();
237  if (data & (1 << i)) {
238  TM1638_DIO_HIGH();
239  } else {
240  TM1638_DIO_LOW();
241  }
242  TM1638_CLK_HIGH();
244  }
245 }
246 
252 {
253  uint8_t data = 0;
254 
256  for (uint8_t i = 0; i < 8; i++) {
257  TM1638_CLK_LOW();
259  if (TM1638_DIO_READ()) {
260  data |= (1 << i);
261  }
262  TM1638_CLK_HIGH();
264  }
266 
267  return data;
268 }
uint8_t _brightness
Display brightness for display control register.
Definition: ErriezTM1638.h:186
#define TM1638_DIO_HIGH()
DIO pin high.
Definition: ErriezTM1638.h:133
virtual void writeByte(uint8_t data)
Write byte to TM1638.
#define TM1638_CMD_DATA
Display data command.
Definition: ErriezTM1638.h:85
TM1638(uint8_t clkPin, uint8_t dioPin, uint8_t stbPin, bool displayOn=true, uint8_t brightness=5)
TM1638 constructor.
#define TM1638_CTRL_DISPLAY_ON
Display on.
Definition: ErriezTM1638.h:105
#define TM1638_DATA_AUTO_INC_ADDR
Auto increment address.
Definition: ErriezTM1638.h:92
#define TM1638_STB_OUTPUT()
STB pin output.
Definition: ErriezTM1638.h:141
#define TM1638_PIN_DELAY()
Delay between pin changes.
Definition: ErriezTM1638.h:148
#define TM1638_DIO_READ()
DIO pin read.
Definition: ErriezTM1638.h:136
virtual void writeDisplayControl()
Write display control register.
virtual void setBrightness(uint8_t brightness)
Set brightness LED&#39;s.
#define TM1638_CLK_INPUT()
CLK pin input.
Definition: ErriezTM1638.h:129
#define TM1638_NUM_GRIDS
Number of grid registers.
Definition: ErriezTM1638.h:108
#define TM1638_DIO_INPUT()
DIO pin input.
Definition: ErriezTM1638.h:134
#define TM1638_STB_INPUT()
STB pin input.
Definition: ErriezTM1638.h:140
virtual void clear()
Turn all LED&#39;s off.
uint8_t _dioPin
Data pin.
Definition: ErriezTM1638.h:181
#define TM1638_CMD_CTRL
Display control command.
Definition: ErriezTM1638.h:86
uint8_t _clkPin
Clock pin.
Definition: ErriezTM1638.h:180
virtual void begin()
Initialize TM1638 controller.
#define TM1638_CLK_LOW()
CLK pin low.
Definition: ErriezTM1638.h:127
virtual void writeData(uint8_t address, uint8_t data)
Write display register.
#define TM1638_STB_LOW()
STB pin low.
Definition: ErriezTM1638.h:138
#define TM1638_DATA_READ_KEYS
Read keys.
Definition: ErriezTM1638.h:91
virtual uint32_t getKeys()
Get key states.
#define TM1638_CMD_ADDR
Display address command.
Definition: ErriezTM1638.h:87
virtual void writeCommand(uint8_t cmd)
Write command to TM1638.
#define TM1638_CTRL_DISPLAY_OFF
Display off.
Definition: ErriezTM1638.h:104
#define TM1638_STB_HIGH()
STB pin high.
Definition: ErriezTM1638.h:139
#define TM1638_CLK_HIGH()
CLK pin high.
Definition: ErriezTM1638.h:128
virtual void displayOff()
Turn display off.
virtual void end()
Disable pins.
#define TM1638_DATA_WRITE
Write data.
Definition: ErriezTM1638.h:90
#define TM1638_DIO_LOW()
DIO pin low.
Definition: ErriezTM1638.h:132
uint8_t _stbPin
Enable pin.
Definition: ErriezTM1638.h:182
#define TM1638_CLK_OUTPUT()
CLK pin output.
Definition: ErriezTM1638.h:130
virtual void displayOn()
Turn Display on.
#define TM1638_DIO_OUTPUT()
DIO pin output.
Definition: ErriezTM1638.h:135
bool _displayOn
Display on and off status for display control register.
Definition: ErriezTM1638.h:185
virtual uint8_t readByte()
Read byte from TM1638.
TM1638 library for Arduino.