Skip to content

Commit

Permalink
Merge pull request #36 from ilovehotcakes/frontend
Browse files Browse the repository at this point in the history
iOS-esque frontend
  • Loading branch information
ilovehotcakes authored Jun 10, 2024
2 parents fe52c57 + fe91b7e commit fde068e
Show file tree
Hide file tree
Showing 26 changed files with 3,889 additions and 556 deletions.
22 changes: 12 additions & 10 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,20 @@ build_flags =
-D BUTTON_PIN=0 ; User button

; Define ESP32 connections to stepper motor driver (Trinamic TMC2209) and other settings
-D STEP_PIN=GPIO_NUM_16 ; Step pin
-D DIR_PIN=GPIO_NUM_18 ; Direction pin
-D DIAG_PIN=GPIO_NUM_23 ; For StallGuard, High if detect error
-D STBY_PIN=GPIO_NUM_19 ; Pull high to disable TMC2209
-D TXD1=GPIO_NUM_22 ; For Serial1
-D RXD1=GPIO_NUM_21 ; For Serial1
-D R_SENSE=0.12f ; Sense resistor, double check the board's sense resistor value
-D DRIVER_ADDR=0b00 ; 0b00 is slave, since there're no other TMC stepper motor drivers
-D STEP_PIN=GPIO_NUM_16 ; Step pin
-D DIR_PIN=GPIO_NUM_18 ; Direction pin
-D DIAG_PIN=GPIO_NUM_23 ; For StallGuard, High if detect error
-D STBY_PIN=GPIO_NUM_19 ; Pull high to disable TMC2209
-D TXD1=GPIO_NUM_22 ; For Serial1
-D RXD1=GPIO_NUM_21 ; For Serial1
-D R_SENSE=0.12f ; Sense resistor, double check the board's sense resistor value
-D DRIVER_ADDR=0b00 ; 0b00 is slave, since there're no other TMC stepper motor drivers

; Define ESP32 connection to rotary encoder (AS5600)
-D SCL_PIN=GPIO_NUM_27 ; SPI clock pin
-D SDA_PIN=GPIO_NUM_14 ; SPI data pin
-D SCL_PIN=GPIO_NUM_27 ; SPI clock pin
-D SDA_PIN=GPIO_NUM_14 ; SPI data pin

!python ./src/scripts/git_revision.py

