From 348d6e80c8b94759f4fc38cb904b82839f370a52 Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Mon, 5 Aug 2019 11:04:59 +0300 Subject: [PATCH] gcode parser thing --- App/package-lock.json | 25 ++++++++++++++++ App/package.json | 4 ++- App/src/GcodeParser.tsx | 56 ++++++++++++++++++++++++++++++++++ App/src/GcodeSender.tsx | 60 +++++++++++++++++++++---------------- App/src/GcodeVisualiser.tsx | 32 ++++++++++++++++++++ 5 files changed, 150 insertions(+), 27 deletions(-) create mode 100644 App/src/GcodeParser.tsx create mode 100644 App/src/GcodeVisualiser.tsx diff --git a/App/package-lock.json b/App/package-lock.json index 3161af9..f18066a 100644 --- a/App/package-lock.json +++ b/App/package-lock.json @@ -5819,6 +5819,11 @@ "integrity": "sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==", "dev": true }, + "konva": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/konva/-/konva-4.0.0.tgz", + "integrity": "sha512-TR1YUga/x8NShLS8vryzEXVDC8kQThOZRxX8x99/ABnfVRH4MN9LtBqn8YJxQWByMcwqjAzlNQWA3XuxCCVW0Q==" + }, "lcid": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/lcid/-/lcid-2.0.0.tgz", @@ -8146,6 +8151,26 @@ "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.8.6.tgz", "integrity": "sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==" }, + "react-konva": { + "version": "16.8.7-3", + "resolved": "https://registry.npmjs.org/react-konva/-/react-konva-16.8.7-3.tgz", + "integrity": "sha512-ybtT21EjinPFnN9/EqjWvbLNOd5uDwjkUWkITkP9OTaItZKfbCZw5aVpjpxTrRC69v4rzXXG+cdHAsbE6zt/6A==", + "requires": { + "react-reconciler": "^0.20.4", + "scheduler": "^0.13.6" + } + }, + "react-reconciler": { + "version": "0.20.4", + "resolved": "https://registry.npmjs.org/react-reconciler/-/react-reconciler-0.20.4.tgz", + "integrity": "sha512-kxERc4H32zV2lXMg/iMiwQHOtyqf15qojvkcZ5Ja2CPkjVohHw9k70pdDBwrnQhLVetUJBSYyqU3yqrlVTOajA==", + "requires": { + "loose-envify": "^1.1.0", + "object-assign": "^4.1.1", + "prop-types": "^15.6.2", + "scheduler": "^0.13.6" + } + }, "react-test-renderer": { "version": "16.8.6", "resolved": "https://registry.npmjs.org/react-test-renderer/-/react-test-renderer-16.8.6.tgz", diff --git a/App/package.json b/App/package.json index ebc526c..bd65408 100644 --- a/App/package.json +++ b/App/package.json @@ -34,9 +34,11 @@ "typescript": "^3.5.3" }, "dependencies": { + "konva": "^4.0.0", "normalize.css": "^8.0.1", "react": "^16.8.6", - "react-dom": "^16.8.6" + "react-dom": "^16.8.6", + "react-konva": "^16.8.7-3" }, "postcss": { "plugins": { diff --git a/App/src/GcodeParser.tsx b/App/src/GcodeParser.tsx new file mode 100644 index 0000000..0e44122 --- /dev/null +++ b/App/src/GcodeParser.tsx @@ -0,0 +1,56 @@ +export enum CommandType { + unk = "unk", + G01 = "G01", + G00 = "G00", +} + +export interface GenericCommand { + type: CommandType; +} + +export interface G01Command extends GenericCommand { + type: CommandType.G01; + X?: number; + Y?: number; + Z?: number; + F?: number; +} + +export interface G00Command extends GenericCommand { + type: CommandType.G00; + X?: number; + Y?: number; + Z?: number; + F?: number; +} + +export interface UnkCommand extends GenericCommand { + type: CommandType.unk; +} + +export type Command = G01Command | G00Command | UnkCommand; + +function getCommandType(type: string): CommandType { + if (type.toUpperCase() === "G01") { + return CommandType.G01; + } + if (type.toUpperCase() === "G00") { + return CommandType.G00; + } + + return CommandType.unk; +} + +export function parseCommand(cmd: string): Command { + const words = cmd.toUpperCase().split(" "); + let command: Command = { type: getCommandType(words.shift()) }; + + const args = words.map(word => { + const arg = word.slice(0, 1); + const val = word.slice(1); + (command as any)[arg] = parseFloat(val); + }); + + console.log(command); + return command; +} diff --git a/App/src/GcodeSender.tsx b/App/src/GcodeSender.tsx index 6776e2e..0976e4e 100644 --- a/App/src/GcodeSender.tsx +++ b/App/src/GcodeSender.tsx @@ -1,5 +1,7 @@ import * as React from "react"; import { IEggbotStatus, getStatus, putCommand } from "~api/eggbot"; +import GcodeVisualiser from "~GcodeVisualiser"; +import { parseCommand } from "~GcodeParser"; interface IGcodeSenderComponentState { eggbotStatus: IEggbotStatus | null; @@ -25,8 +27,8 @@ export class GcodeSenderComponent extends React.PureComponent< {}, IGcodeSenderComponentState > { - constructor() { - super(null); + constructor(props: {}) { + super(props); this.state = defaultState; this.handleInputChange = this.handleInputChange.bind(this); @@ -57,13 +59,13 @@ export class GcodeSenderComponent extends React.PureComponent< 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(); + console.log(parseCommand(command)); putCommand(command.substr(0, 50)); gcodeLinesSent.push(command); } @@ -73,32 +75,38 @@ export class GcodeSenderComponent extends React.PureComponent< } render() { - const queuedCommands = this.state.gcodeLinesQueue.map(el => ( -
{el}
- )); + const { gcodeLinesQueue, gcodeLinesSent } = this.state; - const executedCommands = this.state.gcodeLinesSent.map(el => ( -
{el}
- )); + const queuedCommandsList = gcodeLinesQueue.map(el =>
{el}
); + + const executedCommandsList = gcodeLinesSent.map(el =>
{el}
); return ( -
- {this.state.executing ? ( -
-
{executedCommands}
-
{queuedCommands}
-
- ) : ( -
-