Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // main.cpp
- #include <WiFi.h>
- #include <PubSubClient.h>
- #include "display.h"
- #include "mqtt.h"
- #include "buttons.h"
- #include "EPD_Init.h"
- void setup() {
- Serial.begin(115200);
- EPD_Init(); // Ensure e-paper display initializes properly
- setupDisplay();
- setupWiFi();
- setupMQTT();
- setupButtons();
- displayMenu();
- }
- void loop() {
- handleMQTT();
- handleButtons();
- delay(100);
- }
- // display.h
- #ifndef DISPLAY_H
- #define DISPLAY_H
- #include <GxEPD2_BW.h>
- #include <Fonts/FreeMonoBold9pt7b.h>
- #include "pic_scenario.h" // Include images/icons
- void setupDisplay();
- void displayMenu();
- void displayData(int page);
- void updateSelection(int direction);
- void showSelectedPage();
- #endif
- // display.cpp
- #include "display.h"
- #define CS_PIN 5
- #define DC_PIN 17
- #define RST_PIN 16
- #define BUSY_PIN 4
- GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(CS_PIN, DC_PIN, RST_PIN, BUSY_PIN));
- int selectedOption = 0;
- void setupDisplay() {
- display.init();
- EPD_Init();
- }
- void displayMenu() {
- display.setFullWindow();
- display.firstPage();
- do {
- display.fillScreen(GxEPD_WHITE);
- display.drawBitmap(10, 10, gImage_1, 50, 50, GxEPD_BLACK); // Display an icon
- display.setCursor(70, 30);
- display.print("Select Data:");
- display.setCursor(10, 70);
- display.print(selectedOption == 0 ? "-> Temp & Humidity" : " Temp & Humidity");
- display.setCursor(10, 100);
- display.print(selectedOption == 1 ? "-> Power Usage" : " Power Usage");
- display.setCursor(10, 130);
- display.print(selectedOption == 2 ? "-> Air Quality" : " Air Quality");
- display.setCursor(10, 160);
- display.print(selectedOption == 3 ? "-> Water Level" : " Water Level");
- } while (display.nextPage());
- }
- void updateSelection(int direction) {
- selectedOption = (selectedOption + direction + 4) % 4;
- displayMenu();
- }
- void showSelectedPage() {
- displayData(selectedOption);
- }
- void displayData(int page) {
- display.setFullWindow();
- display.firstPage();
- do {
- display.fillScreen(GxEPD_WHITE);
- display.setCursor(10, 30);
- if (page == 0) display.print("Temperature: " + data_store[0] + " C\nHumidity: " + data_store[1] + "%");
- else if (page == 1) display.print("Power Usage: " + data_store[2] + " W");
- else if (page == 2) display.print("PM2.5: " + data_store[3] + " ug/m3\nPurifier RPM: " + data_store[4]);
- else if (page == 3) display.print("Water Level: " + data_store[5] + "%");
- } while (display.nextPage());
- EPD_Update(); // Ensure display refresh
- }
- // buttons.h
- #ifndef BUTTONS_H
- #define BUTTONS_H
- void setupButtons();
- void handleButtons();
- #endif
- // buttons.cpp
- #include "buttons.h"
- #include "display.h"
- #include <Arduino.h>
- #define MENU_BUTTON 2
- #define UP_BUTTON 4
- #define DOWN_BUTTON 6
- #define SELECT_BUTTON 5
- void setupButtons() {
- pinMode(MENU_BUTTON, INPUT_PULLUP);
- pinMode(UP_BUTTON, INPUT_PULLUP);
- pinMode(DOWN_BUTTON, INPUT_PULLUP);
- pinMode(SELECT_BUTTON, INPUT_PULLUP);
- }
- void handleButtons() {
- if (digitalRead(UP_BUTTON) == LOW) {
- updateSelection(-1);
- delay(200);
- }
- if (digitalRead(DOWN_BUTTON) == LOW) {
- updateSelection(1);
- delay(200);
- }
- if (digitalRead(SELECT_BUTTON) == LOW) {
- showSelectedPage();
- delay(500);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment