Erriez DHT22 library for Arduino  1.2.0
AM2302/AM2303 DHT22 temperature and humidity sensor library for Arduino
ErriezTM1637.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 "ErriezTM1637.h"
34 
49 TM1637::TM1637(uint8_t clkPin, uint8_t dioPin, bool displayOn, uint8_t brightness) :
50  _displayOn(displayOn), _brightness((uint8_t)(brightness & 0x07))
51 {
52 #ifdef __AVR
53  // Calculate bit and port register for fast pin read and writes (AVR targets only)
54  _clkPort = digitalPinToPort(clkPin);
55  _dioPort = digitalPinToPort(dioPin);
56 
57  _clkBit = digitalPinToBitMask(clkPin);
58  _dioBit = digitalPinToBitMask(dioPin);
59 #else
60  // Use the slow digitalRead() and digitalWrite() functions for non-AVR targets
61  _dioPin = dioPin;
62  _clkPin = clkPin;
63 #endif
64 }
65 
70 {
71  // Make DIO and CLK pins high
74 
75  // Set pin mode
78 
79  // Write _displayOn and _brightness to display control register
81 
82  // Data write with auto address increment
84 }
85 
90 {
91  // Release DIO and CLK pins
94 
97 }
98 
103 {
104  _displayOn = true;
105 
107 }
108 
113 {
114  _displayOn = false;
115 
117 }
118 
124 void TM1637::setBrightness(uint8_t brightness)
125 {
126  if (brightness <= 7) {
127  _brightness = brightness;
128 
130  }
131 }
132 
137 {
138  start();
139  writeByte((uint8_t)(TM1637_CMD_ADDR | 0x00));
140  for (uint8_t i = 0; i < TM1637_NUM_GRIDS; i++) {
141  writeByte(0x00);
142  }
143  stop();
144 }
145 
151 void TM1637::writeData(uint8_t address, uint8_t data)
152 {
153  if (address < TM1637_NUM_GRIDS) {
154  start();
155  writeByte((uint8_t)(TM1637_CMD_ADDR | address));
156  writeByte(data);
157  stop();
158  }
159 }
160 
172 void TM1637::writeData(uint8_t address, const uint8_t *buf, uint8_t len)
173 {
174  if ((address + len) <= TM1637_NUM_GRIDS) {
175  // Write buffer to display registers
176  start();
177  writeByte((uint8_t)(TM1637_CMD_ADDR | address));
178  for (uint8_t i = 0; i < len; i++) {
179  writeByte(buf[i]);
180  }
181  stop();
182  }
183 }
184 
191 uint8_t TM1637::getKeys()
192 {
193  uint8_t keyCode;
194 
195  start();
197  start();
198  keyCode = readByte();
199  stop();
200 
201  // Check if key is down (at least one bit is zero)
202  if (keyCode != 0xFF) {
203  // Invert keyCode:
204  // Bit | 7 6 5 4 3 2 1 0
205  // ------+-------------------------
206  // From | S0 S1 S2 K1 K2 1 1 1
207  // To | S0 S1 S2 K1 K2 0 0 0
208  keyCode = ~keyCode;
209 
210  // Shift bits to:
211  // Bit | 7 6 5 4 3 2 1 0
212  // ------+------------------------
213  // To | 0 0 0 0 K2 S2 S1 S0
214  keyCode = (uint8_t)((keyCode & 0x80) >> 7 |
215  (keyCode & 0x40) >> 5 |
216  (keyCode & 0x20) >> 3 |
217  (keyCode & 0x08));
218  }
219 
220  return keyCode;
221 }
222 
223 // -------------------------------------------------------------------------------------------------
228 {
229  // Write to display control register
230  start();
231  writeByte((uint8_t)(TM1637_CMD_CTRL |
233  _brightness));
234  stop();
235 }
236 
240 void TM1637::writeCommand(uint8_t cmd)
241 {
242  start();
243  writeByte(cmd);
244  stop();
245 }
246 
247 // -------------------------------------------------------------------------------------------------
251 void TM1637::start(void)
252 {
253  TM1637_DIO_LOW();
255  TM1637_CLK_LOW();
257 }
258 
262 void TM1637::stop(void)
263 {
264  TM1637_DIO_LOW();
266  TM1637_CLK_HIGH();
268  TM1637_DIO_HIGH();
270 }
271 
276 void TM1637::writeByte(uint8_t data)
277 {
278  for (uint8_t i = 0; i < 8; i++) {
280  TM1637_CLK_LOW();
281 
282  if (data & 0x01) {
283  TM1637_DIO_HIGH();
284  } else {
285  TM1637_DIO_LOW();
286  }
287  data >>= 1;
288 
290  TM1637_CLK_HIGH();
291  }
292 
294  TM1637_CLK_LOW();
295 
296  // Set DIO pin to input with pull-up to read ACK
297  TM1637_DIO_HIGH();
300 
301  TM1637_CLK_HIGH();
303  // Skip reading DIO pin: ack = _dioPin;
304  TM1637_CLK_LOW();
306 
307  // Set DIO pin to output
310 }
311 
317 {
318  uint8_t retval = 0;
319 
320  // Prepare DIO to read data
321  TM1637_DIO_HIGH();
324 
325  // Data is shifted out by the TM1637 on the CLK falling edge
326  for (uint8_t bit = 0; bit < 8; bit++) {
327  TM1637_CLK_HIGH();
329 
330  // Read next bit
331  retval <<= 1;
332  if (TM1637_DIO_READ()) {
333  retval |= 0x01;
334  }
335 
336  TM1637_CLK_LOW();
338  }
339 
340  // Return DIO to output mode
343 
344  // Dummy ACK
345  TM1637_DIO_LOW();
347 
348  TM1637_CLK_HIGH();
350  TM1637_CLK_LOW();
352 
353  TM1637_DIO_HIGH();
355 
356  return retval;
357 }
#define TM1637_DIO_INPUT()
DIO pin input.
Definition: ErriezTM1637.h:126
#define TM1637_CLK_OUTPUT()
CLK pin output.
Definition: ErriezTM1637.h:123
#define TM1637_DIO_OUTPUT()
DIO pin output.
Definition: ErriezTM1637.h:127
uint8_t _dioPin
Data pin.
Definition: ErriezTM1637.h:166
#define TM1637_NUM_GRIDS
Number of grid registers.
Definition: ErriezTM1637.h:107
#define TM1637_CTRL_DISPLAY_OFF
Display off.
Definition: ErriezTM1637.h:104
#define TM1637_CMD_ADDR
Display address command.
Definition: ErriezTM1637.h:87
#define TM1637_DIO_READ()
DIO pin read.
Definition: ErriezTM1637.h:128
virtual void writeByte(uint8_t data)
Write byte to TM1637.
#define TM1637_DIO_LOW()
DIO pin low.
Definition: ErriezTM1637.h:124
virtual void writeData(uint8_t address, uint8_t data)
Write display register.
virtual void start()
Generate start condition.
#define TM1637_CMD_CTRL
Display control command.
Definition: ErriezTM1637.h:86
virtual uint8_t getKeys()
Get key states.
#define TM1637_DATA_AUTO_INC_ADDR
Auto increment address.
Definition: ErriezTM1637.h:92
#define TM1637_CLK_INPUT()
CLK pin input.
Definition: ErriezTM1637.h:122
virtual uint8_t readByte()
Read byte from TM1637.
virtual void end()
Release TM1637 pins.
uint8_t _brightness
Display brightness for display control register.
Definition: ErriezTM1637.h:170
uint8_t _clkPin
Clock pin.
Definition: ErriezTM1637.h:165
#define TM1637_PIN_DELAY()
Delay between pin changes.
Definition: ErriezTM1637.h:134
virtual void writeDisplayControl()
Write display control register.
#define TM1637_CTRL_DISPLAY_ON
Display on.
Definition: ErriezTM1637.h:105
#define TM1637_CLK_HIGH()
CLK pin high.
Definition: ErriezTM1637.h:121
virtual void writeCommand(uint8_t cmd)
Write command to TM1637.
virtual void displayOff()
Turn display off.
#define TM1637_DATA_WRITE
Write data.
Definition: ErriezTM1637.h:90
virtual void clear()
Turn all LED&#39;s off.
#define TM1637_DATA_READ_KEYS
Read keys.
Definition: ErriezTM1637.h:91
virtual void stop()
Generate stop condition.
#define TM1637_CLK_LOW()
CLK pin low.
Definition: ErriezTM1637.h:120
bool _displayOn
Display on and off status for display control register.
Definition: ErriezTM1637.h:169
TM1637(uint8_t clkPin, uint8_t dioPin, bool displayOn=true, uint8_t brightness=5)
TM1637 constructor.
#define TM1637_DIO_HIGH()
DIO pin high.
Definition: ErriezTM1637.h:125
virtual void setBrightness(uint8_t brightness)
Set brightness LED&#39;s.
#define TM1637_CMD_DATA
Display data command.
Definition: ErriezTM1637.h:85
TM1637 library for Arduino.
virtual void displayOn()
Turn Display on.
virtual void begin()
Initialize TM1637 controller.