flipdot/flipcontrol_esp32/src/flipdot.cpp

176 lines
4.6 KiB
C++
Raw Normal View History

2023-02-07 16:51:02 +00:00
#include "flipdot.h"
Flipdot::Flipdot()
{
}
void Flipdot::init()
{
2023-02-11 11:17:47 +00:00
pinMode(PIN_SR_DATA, OUTPUT);
pinMode(PIN_SR_CLK, OUTPUT);
pinMode(PIN_SR_OE, OUTPUT);
2023-02-11 11:17:47 +00:00
pinMode(PIN_SR_LATCH, OUTPUT);
2023-02-07 16:51:02 +00:00
pinMode(PIN_CLEAR, OUTPUT);
pinMode(PIN_DRIVE, OUTPUT);
pinMode(PIN_DATA_DRVBRD, OUTPUT);
pinMode(PIN_CLK_DRVBRD, OUTPUT);
pinMode(PIN_RESET_DRVBRD, OUTPUT);
2023-02-07 16:51:02 +00:00
2023-02-11 11:17:47 +00:00
digitalWrite(PIN_SR_OE, HIGH); //Active Low
2023-02-11 11:17:47 +00:00
digitalWrite(PIN_SR_LATCH, LOW);
2023-02-07 16:51:02 +00:00
digitalWrite(PIN_DRIVE, LOW);
digitalWrite(PIN_RESET_DRVBRD,HIGH); //high = reset. set low to enable data flow to driverboard shift registers
2023-02-07 16:51:02 +00:00
}
void Flipdot::shiftOutSlow(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
{
uint8_t i;
for (i = 0; i < 8; i++) {
if (bitOrder == LSBFIRST)
digitalWrite(dataPin, !!(val & (1 << i)));
else
digitalWrite(dataPin, !!(val & (1 << (7 - i))));
delayMicroseconds(MICROS_SHIFTDELAY);
digitalWrite(clockPin, HIGH);
delayMicroseconds(MICROS_SHIFTDELAY);
digitalWrite(clockPin, LOW);
}
}
void Flipdot::selectColumnClear(uint8_t selcolumn) {
selectColumn(selcolumn, true);
}
void Flipdot::selectColumnSet(uint8_t selcolumn) {
selectColumn(selcolumn, false);
}
void Flipdot::selectColumn(uint8_t selcolumn, bool clear) {
uint8_t sc_bit=3-(selcolumn%4); //each two shift registers control four columns
uint8_t sc_byte=selcolumn/4;
resetColumns();
col[sc_byte]=pow(2, (sc_bit*2+clear)); // possible numbers for clear=false: 1,4,16,64
}
bool Flipdot::clearSelectedColumn() {
2023-02-11 16:18:50 +00:00
shiftDataColumn();
2023-02-07 16:51:02 +00:00
//Clear Columns
2023-02-11 16:18:50 +00:00
2023-02-07 17:37:23 +00:00
for (uint8_t cc=0;cc<COLUMNBYTES;cc++) {
2023-02-07 16:51:02 +00:00
//Serial.print("checking cc="); Serial.println(cc);
for (uint8_t i=0;i<8;i+=2) {
if (CHECK_BIT(col[cc],i)) {
Serial.print("Error on bit ");
Serial.print(i); Serial.print(" col="); Serial.println(cc);
return 0; //a column is set to ground (should not be set for clear column)
}
}
}
digitalWrite(PIN_DRIVE, HIGH);
digitalWrite(PIN_CLEAR, HIGH);
delayMicroseconds(MICROS_DRIVEDOTCLEAR);
digitalWrite(PIN_CLEAR, LOW);
digitalWrite(PIN_DRIVE, LOW);
digitalWrite(PIN_RESET_DRVBRD,HIGH); //reset driverboard shift registers
2023-02-07 16:51:02 +00:00
return 1;
}
bool Flipdot::setSelectedDot() {
2023-02-11 16:18:50 +00:00
shiftDataRow();
shiftDataColumn();
2023-02-07 16:51:02 +00:00
2023-02-07 17:37:23 +00:00
for (uint8_t cc=0;cc<COLUMNBYTES;cc++) {
2023-02-07 16:51:02 +00:00
//Serial.print("checking cc="); Serial.println(cc);
for (uint8_t i=1;i<8;i+=2) {
if (CHECK_BIT(col[cc],i)) {
Serial.print("Error on bit ");
Serial.print(i); Serial.print(" col="); Serial.println(cc);
return 0; //a column is set to ground (should not be set for clear column)
}
}
}
if (!HBridgeOK()) {
return 0;
}
2023-02-11 16:18:50 +00:00
digitalWrite(PIN_SR_OE, LOW); //Active Low. Enable Row
2023-02-07 16:51:02 +00:00
digitalWrite(PIN_DRIVE, HIGH);
delayMicroseconds(MICROS_DRIVEDOTSET); //Drive Dot
2023-02-11 11:17:47 +00:00
digitalWrite(PIN_SR_OE, HIGH); //Active Low
2023-02-07 16:51:02 +00:00
digitalWrite(PIN_DRIVE, LOW);
digitalWrite(PIN_RESET_DRVBRD,HIGH); //reset driverboard shift registers
2023-02-07 16:51:02 +00:00
return 1;
}
bool Flipdot::HBridgeOK() {
2023-02-07 17:37:23 +00:00
for (uint8_t cc=0;cc<COLUMNBYTES;cc++) {
2023-02-07 16:51:02 +00:00
//Serial.print("checking cc="); Serial.println(cc);
for (uint8_t i=0;i<8;i+=2) {
if (CHECK_BIT(col[cc],i) && CHECK_BIT(col[cc],i+1)) {
Serial.print("Short circuit on bit ");
Serial.print(i); Serial.print(" col="); Serial.println(cc);
return 0; //a column is set to ground (should not be set for clear column)
}
}
}
return 1;
}
2023-02-11 16:18:50 +00:00
void Flipdot::shiftDataRow() { //send out all data to shift registers
2023-02-07 16:51:02 +00:00
//select Rows via shift registers on own controller board
2023-02-11 11:17:47 +00:00
shiftOutSlow(PIN_SR_DATA, PIN_SR_CLK, LSBFIRST, row&0xff); //lower byte
shiftOutSlow(PIN_SR_DATA, PIN_SR_CLK, LSBFIRST, row>>8); //LSBFIRST= LSB is QH, bit 8 is QA. //upper byte
digitalWrite(PIN_SR_LATCH, HIGH);
2023-02-07 16:51:02 +00:00
delayMicroseconds(MICROS_SHIFT_LATCH);
2023-02-11 11:17:47 +00:00
digitalWrite(PIN_SR_LATCH, LOW);
2023-02-07 16:51:02 +00:00
2023-02-11 16:18:50 +00:00
}
void Flipdot::shiftDataColumn() { //send out all data to shift registers
digitalWrite(PIN_RESET_DRVBRD,LOW); //get driverboard shift registers out of reset
2023-02-07 16:51:02 +00:00
//Select Columns via Shift registers
2023-02-07 17:37:23 +00:00
for (uint8_t i=0;i<COLUMNBYTES;i++) { //adsf: previously i<7
shiftOutSlow(PIN_DATA_DRVBRD, PIN_CLK_DRVBRD, LSBFIRST, col[COLUMNBYTES-1-i]); //asdf previously col·[6-i]
2023-02-07 16:51:02 +00:00
}
}
void Flipdot::resetColumns() {
2023-02-07 17:37:23 +00:00
for (uint8_t i=0;i<COLUMNBYTES;i++) {
2023-02-07 16:51:02 +00:00
col[i]=0;
}
}
void Flipdot::setRow(uint16_t _row){
row=_row;
}
uint16_t Flipdot::getRow() {
return row;
2023-02-07 16:51:02 +00:00
}