1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-30 03:26:13 +01:00
CommandStation-EX/IO_TM1638.h

135 lines
3.6 KiB
C
Raw Normal View History

Squashed commit of the following: commit f13824164b853b2fec66f67b7f3296e2264a861b Author: Asbelos <asbelos@btinternet.com> Date: Thu Oct 10 16:07:42 2024 +0100 _s7 keyword generator commit 8a7dc2643cd15a6d55dd1f257c4940d4167702fc Author: Asbelos <asbelos@btinternet.com> Date: Mon Oct 7 10:54:05 2024 +0100 comments commit 801cddfef70a5ced01da9f022d4b0ad603264e0a Author: Asbelos <asbelos@btinternet.com> Date: Sun Oct 6 13:24:07 2024 +0100 simpler macro insert commit 5883f474ee79274aaeca94aee13b2235accddbd6 Author: Asbelos <asbelos@btinternet.com> Date: Sun Oct 6 13:18:29 2024 +0100 Auto include commit 312fc255e4eb8aa4f77dcedbe7206aca22e00af6 Author: Asbelos <asbelos@btinternet.com> Date: Sun Oct 6 13:12:51 2024 +0100 Cleanup to one class commit 309407434928f14f22fef1ff2cdd0f29238f33fb Author: Asbelos <asbelos@btinternet.com> Date: Sun Oct 6 10:34:16 2024 +0100 peeled back commit aa2a6ad1191dbcd6ed75416d457e47b68373277d Author: Asbelos <asbelos@btinternet.com> Date: Sat Oct 5 18:27:43 2024 +0100 all fastpins commit 931baf4b6d4d5b4d55389a64efd5885eeb2218ff Author: Asbelos <asbelos@btinternet.com> Date: Sat Oct 5 16:28:03 2024 +0100 Partial lib extract commit 47bc3b55fc2259cbec9ab830850ea07044ef66be Author: Asbelos <asbelos@btinternet.com> Date: Fri Oct 4 15:41:51 2024 +0100 fixes and SEG7 macro commit 3f26ca2d1a39203428de60cb27f31008847c4233 Author: Asbelos <asbelos@btinternet.com> Date: Fri Oct 4 14:33:23 2024 +0100 enums for exrail easy commit 7e7c00594bf34ebfc331485befc9feaf66cc32ff Author: Asbelos <asbelos@btinternet.com> Date: Fri Oct 4 13:16:57 2024 +0100 Working commit fc4df878482ee3eca6e1232a1ed49ac0c06d2a55 Author: Asbelos <asbelos@btinternet.com> Date: Fri Oct 4 09:27:46 2024 +0100 leds and buttons
2024-10-10 20:38:35 +02:00
/*
* © 2024, Chris Harlow. 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/>.
*/
#ifndef IO_TM1638_h
#define IO_TM1638_h
#include <Arduino.h>
#include "IODevice.h"
#include "DIAG.h"
class TM1638 : public IODevice {
private:
uint8_t _buttons;
uint8_t _leds;
unsigned long _lastLoop;
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
TM1638(VPIN firstVpin, byte clk_pin,byte dio_pin,byte stb_pin);
public:
enum DigitFormat : byte {
// last 4 bits are length.
// DF_1.. DF_8 decimal
DF_1=0x01,DF_2=0x02,DF_3=0x03,DF_4=0x04,
DF_5=0x05,DF_6=0x06,DF_7=0x07,DF_8=0x08,
// DF_1X.. DF_8X HEX
DF_1X=0x11,DF_2X=0x12,DF_3X=0x13,DF_4X=0x14,
DF_5X=0x15,DF_6X=0x16,DF_7X=0x17,DF_8X=0x18,
// DF_1R .. DF_4R raw 7 segmnent data
// only 4 because HAL analogWrite only passes 4 bytes
DF_1R=0x21,DF_2R=0x22,DF_3R=0x23,DF_4R=0x24,
// bits of data conversion type (ored with length)
_DF_DECIMAL=0x00,// right adjusted decimal unsigned leading zeros
_DF_HEX=0x10, // right adjusted hex leading zeros
_DF_RAW=0x20 // bytes are raw 7-segment pattern (max length 4)
};
static void create(VPIN firstVpin, byte clk_pin,byte dio_pin,byte stb_pin);
// Functions overridden in IODevice
void _begin();
void _loop(unsigned long currentMicros) override ;
void _writeAnalogue(VPIN vpin, int value, uint8_t param1, uint16_t param2) override;
void _display() override ;
int _read(VPIN pin) 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