commit 37e2fb9c20add1d659caa42a732815bd7e304b55 Author: lewisxhe Date: Sat May 11 18:45:12 2019 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e9ee0fb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build +.vscode \ No newline at end of file diff --git a/T22_TBeam.ino b/T22_TBeam.ino new file mode 100644 index 0000000..a1936a0 --- /dev/null +++ b/T22_TBeam.ino @@ -0,0 +1,322 @@ +#include "board_def.h" +#include +#include +#include "axp20x.h" +#include + +#define AXP192_SLAVE_ADDRESS 0x34 +SSD1306_OBJECT(); +UBLOX_GPS_OBJECT(); + +AXP20X_Class axp; + +bool ssd1306_found = false; +bool axp192_found = false; +bool loraBeginOK = false; + +uint64_t dispMap = 0; +String dispInfo; +char buff[512]; +uint64_t gpsSec = 0; + +#define BUTTONS_MAP {38} + +Button2 *pBtns = nullptr; +uint8_t g_btns[] = BUTTONS_MAP; +#define ARRARY_SIZE(a) (sizeof(a) / sizeof(a[0])) + + +/************************************ + * BUTTON + * *********************************/ +void button_callback(Button2 &b) +{ + for (int i = 0; i < ARRARY_SIZE(g_btns); ++i) { + if (pBtns[i] == b) { + ui.nextFrame(); + } + } +} + +void button_loop() +{ + for (int i = 0; i < ARRARY_SIZE(g_btns); ++i) { + pBtns[i].loop(); + } +} + + +void button_init() +{ + uint8_t args = ARRARY_SIZE(g_btns); + pBtns = new Button2 [args]; + for (int i = 0; i < args; ++i) { + pBtns[i] = Button2(g_btns[i]); + pBtns[i].setPressedHandler(button_callback); + } +} + +/************************************ + * SCREEN + * *********************************/ +void msOverlay(OLEDDisplay *display, OLEDDisplayUiState *state) +{ + static char volbuffer[128]; + display->setTextAlignment(TEXT_ALIGN_LEFT); + display->setFont(ArialMT_Plain_10); + + display->drawString(0, 0, axp.isChargeing() ? "Charging" : "No charging"); + + if (axp.isBatteryConnect()) { + snprintf(volbuffer, sizeof(volbuffer), "BAT:%.2fV", axp.getBattVoltage() / 1000.0); + display->drawString(67, 0, volbuffer); + } +} + +void drawFrame1(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) +{ + static uint64_t gpsMap = 0; + + display->setFont(ArialMT_Plain_10); + display->setTextAlignment(TEXT_ALIGN_CENTER); + +#ifdef ENABLE_GPS + while (Serial1.available()) + gps.encode(Serial1.read()); + + if (millis() > 5000 && gps.charsProcessed() < 10) { + display->drawString(64 + x, 11 + y, "T-Beam GPS"); + display->drawString(64 + x, 22 + y, "No GPS detected"); + return; + } + + if (!gps.location.isValid()) { + if (millis() - gpsMap > 1000) { + snprintf(buff, sizeof(buff), "Positioning(%llu)", gpsSec++); + gpsMap = millis(); + } + display->drawString(64 + x, 11 + y, "T-Beam GPS"); + display->drawString(64 + x, 22 + y, buff); + } else { + snprintf(buff, sizeof(buff), "UTC:%d:%d:%d", gps.time.hour(), gps.time.minute(), gps.time.second()); + display->drawString(64 + x, 11 + y, buff); + snprintf(buff, sizeof(buff), "LNG:%.4f", gps.location.lng()); + display->drawString(64 + x, 22 + y, buff); + snprintf(buff, sizeof(buff), "LAT:%.4f", gps.location.lat()); + display->drawString(64 + x, 33 + y, buff); + snprintf(buff, sizeof(buff), "satellites:%lu", gps.satellites.value()); + display->drawString(64 + x, 44 + y, buff); + } +#endif +} + +void drawFrame2(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) +{ + static uint32_t sendCount = 0; + static uint64_t loraMap = 0; + + display->setFont(ArialMT_Plain_10); + display->setTextAlignment(TEXT_ALIGN_CENTER); + display->drawString(64 + x, 11 + y, "T-Beam Lora Sender"); + + if (!loraBeginOK) { + display->drawString(64 + x, 22 + y, "Lora Begin FAIL"); + return; + } + + if (millis() - loraMap > 3000) { + LoRa.beginPacket(); + LoRa.print("lora: "); + LoRa.print(sendCount); + LoRa.endPacket(); + ++sendCount; + Serial.printf("Send %lu\n", sendCount); + loraMap = millis(); + } + display->drawString(64 + x, 22 + y, "Send " + String(sendCount)); +} + +void drawFrame3(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) +{ + static String recv = ""; + + display->setFont(ArialMT_Plain_10); + display->setTextAlignment(TEXT_ALIGN_CENTER); + + if (!loraBeginOK) { + display->drawString(64 + x, 22 + y, "Lora Begin FAIL"); + return; + } + +#ifdef ENABLE_LOAR + if (LoRa.parsePacket()) { + recv = ""; + while (LoRa.available()) { + recv += (char)LoRa.read(); + } + Serial.println(recv); + } + + display->drawString(64 + x, 9 + y, "T-Beam Lora Received"); + display->drawString(64 + x, 22 + y, recv == "" ? "No message" : recv); + display->drawString(64 + x, 35 + y, "rssi :" + String(LoRa.packetRssi())); +#endif +} + +void drawFrame4(OLEDDisplay *display, OLEDDisplayUiState *state, int16_t x, int16_t y) +{ + display->setFont(ArialMT_Plain_10); + display->setTextAlignment(TEXT_ALIGN_CENTER); + if (!axp192_found) { + display->drawString(64 + x, 22 + y, "PMU Begin FAIL"); + return; + } +} + + +FrameCallback frames[] = {drawFrame1, drawFrame2, drawFrame3, drawFrame4}; +OverlayCallback overlays[] = { msOverlay }; + +void ssd1306_init() +{ +#ifdef ENABLE_SSD1306 + // if (!ssd1306_found) { + // Serial.println("SSD1306 not found"); + // return; + // } + if (oled.init()) { + oled.flipScreenVertically(); + oled.setFont(ArialMT_Plain_16); + oled.setTextAlignment(TEXT_ALIGN_CENTER); + // display_to_screen("TTGO T-Beam"); + } else { + Serial.println("SSD1306 Begin FAIL"); + } + Serial.println("SSD1306 Begin PASS"); + ui.setTargetFPS(30); + ui.disableAutoTransition(); + ui.setIndicatorPosition(BOTTOM); + ui.setIndicatorDirection(LEFT_RIGHT); + ui.setFrameAnimation(SLIDE_LEFT); + ui.setFrames(frames, ARRARY_SIZE(frames)); + if (axp192_found) { + ui.setOverlays(overlays, ARRARY_SIZE(overlays)); + } +#endif +} + +void setup() +{ + Serial.begin(115200); + + delay(1000); + + Wire.begin(I2C_SDA, I2C_SCL); + + scanI2Cdevice(); + pinMode(2, OUTPUT); + digitalWrite(2, 0); + if (axp192_found) { + if (!axp.begin(Wire, AXP192_SLAVE_ADDRESS)) { + Serial.println("AXP192 Begin PASS"); + } else { + Serial.println("AXP192 Begin FAIL"); + } + + Serial.printf("isLDO2Enable :%d\n", axp.isLDO2Enable()); + Serial.printf("isLDO3Enable :%d\n", axp.isLDO3Enable()); + + axp.setPowerOutPut(AXP202_LDO2, 1); + axp.setPowerOutPut(AXP202_LDO4, 1); //LDO3 + + Serial.printf("isLDO2Enable :%d\n", axp.isLDO2Enable()); + Serial.printf("isLDO3Enable :%d\n", axp.isLDO3Enable()); + + + attachInterrupt(35, [] { + // axp.readIRQ(); + Serial.println("IRQ "); + digitalWrite(2, !digitalRead(2)); + axp.clearIRQ(); + }, FALLING); + + + } else { + Serial.println("AXP192 not found"); + } + button_init(); + + ssd1306_init(); + +#ifdef ENABLE_GPS + Serial1.begin(GPS_BANUD_RATE, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN); +#endif + +#ifdef ENABLE_LOAR + lora_init(); +#endif +} + + +void lora_init() +{ +#ifdef ENABLE_LOAR + SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_SS); + LoRa.setPins(LORA_SS, LORA_RST, LORA_DI0); + if (!LoRa.begin(BAND)) + Serial.println("LORA Begin FAIL"); + else { + loraBeginOK = true; + Serial.println("LORA Begin PASS"); + } +#endif +} + + +void loop() +{ +#ifdef ENABLE_SSD1306 + if (ui.update()) { +#endif + button_loop(); +#ifdef ENABLE_SSD1306 + } +#endif +} + + +void scanI2Cdevice(void) +{ + byte err, addr; + int nDevices = 0; + for (addr = 1; addr < 127; addr++) { + Wire.beginTransmission(addr); + err = Wire.endTransmission(); + if (err == 0) { + Serial.print("I2C device found at address 0x"); + if (addr < 16) + Serial.print("0"); + Serial.print(addr, HEX); + Serial.println(" !"); + nDevices++; + + if (addr == SSD1306_ADDRESS) { + ssd1306_found = true; + Serial.println("ssd1306 display found"); + } + if (addr == AXP192_SLAVE_ADDRESS) { + axp192_found = true; + Serial.println("axp192 PMU found"); + } + } else if (err == 4) { + Serial.print("Unknow error at address 0x"); + if (addr < 16) + Serial.print("0"); + Serial.println(addr, HEX); + } + } + if (nDevices == 0) + Serial.println("No I2C devices found\n"); + else + Serial.println("done\n"); +} \ No newline at end of file diff --git a/board_def.h b/board_def.h new file mode 100644 index 0000000..1103ea9 --- /dev/null +++ b/board_def.h @@ -0,0 +1,86 @@ +#ifndef BOARD_DEF_H +#define BOARD_DEF_H + +// #define T_BEAM_V07 +#define T_BEAM_V10 + +#define ENABLE_SSD1306 +#define ENABLE_GPS +#define ENABLE_LOAR + + +#ifdef ENABLE_BUTTON +#define BUTTON_1 36 +#define BUTTON_2 0 +#define BUTTON_3 0 +#define BUTTON_4 36 +#define BUTTON_5 0 +#endif + +//ssd1306 oled显示屏 +#define SSD1306_ADDRESS 0x3c +#ifdef ENABLE_SSD1306 +#include "SSD1306.h" +#include "OLEDDisplayUi.h" +#define SSD1306_OBJECT() SSD1306 oled(SSD1306_ADDRESS, I2C_SDA, I2C_SCL);OLEDDisplayUi ui(&oled) + +#else +#define SSD1306_OBJECT() +#endif + +//lora模块 +#ifdef ENABLE_LOAR +#include + +#define LORA_PERIOD 868 + +#define LORA_SCK 5 +#define LORA_MISO 19 +#define LORA_MOSI 27 +#define LORA_SS 18 +#define LORA_DI0 26 +#define LORA_RST 23 + +//设置为1则设定为lora 发送, 0 则为接收 +#define LORA_SENDER 1 +#endif + +//GPS模块 +#ifdef ENABLE_GPS +#include +#define UBLOX_GPS_OBJECT() TinyGPSPlus gps +#define GPS_BANUD_RATE 9600 + +#if defined(T_BEAM_V07) +#define GPS_RX_PIN 12 +#define GPS_TX_PIN 15 +#elif defined(T_BEAM_V10) +#define GPS_RX_PIN 34 +#define GPS_TX_PIN 12 +#endif +#else +UBLOX_GPS_OBJECT() +#endif + + +#ifdef ENABLE_LOAR +#if LORA_PERIOD == 433 +#define BAND 433E6 +#elif LORA_PERIOD == 868 +#define BAND 868E6 +#elif LORA_PERIOD == 915 +#define BAND 915E6 +#else +#define BAND 433E6 +#endif +#endif + + +#define I2C_SDA 21 +#define I2C_SCL 22 +#define PMU_IRQ 35 +#define GPS_POWER_CTRL_CH 3 +#define LORA_POWER_CTRL_CH 2 + + +#endif /*BOARD_DEF_H*/ \ No newline at end of file