1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-23 12:51:24 +01:00
This commit is contained in:
Asbelos 2024-10-04 13:16:57 +01:00
parent fc4df87848
commit 7e7c00594b
3 changed files with 42 additions and 3 deletions

View File

@ -1,6 +1,5 @@
/*
* © 2024, Henk Kruisbrink & Chris Harlow. All rights reserved.
* © 2023, Neil McKechnie. All rights reserved.
* © 2024, Chris Harlow. All rights reserved.
*
* This file is part of DCC++EX API
*
@ -22,9 +21,9 @@
#define IO_TM1638_h
#include <Arduino.h>
#include "IODevice.h"
#include "I2CManager.h"
#include "DIAG.h"
#include "TM1638x.h"
class TM1638 : public IODevice {
private:
@ -81,8 +80,47 @@ int _read(VPIN vpin) override {
// digital write sets led state
void _write(VPIN vpin, int value) override {
// TODO.. skip if no state change
tm->writeLed(vpin - _firstVpin + 1,value!=0);
}
// Analog write sets digit displays
void _writeAnalogue(VPIN vpin, int lowBytes, uint8_t mode, uint16_t highBytes) override {
DIAG(F("TM1638 writeAnalogue(v=%d,l=%d,m=%d,h=%d"),vpin,lowBytes,mode,highBytes);
byte formatLength=mode & 0x0f;
byte formatType=mode>>4;
int8_t leftDigit=vpin-_firstVpin; // 0..7 from left
int8_t rightDigit=leftDigit+formatLength-1; // 0..7 from left
// loading is done right to left startDigit first
int8_t startDigit=7-rightDigit; // reverse as 7 on left
int8_t lastDigit=7-leftDigit; // reverse as 7 on left
auto value= ((uint32_t)highBytes<<16) | (uint32_t)lowBytes;
DIAG(F("TM1638 fl=%d ft=%x sd=%d ld=%d v=%l"),formatLength,formatType,
startDigit,lastDigit,value);
while(startDigit<=lastDigit) {
switch (formatType) {
case 0:// decimal (leading zeros)
tm->displayVal(startDigit,value%10);
value=value/10;
break;
case 1:// HEX (leading zeros)
tm->displayVal(startDigit,value & 0x0F);
value>>=4;
break;
case 2:// Raw 7-segment pattern
tm->displayDig(startDigit,value & 0xFF);
value>>=8;
break;
default:
tm->displayDig(startDigit,0);
break;
}
startDigit++;
}
}
};
#endif // IO_TM1638_h

View File

View File

@ -39,6 +39,7 @@ void TM1638x::reset(){
}
void TM1638x::displayVal(uint8_t digitId, uint8_t val){
DIAG(F("TM1638x displayVal(%d,%d)"),digitId,val);
if ((digitId>7) | (val>15) | (val<0)) return;
setDisplayMode(DISPLAY_TURN_ON | _pulse);
setDataInstruction(INSTRUCTION_WRITE_DATA| INSTRUCTION_ADDRESS_FIXED);