some refactoring,

minor code changes
added a small class for SNESController reading
This commit is contained in:
2021-01-03 16:21:20 +01:00
parent ac74587ee7
commit 3babe3f1ef
8 changed files with 188 additions and 22 deletions

122
ext/ctrl/SNESController.h Normal file
View File

@@ -0,0 +1,122 @@
#ifndef SNES_CONTROLLER_H
#define SNES_CONTROLLER_H
#include "../../io/GPIO.h"
/** https://fpgalover.com/images/manuals/SNES/time.JPG */
template <int PIN_LATCH, int PIN_DATA, int PIN_CLK> class SNESController {
public:
union State {
uint16_t raw;
State() : raw(0) {}
State(uint16_t raw) : raw(raw) {}
struct {
uint8_t dummy1 : 1;
uint8_t dummy2 : 1;
uint8_t dummy3 : 1;
uint8_t dummy4 : 1;
uint8_t r : 1;
uint8_t l : 1;
uint8_t x : 1;
uint8_t a : 1;
uint8_t right : 1;
uint8_t left : 1;
uint8_t down : 1;
uint8_t up : 1;
uint8_t start : 1;
uint8_t select : 1;
uint8_t y : 1;
uint8_t b : 1;
};
bool operator != (const State o) const {return raw != o.raw;}
/** write current state into the given 12+1 char* */
void toString(char* dst) {
dst[ 0] = !r ? 'R' : ' ';
dst[ 1] = !l ? 'L' : ' ';
dst[ 2] = !x ? 'X' : ' ';
dst[ 3] = !a ? 'A' : ' ';
dst[ 4] = !right ? 'r' : ' ';
dst[ 5] = !left ? 'l' : ' ';
dst[ 6] = !down ? 'd' : ' ';
dst[ 7] = !up ? 'u' : ' ';
dst[ 8] = !start ? 'S' : ' ';
dst[ 9] = !select ? 's' : ' ';
dst[10] = !y ? 'Y' : ' ';
dst[11] = !b ? 'B' : ' ';
dst[12] = 0;
}
};
public:
/** ctor */
SNESController() {
MyGPIO::setInput(PIN_DATA);
MyGPIO::setOutput(PIN_LATCH);
MyGPIO::setOutput(PIN_CLK);
}
/** read the current state from the controller */
State read() {
// latch the current data (remember state)
latch();
// read all 16 data bits
uint16_t res = 0;
for (uint8_t i = 0; i < 16; ++i) {
res <<= 1;
res |= readBit();
}
// wrap
return State(res);
}
private:
void latch() {
// ensure CLK is HIGH
MyGPIO::set(PIN_CLK);
// send latch impulse
MyGPIO::set(PIN_LATCH);
wait();
MyGPIO::clear(PIN_LATCH);
wait();
}
uint8_t readBit() {
MyGPIO::set(PIN_CLK);
wait();
MyGPIO::clear(PIN_CLK);
uint8_t res = MyGPIO::get(PIN_DATA);
wait();
return res;
}
void wait() {
for (uint8_t i = 0; i < 128; ++i) {asm("nop");}
}
};
#endif

View File

@@ -230,8 +230,8 @@ private:
public:
void flush(const uint16_t* data) {
void startWrite() {
select();
sendCommand(ST7735_CASET); // Column addr set
@@ -249,6 +249,22 @@ public:
sendCommand(ST7735_RAMWR);
modeData();
}
void writePixel(const uint16_t rgb) {
spi.writeByte(rgb >> 0);
spi.writeByte(rgb >> 8);
}
void stopWrite() {
deselect();
}
void flush(const uint16_t* data) {
startWrite();
/*
for (int i = 0; i < (V*H); ++i) {
const uint16_t color = data[i];
@@ -262,7 +278,7 @@ public:
spi.write((const uint8_t*)data, V*H*sizeof(uint16_t));
deselect();
stopWrite();
}

View File

@@ -19,7 +19,7 @@ public:
struct Voltages {
int vShunt; // in uV * 10(!!!)
int16_t vShunt; // in uV * 10(!!!)
int16_t vBus; // in mV
int getMilliAmps(int shunt_milliOhm) const {
@@ -68,24 +68,24 @@ public:
const uint8_t pg = (cfg & 0b01100000000000) >> 11;
const uint8_t brng = (cfg & 0b10000000000000) >> 13;
printf("INA219\n");
printf("- Mode: %d", mode);
printf("- SADC: %d", sadc);
printf("- BADC: %d", badc);
printf("PG (shunt divider) %d", (1<<pg));
if (brng) {printf("- bus voltage range [0:16]");}
else {printf("- bus voltage range [0:32]");}
printf("- Mode: %d\n", mode);
printf("- SADC: %d\n", sadc);
printf("- BADC: %d\n", badc);
printf("PG (shunt divider) %d\n", (1<<pg));
if (brng) {printf("- bus voltage range [0:16]\n");}
else {printf("- bus voltage range [0:32]\n");}
}
private:
/** convert to bytes into a mV reading */
int16_t getBusVoltage(const uint8_t* buf) {
return (getU16(buf) >> 3) * 4; // lower 3 bits are status indicators -> remove, the LSB equals 4 mV -> * 4
return (int16_t)((getU16(buf) >> 3) * 4); // lower 3 bits are status indicators -> remove, the LSB equals 4 mV -> * 4
}
/** convert to bytes into a uV reading */
int getShuntVoltage(const uint8_t* buf) {
return ((int)((int16_t)getU16(buf))); // NOTE: LSB = 10 uV
int16_t getShuntVoltage(const uint8_t* buf) {
return (int16_t)getU16(buf); // NOTE: LSB = 10 uV
}
uint16_t getU16(const uint8_t* buf) {