Initial commit
This commit is contained in:
251
Ecran Lilygot-T-RGB/Code lib stable/SDmanager/SDmanager.ino
Normal file
251
Ecran Lilygot-T-RGB/Code lib stable/SDmanager/SDmanager.ino
Normal file
@@ -0,0 +1,251 @@
|
||||
|
||||
#include "SD_MMC.h"
|
||||
#include "Wire.h"
|
||||
#include "XL9535_driver.h"
|
||||
#include "pin_config.h"
|
||||
#include "FS.h"
|
||||
|
||||
XL9535 xl;
|
||||
|
||||
|
||||
void SD_init(void);
|
||||
|
||||
void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
|
||||
Serial.printf("Listing directory: %s\n", dirname);
|
||||
|
||||
File root = fs.open(dirname);
|
||||
if (!root) {
|
||||
Serial.println("Failed to open directory");
|
||||
return;
|
||||
}
|
||||
if (!root.isDirectory()) {
|
||||
Serial.println("Not a directory");
|
||||
return;
|
||||
}
|
||||
|
||||
File file = root.openNextFile();
|
||||
while (file) {
|
||||
if (file.isDirectory()) {
|
||||
Serial.print(" DIR : ");
|
||||
Serial.println(file.name());
|
||||
if (levels) {
|
||||
listDir(fs, file.path(), levels - 1);
|
||||
}
|
||||
} else {
|
||||
Serial.print(" FILE: ");
|
||||
Serial.print(file.name());
|
||||
Serial.print(" SIZE: ");
|
||||
Serial.println(file.size());
|
||||
}
|
||||
file = root.openNextFile();
|
||||
}
|
||||
}
|
||||
|
||||
void createDir(fs::FS &fs, const char * path) {
|
||||
Serial.printf("Creating Dir: %s\n", path);
|
||||
if (fs.mkdir(path)) {
|
||||
Serial.println("Dir created");
|
||||
} else {
|
||||
Serial.println("mkdir failed");
|
||||
}
|
||||
}
|
||||
|
||||
void removeDir(fs::FS &fs, const char * path) {
|
||||
Serial.printf("Removing Dir: %s\n", path);
|
||||
if (fs.rmdir(path)) {
|
||||
Serial.println("Dir removed");
|
||||
} else {
|
||||
Serial.println("rmdir failed");
|
||||
}
|
||||
}
|
||||
|
||||
void readFile(fs::FS &fs, const char * path) {
|
||||
Serial.printf("Reading file: %s\n", path);
|
||||
|
||||
File file = fs.open(path);
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for reading");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.print("Read from file: ");
|
||||
while (file.available()) {
|
||||
Serial.write(file.read());
|
||||
}
|
||||
}
|
||||
|
||||
void writeFile(fs::FS &fs, const char * path, const char * message) {
|
||||
Serial.printf("Writing file: %s\n", path);
|
||||
|
||||
File file = fs.open(path, FILE_WRITE);
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
if (file.print(message)) {
|
||||
Serial.println("File written");
|
||||
} else {
|
||||
Serial.println("Write failed");
|
||||
}
|
||||
}
|
||||
|
||||
void appendFile(fs::FS &fs, const char * path, const char * message) {
|
||||
Serial.printf("Appending to file: %s\n", path);
|
||||
|
||||
File file = fs.open(path, FILE_APPEND);
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for appending");
|
||||
return;
|
||||
}
|
||||
if (file.print(message)) {
|
||||
Serial.println("Message appended");
|
||||
} else {
|
||||
Serial.println("Append failed");
|
||||
}
|
||||
}
|
||||
|
||||
void renameFile(fs::FS &fs, const char * path1, const char * path2) {
|
||||
Serial.printf("Renaming file %s to %s\n", path1, path2);
|
||||
if (fs.rename(path1, path2)) {
|
||||
Serial.println("File renamed");
|
||||
} else {
|
||||
Serial.println("Rename failed");
|
||||
}
|
||||
}
|
||||
|
||||
void deleteFile(fs::FS &fs, const char * path) {
|
||||
Serial.printf("Deleting file: %s\n", path);
|
||||
if (fs.remove(path)) {
|
||||
Serial.println("File deleted");
|
||||
} else {
|
||||
Serial.println("Delete failed");
|
||||
}
|
||||
}
|
||||
|
||||
void testFileIO(fs::FS &fs, const char * path) {
|
||||
File file = fs.open(path);
|
||||
static uint8_t buf[512];
|
||||
size_t len = 0;
|
||||
uint32_t start = millis();
|
||||
uint32_t end = start;
|
||||
if (file) {
|
||||
len = file.size();
|
||||
size_t flen = len;
|
||||
start = millis();
|
||||
while (len) {
|
||||
size_t toRead = len;
|
||||
if (toRead > 512) {
|
||||
toRead = 512;
|
||||
}
|
||||
file.read(buf, toRead);
|
||||
len -= toRead;
|
||||
}
|
||||
end = millis() - start;
|
||||
Serial.printf("%u bytes read for %u ms\n", flen, end);
|
||||
file.close();
|
||||
} else {
|
||||
Serial.println("Failed to open file for reading");
|
||||
}
|
||||
|
||||
|
||||
file = fs.open(path, FILE_WRITE);
|
||||
if (!file) {
|
||||
Serial.println("Failed to open file for writing");
|
||||
return;
|
||||
}
|
||||
|
||||
size_t i;
|
||||
start = millis();
|
||||
for (i = 0; i < 2048; i++) {
|
||||
file.write(buf, 512);
|
||||
}
|
||||
end = millis() - start;
|
||||
Serial.printf("%u bytes written for %u ms\n", 2048 * 512, end);
|
||||
file.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
// put your setup code here, to run once:
|
||||
pinMode(BAT_VOLT_PIN, ANALOG);
|
||||
|
||||
Wire.begin(IIC_SDA_PIN, IIC_SCL_PIN);
|
||||
|
||||
xl.begin();
|
||||
|
||||
uint8_t pin = (1 << PWR_EN_PIN) | (1 << LCD_CS_PIN) | (1 << TP_RES_PIN) | (1 << LCD_SDA_PIN) | (1 << LCD_CLK_PIN) |
|
||||
(1 << LCD_RST_PIN) | (1 << SD_CS_PIN);
|
||||
|
||||
xl.pinMode8(0, pin, OUTPUT);
|
||||
xl.digitalWrite(PWR_EN_PIN, HIGH);
|
||||
|
||||
|
||||
Serial.println(1);
|
||||
delay(1000);
|
||||
Serial.println(1);
|
||||
delay(1000);
|
||||
Serial.println(1);
|
||||
delay(1000);
|
||||
SD_init();
|
||||
|
||||
delay(100);
|
||||
xl.digitalWrite(TP_RES_PIN, LOW);
|
||||
delay(300);
|
||||
xl.digitalWrite(TP_RES_PIN, HIGH);
|
||||
delay(300);
|
||||
pinMode(TP_INT_PIN, INPUT);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void SD_init(void)
|
||||
{
|
||||
xl.digitalWrite(SD_CS_PIN, 1); // To use SDIO one-line mode, you need to pull the CS pin high
|
||||
SD_MMC.setPins(SD_CLK_PIN, SD_CMD_PIN, SD_D0_PIN);
|
||||
if (!SD_MMC.begin("/sdcard", true, true)) {
|
||||
Serial.println("Card Mount Failed");
|
||||
return;
|
||||
}
|
||||
uint8_t cardType = SD_MMC.cardType();
|
||||
if (cardType == CARD_NONE) {
|
||||
Serial.println("No SD card attached");
|
||||
return;
|
||||
}
|
||||
Serial.print("SD Card Type: ");
|
||||
if (cardType == CARD_MMC)
|
||||
Serial.println("MMC");
|
||||
else if (cardType == CARD_SD)
|
||||
Serial.println("SDSC");
|
||||
else if (cardType == CARD_SDHC)
|
||||
Serial.println("SDHC");
|
||||
else
|
||||
Serial.println("UNKNOWN");
|
||||
|
||||
uint64_t cardSize = SD_MMC.cardSize() / (1024 * 1024);
|
||||
Serial.printf("SD Card Size: %lluMB\n", cardSize);
|
||||
listDir(SD_MMC, "/", 0);
|
||||
createDir(SD_MMC, "/mydir");
|
||||
listDir(SD_MMC, "/", 0);
|
||||
removeDir(SD_MMC, "/mydir");
|
||||
listDir(SD_MMC, "/", 2);
|
||||
writeFile(SD_MMC, "/hello.txt", "Hello ");
|
||||
appendFile(SD_MMC, "/hello.txt", "World!\n");
|
||||
readFile(SD_MMC, "/hello.txt");
|
||||
deleteFile(SD_MMC, "/foo.txt");
|
||||
renameFile(SD_MMC, "/hello.txt", "/foo.txt");
|
||||
readFile(SD_MMC, "/foo.txt");
|
||||
testFileIO(SD_MMC, "/test.txt");
|
||||
Serial.printf("Total space: %lluMB\n", SD_MMC.totalBytes() / (1024 * 1024));
|
||||
Serial.printf("Used space: %lluMB\n", SD_MMC.usedBytes() / (1024 * 1024));
|
||||
}
|
||||
117
Ecran Lilygot-T-RGB/Code lib stable/SDmanager/XL9535_driver.cpp
Normal file
117
Ecran Lilygot-T-RGB/Code lib stable/SDmanager/XL9535_driver.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
#include "XL9535_driver.h"
|
||||
#include "Wire.h"
|
||||
|
||||
void XL9535::writeRegister(uint8_t reg, uint8_t *data, uint8_t len) {
|
||||
_wire->beginTransmission(_address);
|
||||
_wire->write(reg);
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
_wire->write(data[i]);
|
||||
}
|
||||
_wire->endTransmission();
|
||||
}
|
||||
uint8_t XL9535::readRegister(uint8_t reg, uint8_t *data, uint8_t len) {
|
||||
_wire->beginTransmission(_address);
|
||||
_wire->write(reg);
|
||||
_wire->endTransmission();
|
||||
_wire->requestFrom(_address, len);
|
||||
uint8_t index = 0;
|
||||
while (index < len)
|
||||
data[index++] = _wire->read();
|
||||
return 0;
|
||||
}
|
||||
|
||||
void XL9535::begin(bool A0, bool A1, bool A2, TwoWire *wire) {
|
||||
_address = XL9535_IIC_ADDRESS | (A2 << 3) | (A1 << 2) | (A0 << 1);
|
||||
_wire = wire;
|
||||
is_found = true;
|
||||
_wire->beginTransmission(_address);
|
||||
if (!_wire->endTransmission()) {
|
||||
Serial.println("Found xl9535");
|
||||
} else {
|
||||
Serial.println("xl9535 not found");
|
||||
is_found = false;
|
||||
}
|
||||
}
|
||||
void XL9535::pinMode(uint8_t pin, uint8_t mode) {
|
||||
if (is_found) {
|
||||
uint8_t port = 0;
|
||||
if (pin > 7) {
|
||||
readRegister(XL9535_CONFIG_PORT_1_REG, &port, 1);
|
||||
if (mode == OUTPUT) {
|
||||
port = port & (~(1 << (pin - 10)));
|
||||
} else {
|
||||
port = port | (1 << (pin - 10));
|
||||
}
|
||||
writeRegister(XL9535_CONFIG_PORT_1_REG, &port, 1);
|
||||
|
||||
} else {
|
||||
readRegister(XL9535_CONFIG_PORT_0_REG, &port, 1);
|
||||
if (mode == OUTPUT) {
|
||||
port = port & (~(1 << pin));
|
||||
} else {
|
||||
port = port | (1 << pin);
|
||||
}
|
||||
writeRegister(XL9535_CONFIG_PORT_0_REG, &port, 1);
|
||||
}
|
||||
} else {
|
||||
Serial.println("xl9535 not found");
|
||||
}
|
||||
}
|
||||
void XL9535::pinMode8(uint8_t port, uint8_t pin, uint8_t mode) {
|
||||
if (is_found) {
|
||||
uint8_t _pin = (mode != OUTPUT) ? pin : ~pin;
|
||||
if (port) {
|
||||
writeRegister(XL9535_CONFIG_PORT_1_REG, &_pin, 1);
|
||||
} else {
|
||||
writeRegister(XL9535_CONFIG_PORT_0_REG, &_pin, 1);
|
||||
}
|
||||
} else {
|
||||
Serial.println("xl9535 not found");
|
||||
}
|
||||
}
|
||||
|
||||
void XL9535::digitalWrite(uint8_t pin, uint8_t val) {
|
||||
if (is_found) {
|
||||
uint8_t port = 0;
|
||||
uint8_t reg_data = 0;
|
||||
if (pin > 7) {
|
||||
readRegister(XL9535_OUTPUT_PORT_1_REG, ®_data, 1);
|
||||
reg_data = reg_data & (~(1 << (pin - 10)));
|
||||
port = reg_data | val << (pin - 10);
|
||||
writeRegister(XL9535_OUTPUT_PORT_1_REG, &port, 1);
|
||||
} else {
|
||||
readRegister(XL9535_OUTPUT_PORT_0_REG, ®_data, 1);
|
||||
reg_data = reg_data & (~(1 << pin));
|
||||
port = reg_data | val << pin;
|
||||
writeRegister(XL9535_OUTPUT_PORT_0_REG, &port, 1);
|
||||
}
|
||||
} else {
|
||||
Serial.println("xl9535 not found");
|
||||
}
|
||||
}
|
||||
|
||||
int XL9535::digitalRead(uint8_t pin) {
|
||||
if (is_found) {
|
||||
int state = 0;
|
||||
uint8_t port = 0;
|
||||
if (pin > 7) {
|
||||
readRegister(XL9535_INPUT_PORT_1_REG, &port, 1);
|
||||
state = port & (pin - 10) ? 1 : 0;
|
||||
} else {
|
||||
readRegister(XL9535_INPUT_PORT_0_REG, &port, 1);
|
||||
state = port & pin ? 1 : 0;
|
||||
}
|
||||
return state;
|
||||
} else {
|
||||
Serial.println("xl9535 not found");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void XL9535::read_all_reg() {
|
||||
uint8_t data;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
readRegister(i, &data, 1);
|
||||
Serial.printf("0x%02x : 0x%02X \r\n", i, data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Wire.h"
|
||||
|
||||
#define XL9535_IIC_ADDRESS 0X20
|
||||
|
||||
#define XL9535_INPUT_PORT_0_REG 0X00
|
||||
#define XL9535_INPUT_PORT_1_REG 0X01
|
||||
#define XL9535_OUTPUT_PORT_0_REG 0X02
|
||||
#define XL9535_OUTPUT_PORT_1_REG 0X03
|
||||
#define XL9535_INVERSION_PORT_0_REG 0X04
|
||||
#define XL9535_INVERSION_PORT_1_REG 0X05
|
||||
#define XL9535_CONFIG_PORT_0_REG 0X06
|
||||
#define XL9535_CONFIG_PORT_1_REG 0X07
|
||||
|
||||
class XL9535 {
|
||||
public:
|
||||
XL9535(){};
|
||||
~XL9535(){};
|
||||
|
||||
void begin(bool A0 = 0, bool A1 = 0, bool A2 = 0, TwoWire *wire = &Wire);
|
||||
void pinMode(uint8_t pin, uint8_t mode);
|
||||
void pinMode8(uint8_t port, uint8_t pin, uint8_t mode);
|
||||
|
||||
void digitalWrite(uint8_t pin, uint8_t val);
|
||||
int digitalRead(uint8_t pin);
|
||||
void read_all_reg();
|
||||
|
||||
protected:
|
||||
void writeRegister(uint8_t reg, uint8_t *data, uint8_t len);
|
||||
uint8_t readRegister(uint8_t reg, uint8_t *data, uint8_t len);
|
||||
|
||||
uint8_t _address;
|
||||
TwoWire *_wire;
|
||||
bool is_found;
|
||||
};
|
||||
57
Ecran Lilygot-T-RGB/Code lib stable/SDmanager/pin_config.h
Normal file
57
Ecran Lilygot-T-RGB/Code lib stable/SDmanager/pin_config.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#define WIFI_SSID "xinyuandianzi"
|
||||
#define WIFI_PASSWORD "AA15994823428"
|
||||
|
||||
#define EXAMPLE_LCD_PIXEL_CLOCK_HZ (8 * 1000 * 1000)
|
||||
#define EXAMPLE_LCD_BK_LIGHT_ON_LEVEL 1
|
||||
#define EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL !EXAMPLE_LCD_BK_LIGHT_ON_LEVEL
|
||||
#define EXAMPLE_PIN_NUM_BK_LIGHT 46
|
||||
#define EXAMPLE_PIN_NUM_HSYNC 47
|
||||
#define EXAMPLE_PIN_NUM_VSYNC 41
|
||||
#define EXAMPLE_PIN_NUM_DE 45
|
||||
#define EXAMPLE_PIN_NUM_PCLK 42
|
||||
// #define EXAMPLE_PIN_NUM_DATA0 44
|
||||
#define EXAMPLE_PIN_NUM_DATA1 21
|
||||
#define EXAMPLE_PIN_NUM_DATA2 18
|
||||
#define EXAMPLE_PIN_NUM_DATA3 17
|
||||
#define EXAMPLE_PIN_NUM_DATA4 16
|
||||
#define EXAMPLE_PIN_NUM_DATA5 15
|
||||
#define EXAMPLE_PIN_NUM_DATA6 14
|
||||
#define EXAMPLE_PIN_NUM_DATA7 13
|
||||
#define EXAMPLE_PIN_NUM_DATA8 12
|
||||
#define EXAMPLE_PIN_NUM_DATA9 11
|
||||
#define EXAMPLE_PIN_NUM_DATA10 10
|
||||
#define EXAMPLE_PIN_NUM_DATA11 9
|
||||
// #define EXAMPLE_PIN_NUM_DATA12 43
|
||||
#define EXAMPLE_PIN_NUM_DATA13 7
|
||||
#define EXAMPLE_PIN_NUM_DATA14 6
|
||||
#define EXAMPLE_PIN_NUM_DATA15 5
|
||||
#define EXAMPLE_PIN_NUM_DATA16 3
|
||||
#define EXAMPLE_PIN_NUM_DATA17 2
|
||||
#define EXAMPLE_PIN_NUM_DISP_EN -1
|
||||
|
||||
// The pixel number in horizontal and vertical
|
||||
#define EXAMPLE_LCD_H_RES 480
|
||||
#define EXAMPLE_LCD_V_RES 480
|
||||
|
||||
#define IIC_SCL_PIN 48
|
||||
#define IIC_SDA_PIN 8
|
||||
|
||||
#define SD_CLK_PIN 39
|
||||
#define SD_CMD_PIN 40
|
||||
#define SD_D0_PIN 38
|
||||
|
||||
#define BAT_VOLT_PIN 4
|
||||
#define TP_INT_PIN 1
|
||||
|
||||
#define BOOT_BTN_PIN 0
|
||||
|
||||
/* XL9535 --- PIN - P0*/
|
||||
#define TP_RES_PIN 1
|
||||
#define PWR_EN_PIN 2
|
||||
#define LCD_CS_PIN 3
|
||||
#define LCD_SDA_PIN 4
|
||||
#define LCD_CLK_PIN 5
|
||||
#define LCD_RST_PIN 6
|
||||
#define SD_CS_PIN 7
|
||||
Reference in New Issue
Block a user