mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-26 09:36:13 +01:00
all fastpins
This commit is contained in:
parent
931baf4b6d
commit
aa2a6ad119
52
TM1638x.cpp
52
TM1638x.cpp
|
@ -1,37 +1,40 @@
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include "TM1638x.h"
|
#include "TM1638x.h"
|
||||||
#include "DIAG.h"
|
#include "DIAG.h"
|
||||||
|
#include "IODevice.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// buttons K3/KS1-8
|
// buttons K3/KS1-8
|
||||||
uint8_t TM1638x::getButtons(){
|
uint8_t TM1638x::getButtons(){
|
||||||
digitalWrite(_stb_pin, LOW);
|
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
||||||
writeData(INSTRUCTION_READ_KEY);
|
writeData(INSTRUCTION_READ_KEY);
|
||||||
//Twait 1µs
|
|
||||||
pinMode(_dio_pin, INPUT);
|
pinMode(_dio_pin, INPUT);
|
||||||
digitalWrite(_clk_pin, LOW);
|
ArduinoPins::fastWriteDigital(_clk_pin, LOW);
|
||||||
uint8_t data[4];
|
_buttons=0;
|
||||||
for (uint8_t i=0; i<sizeof(data);i++){
|
for (uint8_t eachByte=0; eachByte<4;eachByte++) {
|
||||||
data[i] = shiftIn(_dio_pin, _clk_pin, LSBFIRST);
|
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);
|
delayMicroseconds(1);
|
||||||
}
|
}
|
||||||
pinMode(_dio_pin, OUTPUT);
|
pinMode(_dio_pin, OUTPUT);
|
||||||
digitalWrite(_stb_pin, HIGH);
|
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
||||||
_buttons=0;
|
|
||||||
for (uint8_t i=0; i<4;i++){
|
|
||||||
_buttons |= data[i]<<i;
|
|
||||||
}
|
|
||||||
return _buttons;
|
return _buttons;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TM1638x::reset(){
|
void TM1638x::reset(){
|
||||||
setDisplayMode(DISPLAY_TURN_ON | _pulse);
|
setDisplayMode(DISPLAY_TURN_ON | _pulse);
|
||||||
setDataInstruction(INSTRUCTION_WRITE_DATA| INSTRUCTION_ADDRESS_AUTO);
|
setDataInstruction(INSTRUCTION_WRITE_DATA| INSTRUCTION_ADDRESS_AUTO);
|
||||||
digitalWrite(_stb_pin, LOW);
|
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
||||||
writeData(FIRST_DISPLAY_ADDRESS);
|
writeData(FIRST_DISPLAY_ADDRESS);
|
||||||
for(uint8_t i=0;i<16;i++)
|
for(uint8_t i=0;i<16;i++)
|
||||||
writeData(0);
|
writeData(0);
|
||||||
digitalWrite(_stb_pin, HIGH);
|
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TM1638x::displayDig(uint8_t digitId, uint8_t pgfedcba){
|
void TM1638x::displayDig(uint8_t digitId, uint8_t pgfedcba){
|
||||||
|
@ -76,27 +79,32 @@ void TM1638x::displaySetBrightness(pulse_t newpulse){
|
||||||
}
|
}
|
||||||
|
|
||||||
void TM1638x::writeData(uint8_t data){
|
void TM1638x::writeData(uint8_t data){
|
||||||
shiftOut(_dio_pin,_clk_pin,LSBFIRST,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){
|
void TM1638x::writeDataAt(uint8_t displayAddress, uint8_t data){
|
||||||
digitalWrite(_stb_pin, LOW);
|
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
||||||
writeData(displayAddress);
|
writeData(displayAddress);
|
||||||
writeData(data);
|
writeData(data);
|
||||||
digitalWrite(_stb_pin, HIGH);
|
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
||||||
delayMicroseconds(1);
|
delayMicroseconds(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TM1638x::setDisplayMode(uint8_t displayMode){
|
void TM1638x::setDisplayMode(uint8_t displayMode){
|
||||||
digitalWrite(_stb_pin, LOW);
|
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
||||||
writeData(displayMode);
|
writeData(displayMode);
|
||||||
digitalWrite(_stb_pin, HIGH);
|
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
||||||
delayMicroseconds(1);
|
delayMicroseconds(1);
|
||||||
}
|
}
|
||||||
void TM1638x::setDataInstruction(uint8_t dataInstruction){
|
void TM1638x::setDataInstruction(uint8_t dataInstruction){
|
||||||
digitalWrite(_stb_pin, LOW);
|
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
||||||
writeData(dataInstruction);
|
writeData(dataInstruction);
|
||||||
digitalWrite(_stb_pin, HIGH);
|
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
||||||
delayMicroseconds(1);
|
delayMicroseconds(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,11 +115,11 @@ void TM1638x::test(){
|
||||||
//setDisplayMode(DISPLAY_TURN_ON | _pulse);
|
//setDisplayMode(DISPLAY_TURN_ON | _pulse);
|
||||||
displayTurnOn();
|
displayTurnOn();
|
||||||
setDataInstruction(INSTRUCTION_WRITE_DATA| INSTRUCTION_ADDRESS_AUTO);
|
setDataInstruction(INSTRUCTION_WRITE_DATA| INSTRUCTION_ADDRESS_AUTO);
|
||||||
digitalWrite(_stb_pin, LOW);
|
ArduinoPins::fastWriteDigital(_stb_pin, LOW);
|
||||||
writeData(FIRST_DISPLAY_ADDRESS);
|
writeData(FIRST_DISPLAY_ADDRESS);
|
||||||
for(uint8_t i=0;i<16;i++)
|
for(uint8_t i=0;i<16;i++)
|
||||||
writeData(val);
|
writeData(val);
|
||||||
digitalWrite(_stb_pin, HIGH);
|
ArduinoPins::fastWriteDigital(_stb_pin, HIGH);
|
||||||
delay(1000);
|
delay(1000);
|
||||||
val = ~val;
|
val = ~val;
|
||||||
}
|
}
|
||||||
|
|
19
TM1638x.h
19
TM1638x.h
|
@ -38,17 +38,18 @@
|
||||||
|
|
||||||
class TM1638x{
|
class TM1638x{
|
||||||
private:
|
private:
|
||||||
#define INSTRUCTION_WRITE_DATA 0x40
|
static const byte
|
||||||
#define INSTRUCTION_READ_KEY 0x42
|
INSTRUCTION_WRITE_DATA=0x40,
|
||||||
#define INSTRUCTION_ADDRESS_AUTO 0x40
|
INSTRUCTION_READ_KEY=0x42,
|
||||||
#define INSTRUCTION_ADDRESS_FIXED 0x44
|
INSTRUCTION_ADDRESS_AUTO=0x40,
|
||||||
#define INSTRUCTION_NORMAL_MODE 0x40
|
INSTRUCTION_ADDRESS_FIXED=0x44,
|
||||||
#define INSTRUCTION_TEST_MODE 0x48
|
INSTRUCTION_NORMAL_MODE=0x40,
|
||||||
|
INSTRUCTION_TEST_MODE=0x48,
|
||||||
|
|
||||||
#define FIRST_DISPLAY_ADDRESS 0xC0
|
FIRST_DISPLAY_ADDRESS=0xC0,
|
||||||
|
|
||||||
#define DISPLAY_TURN_OFF 0x80
|
DISPLAY_TURN_OFF=0x80,
|
||||||
#define DISPLAY_TURN_ON 0x88
|
DISPLAY_TURN_ON=0x88;
|
||||||
|
|
||||||
|
|
||||||
uint8_t _clk_pin;
|
uint8_t _clk_pin;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user