mirror of
https://github.com/avishorp/TM1637.git
synced 2026-07-27 19:56:13 +00:00
137 lines
2.8 KiB
C++
137 lines
2.8 KiB
C++
|
|
// Author: avishorp@gmail.com
|
|
//
|
|
// This library is free software; you can redistribute it and/or
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
// License as published by the Free Software Foundation; either
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
//
|
|
// This library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
// Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
// License along with this library; if not, write to the Free Software
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
extern "C" {
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <inttypes.h>
|
|
}
|
|
|
|
#include <TM1637Display.h>
|
|
#include <Arduino.h>
|
|
|
|
TM1637Display::TM1637Display(uint8_t pinClk, uint8_t pinDIO)
|
|
{
|
|
// Copy the pin numbers
|
|
m_pinClk = pinClk;
|
|
m_pinDIO = pinDIO;
|
|
|
|
// Set the pin direction and default value.
|
|
// Both pins are set as inputs, allowing the pull-up resistors to pull them up
|
|
pinMode(m_pinClk, INPUT);
|
|
pinMode(m_pinDIO,INPUT);
|
|
digitalWrite(m_pinClk, LOW);
|
|
digitalWrite(m_pinDIO, LOW);
|
|
}
|
|
|
|
void TM1637Display::setBrightness(uint8_t brightness)
|
|
{
|
|
m_brightness = brightness;
|
|
}
|
|
|
|
void TM1637Display::setSegments(uint8_t segments[], uint8_t length, uint8_t pos)
|
|
{
|
|
start();
|
|
if (!writeByte(0x40))
|
|
digitalWrite(13,HIGH);
|
|
stop();
|
|
start();
|
|
writeByte(0xc0);
|
|
writeByte(0xff);
|
|
writeByte(0xff);
|
|
writeByte(0x00);
|
|
writeByte(0xff);
|
|
stop();
|
|
start();
|
|
writeByte(0x8f);
|
|
stop();
|
|
}
|
|
|
|
void TM1637Display::showNumberDec(int num, bool leading_zero, uint8_t length, uint8_t pos)
|
|
{
|
|
}
|
|
|
|
void TM1637Display::bitDelay()
|
|
{
|
|
delay(1);
|
|
// int i;
|
|
// for(i = 0; i < 250; i++);
|
|
}
|
|
|
|
void TM1637Display::start()
|
|
{
|
|
pinMode(m_pinDIO, OUTPUT);
|
|
bitDelay();
|
|
}
|
|
|
|
void TM1637Display::stop()
|
|
{
|
|
pinMode(m_pinDIO, OUTPUT);
|
|
bitDelay();
|
|
pinMode(m_pinClk, INPUT);
|
|
bitDelay();
|
|
pinMode(m_pinDIO, INPUT);
|
|
bitDelay();
|
|
}
|
|
|
|
bool TM1637Display::writeByte(uint8_t b)
|
|
{
|
|
uint8_t data = b;
|
|
|
|
// 8 Data Bits
|
|
for(uint8_t i = 0; i < 8; i++) {
|
|
// CLK low
|
|
pinMode(m_pinClk, OUTPUT);
|
|
bitDelay();
|
|
|
|
// Set data bit
|
|
if (data & 0x01)
|
|
pinMode(m_pinDIO, INPUT);
|
|
else
|
|
pinMode(m_pinDIO, OUTPUT);
|
|
|
|
bitDelay();
|
|
|
|
// CLK high
|
|
pinMode(m_pinClk, INPUT);
|
|
bitDelay();
|
|
data = data >> 1;
|
|
}
|
|
|
|
// Wait for acknowledge
|
|
// CLK to zero
|
|
pinMode(m_pinClk, OUTPUT);
|
|
pinMode(m_pinDIO, INPUT);
|
|
bitDelay();
|
|
|
|
// CLK to high
|
|
pinMode(m_pinClk, INPUT);
|
|
bitDelay();
|
|
uint8_t ack = digitalRead(m_pinDIO);
|
|
if (ack == 0)
|
|
pinMode(m_pinDIO, OUTPUT);
|
|
|
|
|
|
bitDelay();
|
|
pinMode(m_pinClk, OUTPUT);
|
|
bitDelay();
|
|
|
|
return ack;
|
|
}
|
|
|
|
|