Skip to content

Instantly share code, notes, and snippets.

@tanakamasayuki
Last active March 2, 2023 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanakamasayuki/cc330c636fb0eadd962b01e5dc49fe32 to your computer and use it in GitHub Desktop.
Save tanakamasayuki/cc330c636fb0eadd962b01e5dc49fe32 to your computer and use it in GitHub Desktop.
#include <Wire.h>
class I2C_SLG46826 {
public:
I2C_SLG46826(TwoWire &i2cWire = Wire) {
_i2cWire = &i2cWire;
}
bool scanSlg(uint8_t controlCode) {
if (16 <= controlCode) {
return false;
}
for (uint8_t i = 0; i < 4; i++) {
_i2cWire->beginTransmission(controlCode * 8 + i);
byte error = _i2cWire->endTransmission();
if (error != 0) {
return false;
}
}
return true;
}
bool readSlg(uint8_t controlCode, uint8_t data[256], uint8_t nvm = 1) {
if (!scanSlg(controlCode)) {
return false;
}
for (uint8_t i = 0; i < 16; i++) {
readRegEx(controlCode * 8 + 1 + nvm, i * 16, &data[i * 16], 16);
}
return true;
}
bool writeSlg(uint8_t controlCode, const uint8_t data[256], uint8_t mode = 1) {
if (!scanSlg(controlCode)) {
return false;
}
for (uint8_t i = 0; i < 16; i++) {
writeRegEx(controlCode * 8 + 1 + mode, i * 16, &data[i * 16], 16);
}
return true;
}
bool eraseSlg(uint8_t controlCode) {
if (!scanSlg(controlCode)) {
return false;
}
for (uint8_t i = 0; i < 16; i++) {
writeReg(controlCode * 8 + 1, 0xE3, 0x80 + i);
}
return true;
}
bool resetSlg(uint8_t controlCode) {
if (!scanSlg(controlCode)) {
return false;
}
writeReg(controlCode * 8 + 1, 0xC8, 0x02);
return true;
}
bool digitalRead(uint8_t controlCode, uint8_t pin) {
if (!scanSlg(controlCode)) {
return false;
}
if (pin <= 8) {
uint8_t val = readReg(controlCode * 8 + 1, 0x74);
if (pin == 0) {
return val & (1 << 1);
} else if (pin == 1) {
return val & (1 << 2);
} else if (pin == 2) {
return val & (1 << 3);
} else if (pin == 3) {
return val & (1 << 4);
} else if (pin == 4) {
return val & (1 << 5);
} else if (pin == 5) {
return val & (1 << 6);
} else if (pin == 8) {
return val & (1 << 7);
}
} else {
uint8_t val = readReg(controlCode * 8 + 1, 0x75);
if (pin == 9) {
return val & (1 << 0);
} else if (pin == 10) {
return val & (1 << 1);
} else if (pin == 11) {
return val & (1 << 2);
} else if (pin == 12) {
return val & (1 << 3);
} else if (pin == 13) {
return val & (1 << 4);
} else if (pin == 14) {
return val & (1 << 5);
}
}
return false;
}
void writeReg(uint8_t deviceAddress, uint8_t reg, uint8_t data) {
//uint32_t clock = _i2cWire->getClock();
//_i2cWire->setClock(100000);
_i2cWire->beginTransmission(deviceAddress);
_i2cWire->write(reg);
_i2cWire->write(data);
_i2cWire->endTransmission();
//_i2cWire->setClock(clock);
delay(10);
}
void writeRegEx(uint8_t deviceAddress, uint8_t reg, const uint8_t *data, uint8_t size) {
//uint32_t clock = _i2cWire->getClock();
//_i2cWire->setClock(100000);
_i2cWire->beginTransmission(deviceAddress);
_i2cWire->write(reg);
for (int i = 0; i < size; i++) {
_i2cWire->write(data[i]);
}
_i2cWire->endTransmission();
//_i2cWire->setClock(clock);
delay(10);
}
uint8_t readReg(uint8_t deviceAddress, uint8_t reg) {
_i2cWire->beginTransmission(deviceAddress);
_i2cWire->write(reg);
_i2cWire->endTransmission();
_i2cWire->requestFrom(deviceAddress, (uint8_t)1);
return _i2cWire->read();
}
void readRegEx(uint8_t deviceAddress, uint8_t reg, uint8_t *data, uint8_t size) {
_i2cWire->beginTransmission(deviceAddress);
_i2cWire->write(reg);
_i2cWire->endTransmission();
_i2cWire->requestFrom(deviceAddress, size);
for (uint8_t i = 0; i < size; i++) {
data[i] = _i2cWire->read();
}
}
private:
TwoWire *_i2cWire;
};
I2C_SLG46826 slg(Wire);
void setup() {
// Init Serial
Serial.begin(115200);
delay(500);
// Init I2C
Wire.begin();
// print
dispHelp();
scanSLG();
}
void dispHelp(void) {
Serial.println("===============================================================");
Serial.println("SLG46826 Command List");
Serial.println("===============================================================");
Serial.println(" ? : This print");
Serial.println(" SCAN : SCAN SLG46826");
Serial.println(" READ [Control Code:0-15] : READ SLG46826");
Serial.println(" ERASE [Control Code:0-15] : ERASE & RESET SLG46826");
Serial.println(" WRITE [Control Code:0-15] [DATA] : WRITE SLG46826");
Serial.println(" RESET [Control Code:0-15] : RESET SLG46826");
Serial.println(" GPIO [Control Code:0-15] : Display IO");
}
void scanSLG() {
Serial.println("===============================================================");
Serial.println("SLG46826 SCAN");
Serial.println("===============================================================");
for (int i = 0; i < 16; i++) {
if (slg.scanSlg(i)) {
Serial.print(" Control Code = ");
Serial.println(i);
}
}
}
void readSLG(int controlCode) {
Serial.println("===============================================================");
Serial.print("SLG46826 READ [Control Code = ");
Serial.print(controlCode);
Serial.println("]");
Serial.println("===============================================================");
uint8_t data[256];
memset(data, 0, sizeof(data));
if (slg.readSlg(controlCode, data)) {
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
Serial.print(data[i * 16 + j], HEX);
}
Serial.println();
}
} else {
Serial.println("SLG46826 not found.");
}
}
void eraseSLG(int controlCode) {
Serial.println("===============================================================");
Serial.print("SLG46826 ERASE [Control Code = ");
Serial.print(controlCode);
Serial.println("]");
Serial.println("===============================================================");
if (slg.eraseSlg(controlCode)) {
Serial.println("SLG46826 erased.");
slg.resetSlg(controlCode);
Serial.println("Resetting SLG46826.");
} else {
Serial.println("SLG46826 not found.");
}
}
void writeSLG(int controlCode, String hexString) {
Serial.println("===============================================================");
Serial.print("SLG46826 WRITE [Control Code = ");
Serial.print(controlCode);
Serial.println("]");
Serial.println("===============================================================");
if (slg.eraseSlg(controlCode)) {
Serial.println("SLG46826 erased.");
slg.resetSlg(controlCode);
Serial.println("Resetting SLG46826.");
uint8_t hexData[16][16];
memset(hexData, 0, sizeof(hexData));
uint16_t hexStrLen = hexString.length();
uint16_t pos = 0;
for (int i = 0; i < 16; i++) {
// find :
while (pos < hexStrLen && hexString[pos] != ':') {
pos++;
}
// data check
if (hexStrLen < (pos + 43)) {
Serial.println("Error");
while (1)
;
}
// data skip
pos += 9;
// hex to data
for (int j = 0; j < 16; j++) {
uint8_t byteData = 0;
if ('A' <= hexString[pos]) {
byteData += (hexString[pos] - 'A' + 10) * 16;
} else {
byteData += (hexString[pos] - '0') * 16;
}
pos++;
if ('A' <= hexString[pos]) {
byteData += hexString[pos] - 'A' + 10;
} else {
byteData += hexString[pos] - '0';
}
pos++;
hexData[i][j] = byteData;
}
}
// print
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
Serial.print(hexData[i][j], HEX);
Serial.println();
}
Serial.println();
}
slg.writeSlg(0, &hexData[0][0]);
slg.resetSlg(0);
Serial.println("Resetting SLG46826.");
} else {
Serial.println("SLG46826 not found.");
}
}
void resetSLG(int controlCode) {
Serial.println("===============================================================");
Serial.print("SLG46826 RESET [Control Code = ");
Serial.print(controlCode);
Serial.println("]");
Serial.println("===============================================================");
if (slg.resetSlg(controlCode)) {
Serial.println("Resetting SLG46826.");
} else {
Serial.println("SLG46826 not found.");
}
}
void gpioSLG(int controlCode) {
Serial.println("===============================================================");
Serial.print("SLG46826 GPIO [Control Code = ");
Serial.print(controlCode);
Serial.println("]");
Serial.println("===============================================================");
for (int i = 0; i <= 14; i++) {
Serial.print(slg.digitalRead(controlCode, i));
Serial.print(" : IO");
Serial.println(i);
}
}
void loop() {
while (Serial.available()) {
char input[1024];
String command = Serial.readStringUntil('\n');
command.trim();
strncpy(input, command.c_str(), sizeof(input) - 1);
char *command0 = strtok(input, " ");
command = command0;
command0 = strtok(NULL, " ");
String command2 = command0;
String command3 = "";
while (1) {
command0 = strtok(NULL, " ");
if (command0 == NULL) {
break;
}
command3 += command0;
}
if (command == "") {
// Skip
} else if (command == "?") {
dispHelp();
} else if (command == "SCAN") {
scanSLG();
} else if (command == "READ") {
readSLG(command2.toInt());
} else if (command == "ERASE") {
eraseSLG(command2.toInt());
} else if (command == "WRITE") {
writeSLG(command2.toInt(), command3);
} else if (command == "RESET") {
resetSLG(command2.toInt());
} else if (command == "GPIO") {
gpioSLG(command2.toInt());
} else {
dispHelp();
}
Serial.println(">");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment