Skip to content

Commit

Permalink
add web uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
74th committed Feb 4, 2024
1 parent adbc4f1 commit dde341d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
1 change: 1 addition & 0 deletions m5dial-simple-pointer/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
src/ssid.h
5 changes: 4 additions & 1 deletion m5dial-simple-pointer/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ monitor_speed = 115200

lib_deps =
m5stack/M5Dial@^1.0.2
m5stack/M5Unified@^0.1.12
m5stack/M5Unified@^0.1.12

upload_protocol = custom
upload_command = curl -F "image=@.pio/build/m5dial/firmware.bin" m5dial.local/update
16 changes: 15 additions & 1 deletion m5dial-simple-pointer/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <Arduino.h>
#include <M5Dial.h>
#include <Wire.h>
#include <webuploader.h>

#define I2C_SLAVE_ADDRESS 0x0B
#define I2C_BUF_SIZE 5
Expand All @@ -23,6 +24,7 @@ typedef struct
#define CMD_POINTER 0x04

volatile simple_pointer_data_t i2c_buf = {0};
bool webuploader_enabled = false;

unsigned long latest_i2c_connection_time = 0;
uint8_t latest_i2c_command = CMD_POINTER;
Expand Down Expand Up @@ -340,13 +342,25 @@ void setup()
M5Dial.begin(cfg, true, false);
mprintf("");

Wire.begin(I2C_SLAVE_ADDRESS, G13, G15, 400000);
if (M5Dial.BtnA.isPressed())
{
webuploader_enabled = true;
return;
}

Wire
.begin(I2C_SLAVE_ADDRESS, G13, G15, 400000);
Wire.onReceive(i2c_receive_event);
Wire.onRequest(i2c_send_event);
}

void loop()
{
if (webuploader_enabled)
{
webuploader_loop();
return;
}
M5Dial.update();

handle_touch();
Expand Down
2 changes: 2 additions & 0 deletions m5dial-simple-pointer/src/ssid.h.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#define MY_SSID "foo"
#define MY_SSID_PASSWORD "bar"
73 changes: 73 additions & 0 deletions m5dial-simple-pointer/src/webuploader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <Arduino.h>
#include <ssid.h>

#include <WebServer.h>
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <M5Dial.h>
#include <HTTPUpdateServer.h>

void webuploader_loop();

const char *host = "m5dial";
const char *ssid = MY_SSID;
const char *password = MY_SSID_PASSWORD;

bool enabled = false;

WebServer httpServer(80);
HTTPUpdateServer httpUpdater;

void draw_message(const char *message)
{
M5Dial.Display.fillRect(0, 0, M5Dial.Display.width(), M5Dial.Display.height(), BLACK);
M5Dial.Display.setTextDatum(middle_center);
M5Dial.Display.setTextFont(&fonts::Orbitron_Light_32);
M5Dial.Display.setTextSize(0.5);
M5Dial.Display.setTextColor(ORANGE);
M5Dial.Display.drawString(message, M5Dial.Display.width() / 2, M5Dial.Display.height() / 2);
}

void webuploader_setup()
{
auto cfg = M5.config();
M5Dial.begin(cfg, false, false);
draw_message("wifi begin");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED)
{
draw_message("connect fail");
delay(2000);
ESP.restart();
}

if (!MDNS.begin(host))
{
draw_message("Error MDNS !");
while (1)
{
delay(1000);
}
}
draw_message("MDNS start");

httpUpdater.setup(&httpServer);
httpServer.begin();

MDNS.addService("http", "tcp", 80);
draw_message(WiFi.localIP().toString().c_str());
enabled = true;
}

void webuploader_loop()
{
if (!enabled)
{
webuploader_setup();
}
httpServer.handleClient();
delay(1);
}
1 change: 1 addition & 0 deletions m5dial-simple-pointer/src/webuploader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void webuploader_loop();

0 comments on commit dde341d

Please sign in to comment.