From 17891a2a6473cd917c1fce2213439e1dcd6e21d0 Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Sat, 3 Aug 2019 14:27:13 +0300 Subject: [PATCH] basic gcode sender thing --- App/src/App.tsx | 3 +- App/src/GcodeSender.tsx | 106 +++++++++++++++++++ App/src/api/eggbot/index.ts | 20 ++++ App/src/api/utils.ts | 22 ++++ App/src/index.html | 2 +- Firmware/EggbotWireless/include/WebAPI.h | 2 + Firmware/EggbotWireless/src/QueueManager.cpp | 11 +- Firmware/EggbotWireless/src/WebAPI.cpp | 21 +++- Firmware/MotorControl/include/Globals.h | 19 ++-- Firmware/MotorControl/include/Pen.h | 3 +- Firmware/MotorControl/src/Globals.cpp | 13 +++ Firmware/MotorControl/src/Pen.cpp | 14 +-- Firmware/MotorControl/src/main.cpp | 36 +++---- 13 files changed, 223 insertions(+), 49 deletions(-) create mode 100644 App/src/GcodeSender.tsx create mode 100644 App/src/api/eggbot/index.ts create mode 100644 App/src/api/utils.ts create mode 100644 Firmware/MotorControl/src/Globals.cpp diff --git a/App/src/App.tsx b/App/src/App.tsx index a140dee..a37746f 100644 --- a/App/src/App.tsx +++ b/App/src/App.tsx @@ -1,7 +1,8 @@ import * as React from "react"; +import { GcodeSender } from "~GcodeSender"; export function AppComponent() { - return
Hello
; + return ; } export const App = AppComponent; diff --git a/App/src/GcodeSender.tsx b/App/src/GcodeSender.tsx new file mode 100644 index 0000000..6776e2e --- /dev/null +++ b/App/src/GcodeSender.tsx @@ -0,0 +1,106 @@ +import * as React from "react"; +import { IEggbotStatus, getStatus, putCommand } from "~api/eggbot"; + +interface IGcodeSenderComponentState { + eggbotStatus: IEggbotStatus | null; + gcodeSet: boolean; + gcodeInput: string; + + gcodeLinesSent: string[]; + gcodeLinesQueue: string[]; + + executing: boolean; +} + +const defaultState: IGcodeSenderComponentState = { + eggbotStatus: null, + gcodeSet: false, + gcodeInput: "", + executing: false, + gcodeLinesSent: [], + gcodeLinesQueue: [], +}; + +export class GcodeSenderComponent extends React.PureComponent< + {}, + IGcodeSenderComponentState +> { + constructor() { + super(null); + this.state = defaultState; + + this.handleInputChange = this.handleInputChange.bind(this); + this.handleSend = this.handleSend.bind(this); + this.update = this.update.bind(this); + + setInterval(this.update, 100); + } + + public handleInputChange( + event: + | React.FormEvent + | React.FormEvent, + ) { + const target = event.currentTarget; + const value = target.value; + const name = target.name; + + this.setState({ + [name]: value, + } as any); + } + + public handleSend() { + const lines = this.state.gcodeInput.split("\n"); + this.setState({ gcodeLinesQueue: lines, executing: true }); + } + + public async update() { + const status = await getStatus(); + console.log(status); + const gcodeLinesQueue = [...this.state.gcodeLinesQueue]; + const gcodeLinesSent = [...this.state.gcodeLinesSent]; + const { executing } = this.state; + if (executing && status.commandQueue < 5) { + if (gcodeLinesQueue && gcodeLinesQueue.length > 0) { + const command = gcodeLinesQueue.shift(); + putCommand(command.substr(0, 50)); + gcodeLinesSent.push(command); + } + } + + this.setState({ gcodeLinesQueue, gcodeLinesSent }); + } + + render() { + const queuedCommands = this.state.gcodeLinesQueue.map(el => ( +
{el}
+ )); + + const executedCommands = this.state.gcodeLinesSent.map(el => ( +
{el}
+ )); + + return ( +
+ {this.state.executing ? ( +
+
{executedCommands}
+
{queuedCommands}
+
+ ) : ( +
+