mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-22 23:56:13 +01:00
Cleanup to one class
This commit is contained in:
parent
3094074349
commit
312fc255e4
121
IO_TM1638.cpp
121
IO_TM1638.cpp
|
@ -21,7 +21,6 @@
|
||||||
#include "IODevice.h"
|
#include "IODevice.h"
|
||||||
#include "DIAG.h"
|
#include "DIAG.h"
|
||||||
#include "IO_TM1638.h"
|
#include "IO_TM1638.h"
|
||||||
#include "TM1638x.h"
|
|
||||||
|
|
||||||
const uint8_t HIGHFLASH _digits[16]={
|
const uint8_t HIGHFLASH _digits[16]={
|
||||||
0b00111111,0b00000110,0b01011011,0b01001111,
|
0b00111111,0b00000110,0b01011011,0b01001111,
|
||||||
|
@ -34,7 +33,14 @@ const uint8_t HIGHFLASH _digits[16]={
|
||||||
TM1638::TM1638(VPIN firstVpin, byte clk_pin,byte dio_pin,byte stb_pin){
|
TM1638::TM1638(VPIN firstVpin, byte clk_pin,byte dio_pin,byte stb_pin){
|
||||||
_firstVpin = firstVpin;
|
_firstVpin = firstVpin;
|
||||||
_nPins = 8;
|
_nPins = 8;
|
||||||
tm=new TM1638x(clk_pin,dio_pin,stb_pin);
|
_clk_pin = clk_pin;
|
||||||
|
_stb_pin = stb_pin;
|
||||||
|
_dio_pin = dio_pin;
|
||||||
|
pinMode(clk_pin,OUTPUT);
|
||||||
|
pinMode(stb_pin,OUTPUT);
|
||||||
|
pinMode(dio_pin,OUTPUT);
|
||||||
|
_pulse = PULSE1_16;
|
||||||
|
|
||||||
_buttons=0;
|
_buttons=0;
|
||||||
_leds=0;
|
_leds=0;
|
||||||
_lastLoop=micros();
|
_lastLoop=micros();
|
||||||
|
@ -48,18 +54,17 @@ const uint8_t HIGHFLASH _digits[16]={
|
||||||
}
|
}
|
||||||
|
|
||||||
void TM1638::_begin() {
|
void TM1638::_begin() {
|
||||||
tm->displayClear();
|
displayClear();
|
||||||
tm->test();
|
test();
|
||||||
_display();
|
_display();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void TM1638::_loop(unsigned long currentMicros) {
|
void TM1638::_loop(unsigned long currentMicros) {
|
||||||
if (currentMicros - _lastLoop > (1000000UL/LoopHz)) {
|
if (currentMicros - _lastLoop > (1000000UL/LoopHz)) {
|
||||||
_buttons=tm->getButtons();// Read the buttons
|
_buttons=getButtons();// Read the buttons
|
||||||
_lastLoop=currentMicros;
|
_lastLoop=currentMicros;
|
||||||
}
|
}
|
||||||
// DIAG(F("TM1638 buttons %x"),_buttons);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TM1638::_display() {
|
void TM1638::_display() {
|
||||||
|
@ -77,14 +82,12 @@ int TM1638::_read(VPIN vpin) {
|
||||||
// digital write sets led state
|
// digital write sets led state
|
||||||
void TM1638::_write(VPIN vpin, int value) {
|
void TM1638::_write(VPIN vpin, int value) {
|
||||||
// TODO.. skip if no state change
|
// TODO.. skip if no state change
|
||||||
tm->writeLed(vpin - _firstVpin + 1,value!=0);
|
writeLed(vpin - _firstVpin + 1,value!=0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Analog write sets digit displays
|
// Analog write sets digit displays
|
||||||
|
|
||||||
void TM1638::_writeAnalogue(VPIN vpin, int lowBytes, uint8_t mode, uint16_t highBytes) {
|
void TM1638::_writeAnalogue(VPIN vpin, int lowBytes, uint8_t mode, uint16_t highBytes) {
|
||||||
DIAG(F("TM1638 w(v=%d,l=%d,m=%d,h=%d,lx=%x,hx=%x"),
|
|
||||||
vpin,lowBytes,mode,highBytes,lowBytes,highBytes);
|
|
||||||
// mode is in DataFormat defined above.
|
// mode is in DataFormat defined above.
|
||||||
byte formatLength=mode & 0x0F; // last 4 bits
|
byte formatLength=mode & 0x0F; // last 4 bits
|
||||||
byte formatType=mode & 0xF0; //
|
byte formatType=mode & 0xF0; //
|
||||||
|
@ -98,21 +101,20 @@ void TM1638::_writeAnalogue(VPIN vpin, int lowBytes, uint8_t mode, uint16_t high
|
||||||
value<<=16;
|
value<<=16;
|
||||||
value |= (uint16_t)lowBytes;
|
value |= (uint16_t)lowBytes;
|
||||||
|
|
||||||
DIAG(F("TM1638 fl=%d ft=%x sd=%d ld=%d v=%l vx=%X"),
|
//DIAG(F("TM1638 fl=%d ft=%x sd=%d ld=%d v=%l vx=%X"),
|
||||||
formatLength,formatType,
|
// formatLength,formatType,startDigit,lastDigit,value,value);
|
||||||
startDigit,lastDigit,value,value);
|
|
||||||
while(startDigit<=lastDigit) {
|
while(startDigit<=lastDigit) {
|
||||||
switch (formatType) {
|
switch (formatType) {
|
||||||
case _DF_DECIMAL:// decimal (leading zeros)
|
case _DF_DECIMAL:// decimal (leading zeros)
|
||||||
tm->displayDig(startDigit,GETHIGHFLASH(_digits,(value%10)));
|
displayDig(startDigit,GETHIGHFLASH(_digits,(value%10)));
|
||||||
value=value/10;
|
value=value/10;
|
||||||
break;
|
break;
|
||||||
case _DF_HEX:// HEX (leading zeros)
|
case _DF_HEX:// HEX (leading zeros)
|
||||||
tm->displayDig(startDigit,GETHIGHFLASH(_digits,(value & 0x0F)));
|
displayDig(startDigit,GETHIGHFLASH(_digits,(value & 0x0F)));
|
||||||
value>>=4;
|
value>>=4;
|
||||||
break;
|
break;
|
||||||
case _DF_RAW:// Raw 7-segment pattern
|
case _DF_RAW:// Raw 7-segment pattern
|
||||||
tm->displayDig(startDigit,value & 0xFF);
|
displayDig(startDigit,value & 0xFF);
|
||||||
value>>=8;
|
value>>=8;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
@ -121,7 +123,92 @@ void TM1638::_writeAnalogue(VPIN vpin, int lowBytes, uint8_t mode, uint16_t high
|
||||||
}
|
}
|
||||||
startDigit++;
|
startDigit++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t TM1638::getButtons(){
|
||||||
|
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
||||||
|
writeData(INSTRUCTION_READ_KEY);
|
||||||
|
pinMode(_dio_pin, INPUT);
|
||||||
|
ArduinoPins::fastWriteDigital(_clk_pin, LOW);
|
||||||
|
uint8_t buttons=0;
|
||||||
|
for (uint8_t eachByte=0; eachByte<4;eachByte++) {
|
||||||
|
uint8_t value = 0;
|
||||||
|
for (uint8_t eachBit = 0; eachBit < 8; eachBit++) {
|
||||||
|
ArduinoPins::fastWriteDigital(_clk_pin, HIGH);
|
||||||
|
value |= ArduinoPins::fastReadDigital(_dio_pin) << eachBit;
|
||||||
|
ArduinoPins::fastWriteDigital(_clk_pin, LOW);
|
||||||
|
}
|
||||||
|
buttons |= value << eachByte;
|
||||||
|
delayMicroseconds(1);
|
||||||
|
}
|
||||||
|
pinMode(_dio_pin, OUTPUT);
|
||||||
|
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
||||||
|
return buttons;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void TM1638::displayDig(uint8_t digitId, uint8_t pgfedcba){
|
||||||
|
if (digitId>7) return;
|
||||||
|
setDataInstruction(DISPLAY_TURN_ON | _pulse);
|
||||||
|
setDataInstruction(INSTRUCTION_WRITE_DATA| INSTRUCTION_ADDRESS_FIXED);
|
||||||
|
writeDataAt(FIRST_DISPLAY_ADDRESS+14-(digitId*2), pgfedcba);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TM1638::displayClear(){
|
||||||
|
setDataInstruction(DISPLAY_TURN_ON | _pulse);
|
||||||
|
setDataInstruction(INSTRUCTION_WRITE_DATA | INSTRUCTION_ADDRESS_FIXED);
|
||||||
|
for (uint8_t i=0;i<15;i+=2){
|
||||||
|
writeDataAt(FIRST_DISPLAY_ADDRESS+i,0x00);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TM1638::writeLed(uint8_t num,bool state){
|
||||||
|
if ((num<1) | (num>8)) return;
|
||||||
|
setDataInstruction(DISPLAY_TURN_ON | _pulse);
|
||||||
|
setDataInstruction(INSTRUCTION_WRITE_DATA | INSTRUCTION_ADDRESS_FIXED);
|
||||||
|
writeDataAt(FIRST_DISPLAY_ADDRESS + (num*2-1), state);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void TM1638::writeData(uint8_t data){
|
||||||
|
for (uint8_t i = 0; i < 8; i++) {
|
||||||
|
ArduinoPins::fastWriteDigital(_dio_pin, data & 1);
|
||||||
|
data >>= 1;
|
||||||
|
ArduinoPins::fastWriteDigital(_clk_pin, HIGH);
|
||||||
|
ArduinoPins::fastWriteDigital(_clk_pin, LOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TM1638::writeDataAt(uint8_t displayAddress, uint8_t data){
|
||||||
|
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
||||||
|
writeData(displayAddress);
|
||||||
|
writeData(data);
|
||||||
|
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
||||||
|
delayMicroseconds(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TM1638::setDataInstruction(uint8_t dataInstruction){
|
||||||
|
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
||||||
|
writeData(dataInstruction);
|
||||||
|
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
||||||
|
delayMicroseconds(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TM1638::test(){
|
||||||
|
DIAG(F("TM1638 test"));
|
||||||
|
uint8_t val=0;
|
||||||
|
for(uint8_t i=0;i<5;i++){
|
||||||
|
setDataInstruction(DISPLAY_TURN_ON | _pulse);
|
||||||
|
setDataInstruction(INSTRUCTION_WRITE_DATA| INSTRUCTION_ADDRESS_AUTO);
|
||||||
|
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
||||||
|
writeData(FIRST_DISPLAY_ADDRESS);
|
||||||
|
for(uint8_t i=0;i<16;i++)
|
||||||
|
writeData(val);
|
||||||
|
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
||||||
|
delay(1000);
|
||||||
|
val = ~val;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
72
IO_TM1638.h
72
IO_TM1638.h
|
@ -22,16 +22,35 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "IODevice.h"
|
#include "IODevice.h"
|
||||||
#include "DIAG.h"
|
#include "DIAG.h"
|
||||||
#include "TM1638x.h"
|
|
||||||
|
|
||||||
class TM1638 : public IODevice {
|
class TM1638 : public IODevice {
|
||||||
private:
|
private:
|
||||||
|
|
||||||
TM1638x * tm;
|
|
||||||
uint8_t _buttons;
|
uint8_t _buttons;
|
||||||
uint8_t _leds;
|
uint8_t _leds;
|
||||||
unsigned long _lastLoop;
|
unsigned long _lastLoop;
|
||||||
static const int LoopHz=20;
|
static const int LoopHz=20;
|
||||||
|
|
||||||
|
static const byte
|
||||||
|
INSTRUCTION_WRITE_DATA=0x40,
|
||||||
|
INSTRUCTION_READ_KEY=0x42,
|
||||||
|
INSTRUCTION_ADDRESS_AUTO=0x40,
|
||||||
|
INSTRUCTION_ADDRESS_FIXED=0x44,
|
||||||
|
INSTRUCTION_NORMAL_MODE=0x40,
|
||||||
|
INSTRUCTION_TEST_MODE=0x48,
|
||||||
|
|
||||||
|
FIRST_DISPLAY_ADDRESS=0xC0,
|
||||||
|
|
||||||
|
DISPLAY_TURN_OFF=0x80,
|
||||||
|
DISPLAY_TURN_ON=0x88;
|
||||||
|
|
||||||
|
|
||||||
|
uint8_t _clk_pin;
|
||||||
|
uint8_t _stb_pin;
|
||||||
|
uint8_t _dio_pin;
|
||||||
|
uint8_t _pulse;
|
||||||
|
bool _isOn;
|
||||||
|
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
TM1638(VPIN firstVpin, byte clk_pin,byte dio_pin,byte stb_pin);
|
TM1638(VPIN firstVpin, byte clk_pin,byte dio_pin,byte stb_pin);
|
||||||
|
@ -64,5 +83,52 @@ public:
|
||||||
void _display() override ;
|
void _display() override ;
|
||||||
int _read(VPIN pin) override;
|
int _read(VPIN pin) override;
|
||||||
void _write(VPIN pin,int value) override;
|
void _write(VPIN pin,int value) override;
|
||||||
|
|
||||||
|
// Device driving functions
|
||||||
|
private:
|
||||||
|
enum pulse_t {
|
||||||
|
PULSE1_16,
|
||||||
|
PULSE2_16,
|
||||||
|
PULSE4_16,
|
||||||
|
PULSE10_16,
|
||||||
|
PULSE11_16,
|
||||||
|
PULSE12_16,
|
||||||
|
PULSE13_16,
|
||||||
|
PULSE14_16
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @fn getButtons
|
||||||
|
* @return state of 8 buttons
|
||||||
|
*/
|
||||||
|
uint8_t getButtons();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @fn writeLed
|
||||||
|
* @brief put led ON or OFF
|
||||||
|
* @param num num of led(1-8)
|
||||||
|
* @param state (true or false)
|
||||||
|
*/
|
||||||
|
void writeLed(uint8_t num, bool state);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @fn displayDig
|
||||||
|
* @brief set 7 segment display + dot
|
||||||
|
* @param digitId num of digit(0-7)
|
||||||
|
* @param val value 8 bits
|
||||||
|
*/
|
||||||
|
void displayDig(uint8_t digitId, uint8_t pgfedcba);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @fn displayClear
|
||||||
|
* @brief switch off all leds and segment display
|
||||||
|
*/
|
||||||
|
void displayClear();
|
||||||
|
void test();
|
||||||
|
void writeData(uint8_t data);
|
||||||
|
void writeDataAt(uint8_t displayAddress, uint8_t data);
|
||||||
|
void setDisplayMode(uint8_t displayMode);
|
||||||
|
void setDataInstruction(uint8_t dataInstruction);
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
|
|
186
IO_TM1638.hzz
186
IO_TM1638.hzz
|
@ -1,186 +0,0 @@
|
||||||
/*
|
|
||||||
* © 2024, Henk Kruisbrink & Chris Harlow. All rights reserved.
|
|
||||||
* © 2023, Neil McKechnie. All rights reserved.
|
|
||||||
*
|
|
||||||
* This file is part of DCC++EX API
|
|
||||||
*
|
|
||||||
* This is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* It 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 General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
*
|
|
||||||
* Dec 2023, Added NXP SC16IS752 I2C Dual UART
|
|
||||||
* The SC16IS752 has 64 bytes TX & RX FIFO buffer
|
|
||||||
* First version without interrupts from I2C UART and only RX/TX are used, interrupts may not be
|
|
||||||
* needed as the RX Fifo holds the reply
|
|
||||||
*
|
|
||||||
* Jan 2024, Issue with using both UARTs simultaniously, the secod uart seems to work but the first transmit
|
|
||||||
* corrupt data. This need more analysis and experimenatation.
|
|
||||||
* Will push this driver to the dev branch with the uart fixed to 0
|
|
||||||
* Both SC16IS750 (single uart) and SC16IS752 (dual uart, but only uart 0 is enable)
|
|
||||||
*
|
|
||||||
* myHall.cpp configuration syntax:
|
|
||||||
*
|
|
||||||
* I2CRailcom::create(1st vPin, vPins, I2C address);
|
|
||||||
*
|
|
||||||
* myAutomation configuration
|
|
||||||
* HAL(I2CRailcom, 1st vPin, vPins, I2C address)
|
|
||||||
* Parameters:
|
|
||||||
* 1st vPin : First virtual pin that EX-Rail can control to play a sound, use PLAYSOUND command (alias of ANOUT)
|
|
||||||
* vPins : Total number of virtual pins allocated (2 vPins are supported, one for each UART)
|
|
||||||
* 1st vPin for UART 0, 2nd for UART 1
|
|
||||||
* I2C Address : I2C address of the serial controller, in 0x format
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef IO_TM1638_h
|
|
||||||
#define IO_TM1638_h
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include "IODevice.h"
|
|
||||||
#include "I2CManager.h"
|
|
||||||
#include "DIAG.h"
|
|
||||||
|
|
||||||
class TM1638 : public IODevice {
|
|
||||||
private:
|
|
||||||
|
|
||||||
static const uint8_t
|
|
||||||
INSTRUCTION_WRITE_DATA=0x40,
|
|
||||||
INSTRUCTION_READ_KEY=0x42,
|
|
||||||
INSTRUCTION_ADDRESS_AUTO=0x40,
|
|
||||||
INSTRUCTION_ADDRESS_FIXED=0x44,
|
|
||||||
INSTRUCTION_NORMAL_MODE=0x40,
|
|
||||||
INSTRUCTION_TEST_MODE=0x48,
|
|
||||||
FIRST_DISPLAY_ADDRESS=0xC0,
|
|
||||||
DISPLAY_TURN_OFF=0x80,
|
|
||||||
DISPLAY_TURN_ON=0x88;
|
|
||||||
|
|
||||||
static constexpr uint8_t _digits[16]={ // 7-segment hex
|
|
||||||
0b00111111,0b00000110,0b01011011,0b01001111,
|
|
||||||
0b01100110,0b01101101,0b01111101,0b00000111,
|
|
||||||
0b01111111,0b01101111,0b01110111,0b01111100,
|
|
||||||
0b00111001,0b01011110,0b01111001,0b01110001
|
|
||||||
};
|
|
||||||
|
|
||||||
uint8_t _clk_pin;
|
|
||||||
uint8_t _stb_pin;
|
|
||||||
uint8_t _dio_pin;
|
|
||||||
uint8_t _buttons;
|
|
||||||
uint8_t _leds;
|
|
||||||
uint8_t _pulse;
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
|
||||||
// Constructor
|
|
||||||
TM1638(VPIN firstVpin, byte clk_pin,byte stb_pin,byte dio_pin){
|
|
||||||
_firstVpin = firstVpin;
|
|
||||||
_nPins = 8;
|
|
||||||
_clk_pin=clk_pin;
|
|
||||||
_stb_pin=stb_pin;
|
|
||||||
_dio_pin=dio_pin;
|
|
||||||
_buttons=0;
|
|
||||||
_leds=0;
|
|
||||||
_pulse=4;
|
|
||||||
pinMode(stb_pin, OUTPUT);
|
|
||||||
pinMode(clk_pin, OUTPUT);
|
|
||||||
pinMode(dio_pin, OUTPUT);
|
|
||||||
ArduinoPins::fastWriteDigital(stb_pin, HIGH);
|
|
||||||
ArduinoPins::fastWriteDigital(clk_pin, HIGH);
|
|
||||||
ArduinoPins::fastWriteDigital(dio_pin, HIGH);
|
|
||||||
addDevice(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
static void create(VPIN firstVpin, byte clk_pin,byte stb_pin,byte dio_pin) {
|
|
||||||
if (checkNoOverlap(firstVpin,8))
|
|
||||||
new TM1638(firstVpin, clk_pin,stb_pin,dio_pin);
|
|
||||||
}
|
|
||||||
|
|
||||||
void _begin() override {
|
|
||||||
_deviceState = DEVSTATE_NORMAL;
|
|
||||||
writeCommand(DISPLAY_TURN_ON | _pulse);
|
|
||||||
_display();
|
|
||||||
}
|
|
||||||
|
|
||||||
void strobe(bool on) {
|
|
||||||
ArduinoPins::fastWriteDigital(_stb_pin, on?LOW:HIGH); // select device
|
|
||||||
delayMicroseconds(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void _loop(unsigned long currentMicros) override {
|
|
||||||
|
|
||||||
// Read the buttons
|
|
||||||
strobe(true);
|
|
||||||
writeData(INSTRUCTION_READ_KEY);
|
|
||||||
pinMode(_dio_pin, INPUT);
|
|
||||||
|
|
||||||
_buttons=0;
|
|
||||||
for (uint8_t i=0; 4;i++){
|
|
||||||
auto data = readData();
|
|
||||||
_buttons |= ((data>>4) & 0x01) | (data & 0x01);
|
|
||||||
_buttons <<= 2;
|
|
||||||
}
|
|
||||||
pinMode(_dio_pin,OUTPUT);
|
|
||||||
strobe(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
void _display() override {
|
|
||||||
DIAG(F("TM1638 Configured on Vpins:%u-%u"), _firstVpin, _firstVpin+_nPins-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// digital read gets button state
|
|
||||||
int _read(VPIN vpin) override {
|
|
||||||
return bitRead(_buttons,vpin - _firstVpin);
|
|
||||||
}
|
|
||||||
|
|
||||||
// digital write sets led state
|
|
||||||
void _write(VPIN vpin, int value) override {
|
|
||||||
auto pin=vpin-_firstVpin;
|
|
||||||
writeCommand(INSTRUCTION_WRITE_DATA | INSTRUCTION_ADDRESS_FIXED);
|
|
||||||
strobe(true);
|
|
||||||
writeData(FIRST_DISPLAY_ADDRESS + (pin*2+1));
|
|
||||||
writeData(value?0x01:0x00);
|
|
||||||
strobe(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
void writeCommand(uint8_t val)
|
|
||||||
{
|
|
||||||
strobe(true);
|
|
||||||
writeData(val);
|
|
||||||
strobe(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
void writeData(uint8_t val)
|
|
||||||
{
|
|
||||||
ArduinoPins::fastWriteDigital(_clk_pin, LOW);
|
|
||||||
for (uint8_t i = 0; i < 8; i++) {
|
|
||||||
ArduinoPins::fastWriteDigital(_dio_pin, val & 1);
|
|
||||||
val >>= 1;
|
|
||||||
ArduinoPins::fastWriteDigital(_clk_pin, HIGH);
|
|
||||||
ArduinoPins::fastWriteDigital(_clk_pin, LOW);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t readData() {
|
|
||||||
uint8_t value = 0;
|
|
||||||
ArduinoPins::fastWriteDigital(_clk_pin, LOW);
|
|
||||||
for (uint8_t i = 0; i < 8; ++i) {
|
|
||||||
ArduinoPins::fastWriteDigital(_clk_pin, HIGH);
|
|
||||||
value |= ArduinoPins::fastReadDigital(_dio_pin) << i;
|
|
||||||
ArduinoPins::fastWriteDigital(_clk_pin, LOW);
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
};
|
|
||||||
#endif // IO_I2CRailcom_h
|
|
111
TM1638x.cpp
111
TM1638x.cpp
|
@ -1,111 +0,0 @@
|
||||||
#include "Arduino.h"
|
|
||||||
#include "TM1638x.h"
|
|
||||||
#include "DIAG.h"
|
|
||||||
#include "IODevice.h"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// buttons K3/KS1-8
|
|
||||||
uint8_t TM1638x::getButtons(){
|
|
||||||
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
|
||||||
writeData(INSTRUCTION_READ_KEY);
|
|
||||||
pinMode(_dio_pin, INPUT);
|
|
||||||
ArduinoPins::fastWriteDigital(_clk_pin, LOW);
|
|
||||||
_buttons=0;
|
|
||||||
for (uint8_t eachByte=0; eachByte<4;eachByte++) {
|
|
||||||
uint8_t value = 0;
|
|
||||||
for (uint8_t eachBit = 0; eachBit < 8; eachBit++) {
|
|
||||||
ArduinoPins::fastWriteDigital(_clk_pin, HIGH);
|
|
||||||
value |= ArduinoPins::fastReadDigital(_dio_pin) << eachBit;
|
|
||||||
ArduinoPins::fastWriteDigital(_clk_pin, LOW);
|
|
||||||
}
|
|
||||||
_buttons |= value << eachByte;
|
|
||||||
delayMicroseconds(1);
|
|
||||||
}
|
|
||||||
pinMode(_dio_pin, OUTPUT);
|
|
||||||
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
|
||||||
return _buttons;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void TM1638x::displayDig(uint8_t digitId, uint8_t pgfedcba){
|
|
||||||
if (digitId>7) return;
|
|
||||||
setDataInstruction(DISPLAY_TURN_ON | _pulse);
|
|
||||||
setDataInstruction(INSTRUCTION_WRITE_DATA| INSTRUCTION_ADDRESS_FIXED);
|
|
||||||
writeDataAt(FIRST_DISPLAY_ADDRESS+14-(digitId*2), pgfedcba);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638x::displayClear(){
|
|
||||||
setDataInstruction(DISPLAY_TURN_ON | _pulse);
|
|
||||||
setDataInstruction(INSTRUCTION_WRITE_DATA | INSTRUCTION_ADDRESS_FIXED);
|
|
||||||
for (uint8_t i=0;i<15;i+=2){
|
|
||||||
writeDataAt(FIRST_DISPLAY_ADDRESS+i,0x00);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638x::writeLed(uint8_t num,bool state){
|
|
||||||
if ((num<1) | (num>8)) return;
|
|
||||||
setDataInstruction(DISPLAY_TURN_ON | _pulse);
|
|
||||||
setDataInstruction(INSTRUCTION_WRITE_DATA | INSTRUCTION_ADDRESS_FIXED);
|
|
||||||
writeDataAt(FIRST_DISPLAY_ADDRESS + (num*2-1), state);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638x::displayTurnOn(){
|
|
||||||
setDataInstruction(DISPLAY_TURN_ON | _pulse);
|
|
||||||
_isOn = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638x::displayTurnOff(){
|
|
||||||
setDataInstruction(DISPLAY_TURN_OFF | _pulse);
|
|
||||||
_isOn = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638x::displaySetBrightness(pulse_t newpulse){
|
|
||||||
if ((newpulse<PULSE1_16) | (newpulse>PULSE14_16)) return;
|
|
||||||
_pulse = newpulse;
|
|
||||||
uint8_t data = (_isOn) ? DISPLAY_TURN_ON : DISPLAY_TURN_OFF;
|
|
||||||
data |= _pulse;
|
|
||||||
setDataInstruction(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638x::writeData(uint8_t data){
|
|
||||||
for (uint8_t i = 0; i < 8; i++) {
|
|
||||||
ArduinoPins::fastWriteDigital(_dio_pin, data & 1);
|
|
||||||
data >>= 1;
|
|
||||||
ArduinoPins::fastWriteDigital(_clk_pin, HIGH);
|
|
||||||
ArduinoPins::fastWriteDigital(_clk_pin, LOW);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638x::writeDataAt(uint8_t displayAddress, uint8_t data){
|
|
||||||
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
|
||||||
writeData(displayAddress);
|
|
||||||
writeData(data);
|
|
||||||
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
|
||||||
delayMicroseconds(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638x::setDataInstruction(uint8_t dataInstruction){
|
|
||||||
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
|
||||||
writeData(dataInstruction);
|
|
||||||
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
|
||||||
delayMicroseconds(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void TM1638x::test(){
|
|
||||||
DIAG(F("TM1638x test"));
|
|
||||||
uint8_t val=0;
|
|
||||||
for(uint8_t i=0;i<5;i++){
|
|
||||||
//setDataInstruction(DISPLAY_TURN_ON | _pulse);
|
|
||||||
displayTurnOn();
|
|
||||||
setDataInstruction(INSTRUCTION_WRITE_DATA| INSTRUCTION_ADDRESS_AUTO);
|
|
||||||
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
|
||||||
writeData(FIRST_DISPLAY_ADDRESS);
|
|
||||||
for(uint8_t i=0;i<16;i++)
|
|
||||||
writeData(val);
|
|
||||||
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
|
||||||
delay(1000);
|
|
||||||
val = ~val;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
144
TM1638x.h
144
TM1638x.h
|
@ -1,144 +0,0 @@
|
||||||
/*!
|
|
||||||
* @file TM1638.h
|
|
||||||
* @brief Arduino library for interface with TM1638 chip.
|
|
||||||
* @n read buttons, switch leds, display on 7segment.
|
|
||||||
* @author [Damien](web@varrel.fr)
|
|
||||||
* @version V1.0.1
|
|
||||||
* @date 2024-02-06
|
|
||||||
* @url https://github.com/dvarrel/TM1638.git
|
|
||||||
* @module https://fr.aliexpress.com/item/32832772646.html
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef _TM1638_H
|
|
||||||
#define _TM1638_H
|
|
||||||
#include "Arduino.h"
|
|
||||||
|
|
||||||
#ifndef ON
|
|
||||||
#define ON 1
|
|
||||||
#endif
|
|
||||||
#ifndef OFF
|
|
||||||
#define OFF 0
|
|
||||||
#endif
|
|
||||||
|
|
||||||
typedef enum{
|
|
||||||
PULSE1_16,
|
|
||||||
PULSE2_16,
|
|
||||||
PULSE4_16,
|
|
||||||
PULSE10_16,
|
|
||||||
PULSE11_16,
|
|
||||||
PULSE12_16,
|
|
||||||
PULSE13_16,
|
|
||||||
PULSE14_16
|
|
||||||
} pulse_t;
|
|
||||||
|
|
||||||
typedef enum{
|
|
||||||
S1,S2,S3,S4,
|
|
||||||
S5,S6,S7,S8
|
|
||||||
} button_t;
|
|
||||||
|
|
||||||
class TM1638x{
|
|
||||||
private:
|
|
||||||
static const byte
|
|
||||||
INSTRUCTION_WRITE_DATA=0x40,
|
|
||||||
INSTRUCTION_READ_KEY=0x42,
|
|
||||||
INSTRUCTION_ADDRESS_AUTO=0x40,
|
|
||||||
INSTRUCTION_ADDRESS_FIXED=0x44,
|
|
||||||
INSTRUCTION_NORMAL_MODE=0x40,
|
|
||||||
INSTRUCTION_TEST_MODE=0x48,
|
|
||||||
|
|
||||||
FIRST_DISPLAY_ADDRESS=0xC0,
|
|
||||||
|
|
||||||
DISPLAY_TURN_OFF=0x80,
|
|
||||||
DISPLAY_TURN_ON=0x88;
|
|
||||||
|
|
||||||
|
|
||||||
uint8_t _clk_pin;
|
|
||||||
uint8_t _stb_pin;
|
|
||||||
uint8_t _dio_pin;
|
|
||||||
uint8_t _buttons;
|
|
||||||
uint8_t _pulse;
|
|
||||||
bool _isOn;
|
|
||||||
|
|
||||||
public:
|
|
||||||
TM1638x(uint8_t clk_pin, uint8_t dio_pin, uint8_t stb_pin){
|
|
||||||
_clk_pin = clk_pin;
|
|
||||||
_stb_pin = stb_pin;
|
|
||||||
_dio_pin = dio_pin;
|
|
||||||
_pulse = PULSE1_16;
|
|
||||||
_isOn = false;
|
|
||||||
|
|
||||||
pinMode(stb_pin, OUTPUT);
|
|
||||||
pinMode(clk_pin, OUTPUT);
|
|
||||||
pinMode(dio_pin, OUTPUT);
|
|
||||||
digitalWrite(stb_pin, HIGH);
|
|
||||||
digitalWrite(clk_pin, HIGH);
|
|
||||||
digitalWrite(dio_pin, HIGH);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @fn getButtons
|
|
||||||
* @return state of 8 buttons
|
|
||||||
*/
|
|
||||||
uint8_t getButtons();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @fn writeLed
|
|
||||||
* @brief put led ON or OFF
|
|
||||||
* @param num num of led(1-8)
|
|
||||||
* @param state (true or false)
|
|
||||||
*/
|
|
||||||
void writeLed(uint8_t num, bool state);
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @fn displayDig
|
|
||||||
* @brief set 7 segment display + dot
|
|
||||||
* @param digitId num of digit(0-7)
|
|
||||||
* @param val value 8 bits
|
|
||||||
*/
|
|
||||||
void displayDig(uint8_t digitId, uint8_t pgfedcba);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @fn displayClear
|
|
||||||
* @brief switch off all leds and segment display
|
|
||||||
*/
|
|
||||||
void displayClear();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @fn displayTurnOff
|
|
||||||
* @brief turn on lights
|
|
||||||
*/
|
|
||||||
void displayTurnOff();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @fn displayTurnOn
|
|
||||||
* @brief turn off lights
|
|
||||||
*/
|
|
||||||
void displayTurnOn();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @fn displaySetBrightness
|
|
||||||
* @brief set display brightness
|
|
||||||
* @param pulse_t (0-7)
|
|
||||||
*/
|
|
||||||
void displaySetBrightness(pulse_t pulse);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @fn reset
|
|
||||||
* @brief switch off all displays-leds
|
|
||||||
*/
|
|
||||||
void reset();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @fn test
|
|
||||||
* @brief blink all displays and leds
|
|
||||||
*/
|
|
||||||
void test();
|
|
||||||
|
|
||||||
private:
|
|
||||||
void writeData(uint8_t data);
|
|
||||||
void writeDataAt(uint8_t displayAddress, uint8_t data);
|
|
||||||
void setDisplayMode(uint8_t displayMode);
|
|
||||||
void setDataInstruction(uint8_t dataInstruction);
|
|
||||||
};
|
|
||||||
#endif
|
|
Loading…
Reference in New Issue
Block a user