extra_scripts =
pre:./src/frontend/assembly.py
Expand Down
106 changes: 93 additions & 13 deletions src/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ Command hash(String command) {
else if (command == "backward") return MOTOR_BACKWARD;
else if (command == "set-min") return MOTOR_SET_MIN;
else if (command == "set-max") return MOTOR_SET_MAX;
else if (command == "standby") return MOTOR_STDBY;
else if (command == "open-close") return MOTOR_OPEN_CLOSE;
else if (command == "speed") return MOTOR_SPEED;
else if (command == "opening-speed") return MOTOR_OP_SPEED;
else if (command == "closing-speed") return MOTOR_CL_SPEED;
else if (command == "zero") return MOTOR_ZERO;
else if (command == "standby") return MOTOR_STANDBY;
else if (command == "sync-settings") return MOTOR_SYNC_STTNG;
else if (command == "velocity") return MOTOR_VLCTY;
else if (command == "opening-velocity") return MOTOR_OP_VLCTY;
else if (command == "closing-velocity") return MOTOR_CL_VLCTY;
else if (command == "acceleration") return MOTOR_ACCEL;
else if (command == "opening-acceleration") return MOTOR_OP_ACCEL;
else if (command == "closing-acceleration") return MOTOR_CL_ACCEL;
else if (command == "current") return MOTOR_CURRENT;
else if (command == "opening-current") return MOTOR_OP_CURRENT;
else if (command == "closing-current") return MOTOR_CL_CURRENT;
else if (command == "direction") return MOTOR_DIRECTION;
else if (command == "microsteps") return MOTOR_MICROSTEPS;
else if (command == "full-steps") return MOTOR_FULL_STEPS;
else if (command == "microsteps") return MOTOR_MICROSTEPS;
else if (command == "stallguard") return MOTOR_STALLGUARD;
else if (command == "coolstep-threshold") return MOTOR_TCOOLTHRS;
else if (command == "stallguard-threshold") return MOTOR_SGTHRS;
Expand All @@ -35,6 +36,7 @@ Command hash(String command) {
else if (command == "sleep") return SYSTEM_SLEEP;
else if (command == "restart") return SYSTEM_RESTART;
else if (command == "reset") return SYSTEM_RESET;
else if (command == "name") return SYSTEM_RENAME;

else if (command == "setup") return WIRELESS_SETUP;
else if (command == "ssid") return WIRELESS_SSID;
Expand All @@ -55,20 +57,21 @@ String hash(Command command) {
else if (command == MOTOR_BACKWARD) return "backward";
else if (command == MOTOR_SET_MIN) return "set-min";
else if (command == MOTOR_SET_MAX) return "set-max";
else if (command == MOTOR_STDBY) return "standby";
else if (command == MOTOR_OPEN_CLOSE) return "open-close";
else if (command == MOTOR_SPEED) return "speed";
else if (command == MOTOR_OP_SPEED) return "opening-speed";
else if (command == MOTOR_CL_SPEED) return "closing-speed";
else if (command == MOTOR_ZERO) return "zero";
else if (command == MOTOR_STANDBY) return "standby";
else if (command == MOTOR_SYNC_STTNG) return "sync-settings";
else if (command == MOTOR_VLCTY) return "velocity";
else if (command == MOTOR_OP_VLCTY) return "opening-velocity";
else if (command == MOTOR_CL_VLCTY) return "closing-velocity";
else if (command == MOTOR_ACCEL) return "acceleration";
else if (command == MOTOR_OP_ACCEL) return "opening-acceleration";
else if (command == MOTOR_CL_ACCEL) return "closing-acceleration";
else if (command == MOTOR_CURRENT) return "current";
else if (command == MOTOR_OP_CURRENT) return "opening-current";
else if (command == MOTOR_CL_CURRENT) return "closing-current";
else if (command == MOTOR_DIRECTION) return "direction";
else if (command == MOTOR_MICROSTEPS) return "microsteps";
else if (command == MOTOR_FULL_STEPS) return "full-steps";
else if (command == MOTOR_MICROSTEPS) return "microsteps";
else if (command == MOTOR_STALLGUARD) return "stallguard";
else if (command == MOTOR_TCOOLTHRS) return "coolstep-threshold";
else if (command == MOTOR_SGTHRS) return "stallguard-threshold";
Expand All @@ -78,6 +81,7 @@ String hash(Command command) {
else if (command == SYSTEM_SLEEP) return "sleep";
else if (command == SYSTEM_RESTART) return "restart";
else if (command == SYSTEM_RESET) return "reset";
else if (command == SYSTEM_RENAME) return "name";

else if (command == WIRELESS_SETUP) return "setup";
else if (command == WIRELESS_SSID) return "ssid";
Expand All @@ -87,6 +91,82 @@ String hash(Command command) {
}


std::pair<std::function<bool(int)>, String> getCommandEvalFunc(Command command) {
if (command == MOTOR_STOP) {
return std::make_pair([=](int val) -> bool { return false; }, "");
} else if (command == MOTOR_PERECENT) {
return std::make_pair([=](int val) -> bool { return val > 100 || val < 0; }, "=0~100 (%); 0 to open; 100 to close");
} else if (command == MOTOR_STEP) {
return std::make_pair([=](int val) -> bool { return val < 0; }, ">=0");
} else if (command == MOTOR_FORWARD) {
return std::make_pair([=](int val) -> bool { return false; }, "");
} else if (command == MOTOR_BACKWARD) {
return std::make_pair([=](int val) -> bool { return false; }, "");
} else if (command == MOTOR_SET_MIN) {
return std::make_pair([=](int val) -> bool { return false; }, "");
} else if (command == MOTOR_SET_MAX) {
return std::make_pair([=](int val) -> bool { return false; }, "");
} else if (command == MOTOR_ZERO) {
return std::make_pair([=](int val) -> bool { return false; }, "");
} else if (command == MOTOR_STANDBY) {
return std::make_pair([=](int val) -> bool { return val != 0 && val != 1; }, "=0 | 1; 1 to standby motor driver; 0 to start");
} else if (command == MOTOR_SYNC_STTNG) {
return std::make_pair([=](int val) -> bool { return val != 0 && val != 1; }, "=0 | 1; 0 to keep opening/closing settings the same");
} else if (command == MOTOR_CURRENT) {
return std::make_pair([=](int val) -> bool { return val < 1 || val > 2000; }, "=1~2000 (mA); please refer to motor datasheet for max RMS");
} else if (command == MOTOR_OP_CURRENT) {
return std::make_pair([=](int val) -> bool { return val < 1 || val > 2000; }, "=1~2000 (mA); please refer to motor datasheet for max RMS");
} else if (command == MOTOR_CL_CURRENT) {
return std::make_pair([=](int val) -> bool { return val < 1 || val > 2000; }, "=1~2000 (mA); please refer to motor datasheet for max RMS");
} else if (command == MOTOR_DIRECTION) {
return std::make_pair([=](int val) -> bool { return val != 0 && val != 1; }, "=0 | 1");
} else if (command == MOTOR_FULL_STEPS) {
return std::make_pair([=](int val) -> bool { return val <= 0; }, ">0");
} else if (command == MOTOR_MICROSTEPS) {
return std::make_pair([=](int val) -> bool { return val != 0 && val != 2 && val != 4 && val != 8 && val != 16 && val != 32 && val != 64
&& val != 128 && val != 256; }, "=0 | 2 | 4 | 8 | 16 | 32 | 64 | 128 | 256");
} else if (command == MOTOR_STALLGUARD) {
return std::make_pair([=](int val) -> bool { return val != 0 && val != 1; }, "=0 | 1; 0 to disable; 1 to enable");
} else if (command == MOTOR_TCOOLTHRS) {
return std::make_pair([=](int val) -> bool { return val < 0 || val > 1048575; }, "=0~1048575; lower threshold velocity for switching on stallguard");
} else if (command == MOTOR_SGTHRS) {
return std::make_pair([=](int val) -> bool { return val < 0 || val > 255; }, "=0~255; the greater, the easier to stall");
} else if (command == MOTOR_SPREADCYCL) {
return std::make_pair([=](int val) -> bool { return val != 0 && val != 1; }, "=0 | 1; 0 to disable; 1 to enable");
} else if (command == MOTOR_TPWMTHRS) {
return std::make_pair([=](int val) -> bool { return val < 0 || val > 1048575; }, "=0~1048575; upper threshold to switch to fastmode");
}

else if (command == SYSTEM_SLEEP) {
return std::make_pair([=](int val) -> bool { return false; }, "");
} else if (command == SYSTEM_RESTART) {
return std::make_pair([=](int val) -> bool { return false; }, "");
} else if (command == SYSTEM_RESET) {
return std::make_pair([=](int val) -> bool { return false; }, "");
}

// else if (command == WIRELESS_SETUP) {
return std::make_pair([=](int val) -> bool { return val != 0 && val != 1; }, "=0 | 1; 1 to enter setup mode");
}


std::pair<std::function<bool(float)>, String> getCommandEvalFuncf(Command command) {
if (command == MOTOR_VLCTY) {
return std::make_pair([=](float val) -> bool { return val <= 0.0; }, ">0.0 (Hz)"); // float
} else if (command == MOTOR_OP_VLCTY) {
return std::make_pair([=](float val) -> bool { return val <= 0.0; }, ">0.0 (Hz)"); // float
} else if (command == MOTOR_CL_VLCTY) {
return std::make_pair([=](float val) -> bool { return val <= 0.0; }, ">0.0 (Hz)"); // float
} else if (command == MOTOR_ACCEL) {
return std::make_pair([=](float val) -> bool { return val <= 0.0; }, ">0.0" ); // float
} else if (command == MOTOR_OP_ACCEL) {
return std::make_pair([=](float val) -> bool { return val <= 0.0; }, ">0.0" ); // float
}
// else if (command == MOTOR_CL_ACCEL) {
return std::make_pair([=](float val) -> bool { return val <= 0.0; }, ">0.0" ); // float
}


String listMotorCommands() {
String list = "";
for (int command = MOTOR_STOP; command <= MOTOR_TPWMTHRS; command++) {
Expand All @@ -98,7 +178,7 @@ String listMotorCommands() {

String listSystemCommands() {
String list = "";
for (int command = SYSTEM_SLEEP; command >= SYSTEM_RESET; command--) {
for (int command = SYSTEM_SLEEP; command >= SYSTEM_RENAME; command--) {
list = list + hash(Command(command)) + " | ";
}
return list.substring(0, list.length() - 3);
Expand Down
41 changes: 23 additions & 18 deletions src/command.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <Arduino.h>
#include <FunctionalInterrupt.h>


enum Command {
Expand All @@ -14,30 +15,32 @@ enum Command {
MOTOR_BACKWARD = 5,
MOTOR_SET_MIN = 6,
MOTOR_SET_MAX = 7,
MOTOR_STDBY = 8,
MOTOR_OPEN_CLOSE = 9,
MOTOR_SPEED = 10,
MOTOR_OP_SPEED = 11,
MOTOR_CL_SPEED = 12,
MOTOR_ACCEL = 13,
MOTOR_OP_ACCEL = 14,
MOTOR_CL_ACCEL = 15,
MOTOR_CURRENT = 16,
MOTOR_OP_CURRENT = 17,
MOTOR_CL_CURRENT = 18,
MOTOR_DIRECTION = 19,
MOTOR_MICROSTEPS = 20,
MOTOR_ZERO = 8,
MOTOR_STANDBY = 9,
MOTOR_SYNC_STTNG = 10,
MOTOR_VLCTY = 11,
MOTOR_OP_VLCTY = 12,
MOTOR_CL_VLCTY = 13,
MOTOR_ACCEL = 14,
MOTOR_OP_ACCEL = 15,
MOTOR_CL_ACCEL = 16,
MOTOR_CURRENT = 17,
MOTOR_OP_CURRENT = 18,
MOTOR_CL_CURRENT = 19,
MOTOR_DIRECTION = 20,
MOTOR_FULL_STEPS = 21,
MOTOR_STALLGUARD = 22,
MOTOR_TCOOLTHRS = 23,
MOTOR_SGTHRS = 24,
MOTOR_SPREADCYCL = 25,
MOTOR_TPWMTHRS = 26,
MOTOR_MICROSTEPS = 22,
MOTOR_STALLGUARD = 23,
MOTOR_TCOOLTHRS = 24,
MOTOR_SGTHRS = 25,
MOTOR_SPREADCYCL = 26,
MOTOR_TPWMTHRS = 27,

// System commands < 0
SYSTEM_SLEEP = -1,
SYSTEM_RESTART = -2,
SYSTEM_RESET = -3,
SYSTEM_RENAME = -4,

WIRELESS_SETUP = -51,
WIRELESS_SSID = -52,
Expand All @@ -47,6 +50,8 @@ enum Command {

Command hash (String command);
String hash (Command command);
std::pair<std::function<bool(int)>, String> getCommandEvalFunc(Command command);
std::pair<std::function<bool(float)>, String> getCommandEvalFuncf(Command command);
String listMotorCommands();
String listSystemCommands();
String listWirelessCommands();
22 changes: 15 additions & 7 deletions src/frontend/assembly.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import base64

def base_64(file_path):
with open(file_path, 'rb') as img_file:
b64_string = base64.b64encode(img_file.read()).decode("utf-8")
return "data:image/png;base64," + b64_string


def process_html():
current_directory = "./src/frontend"
assembled_html = "const char index_html[] = R\"rawliteral("
Expand All @@ -11,8 +17,10 @@ def process_html():
with open(file_path, 'r') as css_file:
for line in css_file:
if "%" in line:
i = line.index("%")
t = line[:i] + "%" + line[i:]
line = line.replace("%", "%%")
elif "background-image: url" in line and "https://" not in line:
file_path = current_directory + line[27:-3]
line = line[:26] + f"'{base_64(file_path)}');"
assembled_html += line
assembled_html += "\t</style>\n"
elif "index.js" in html_line:
Expand All @@ -23,12 +31,12 @@ def process_html():
assembled_html += line
assembled_html += "\t</script>\n"
elif ".png" in html_line:
file_path = current_directory + html_line[html_line.index("href=")+7:-5]
with open(file_path, 'rb') as img_file:
b64_string = base64.b64encode(img_file.read()).decode("utf-8")
href_tag = html_line[:html_line.index("href=") + 6] + "data:image/png;base64," + b64_string + "\" />\n"
assembled_html += href_tag
file_path = current_directory + html_line[html_line.index("href=")+7:-4]
href_tag = html_line[:html_line.index("href=")+6] + base_64(file_path) + "\"/>\n"
assembled_html += href_tag
else:
if "%;" in html_line:
html_line = html_line.replace("%", "%%")
assembled_html += html_line

assembled_html += ")rawliteral\";"
Expand Down
Binary file added src/frontend/img/icon_back_100x100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/frontend/img/icon_backward_100x100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/frontend/img/icon_close_100x100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/frontend/img/icon_dropdown_100x100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/frontend/img/icon_forward_100x100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/frontend/img/icon_more_100x100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/frontend/img/icon_open_100x100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/frontend/img/icon_stop_100x100.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit fde068e

Please sign in to comment.