1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-26 17:46:14 +01:00
CommandStation-EX/TM1638x.cpp

127 lines
3.6 KiB
C++
Raw Normal View History

2024-10-04 10:27:46 +02:00
#include "Arduino.h"
#include "TM1638x.h"
#include "DIAG.h"
2024-10-05 19:27:43 +02:00
#include "IODevice.h"
2024-10-04 10:27:46 +02:00
// buttons K3/KS1-8
uint8_t TM1638x::getButtons(){
2024-10-05 19:27:43 +02:00
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
2024-10-04 10:27:46 +02:00
writeData(INSTRUCTION_READ_KEY);
pinMode(_dio_pin, INPUT);
2024-10-05 19:27:43 +02:00
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;
2024-10-04 10:27:46 +02:00
delayMicroseconds(1);
}
pinMode(_dio_pin, OUTPUT);
2024-10-05 19:27:43 +02:00
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
2024-10-04 10:27:46 +02:00
return _buttons;
}
void TM1638x::reset(){
setDisplayMode(DISPLAY_TURN_ON | _pulse);
setDataInstruction(INSTRUCTION_WRITE_DATA| INSTRUCTION_ADDRESS_AUTO);
2024-10-05 19:27:43 +02:00
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
2024-10-04 10:27:46 +02:00
writeData(FIRST_DISPLAY_ADDRESS);
for(uint8_t i=0;i<16;i++)
writeData(0);
2024-10-05 19:27:43 +02:00
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
2024-10-04 10:27:46 +02:00
}
void TM1638x::displayDig(uint8_t digitId, uint8_t pgfedcba){
if (digitId>7) return;
setDisplayMode(DISPLAY_TURN_ON | _pulse);
setDataInstruction(INSTRUCTION_WRITE_DATA| INSTRUCTION_ADDRESS_FIXED);
writeDataAt(FIRST_DISPLAY_ADDRESS+14-(digitId*2), pgfedcba);
}
void TM1638x::displayClear(){
setDisplayMode(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){
//DIAG(F("TM1638x writeLed(%d,%d)"),num,state);
if ((num<1) | (num>8)) return;
setDisplayMode(DISPLAY_TURN_ON | _pulse);
setDataInstruction(INSTRUCTION_WRITE_DATA | INSTRUCTION_ADDRESS_FIXED);
writeDataAt(FIRST_DISPLAY_ADDRESS + (num*2-1), state);
}
void TM1638x::displayTurnOn(){
setDisplayMode(DISPLAY_TURN_ON | _pulse);
_isOn = true;
}
void TM1638x::displayTurnOff(){
setDisplayMode(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;
setDisplayMode(data);
}
void TM1638x::writeData(uint8_t data){
2024-10-05 19:27:43 +02:00
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);
}
2024-10-04 10:27:46 +02:00
}
void TM1638x::writeDataAt(uint8_t displayAddress, uint8_t data){
2024-10-05 19:27:43 +02:00
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
2024-10-04 10:27:46 +02:00
writeData(displayAddress);
writeData(data);
2024-10-05 19:27:43 +02:00
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
2024-10-04 10:27:46 +02:00
delayMicroseconds(1);
}
void TM1638x::setDisplayMode(uint8_t displayMode){
2024-10-05 19:27:43 +02:00
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
2024-10-04 10:27:46 +02:00
writeData(displayMode);
2024-10-05 19:27:43 +02:00
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
2024-10-04 10:27:46 +02:00
delayMicroseconds(1);
}
void TM1638x::setDataInstruction(uint8_t dataInstruction){
2024-10-05 19:27:43 +02:00
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
2024-10-04 10:27:46 +02:00
writeData(dataInstruction);
2024-10-05 19:27:43 +02:00
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
2024-10-04 10:27:46 +02:00
delayMicroseconds(1);
}
void TM1638x::test(){
DIAG(F("TM1638x test"));
uint8_t val=0;
for(uint8_t i=0;i<5;i++){
//setDisplayMode(DISPLAY_TURN_ON | _pulse);
displayTurnOn();
setDataInstruction(INSTRUCTION_WRITE_DATA| INSTRUCTION_ADDRESS_AUTO);
2024-10-05 19:27:43 +02:00
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
2024-10-04 10:27:46 +02:00
writeData(FIRST_DISPLAY_ADDRESS);
for(uint8_t i=0;i<16;i++)
writeData(val);
2024-10-05 19:27:43 +02:00
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
2024-10-04 10:27:46 +02:00
delay(1000);
val = ~val;
}
}