Probably fix knob skipping steps if devtools closed

This commit is contained in:
GHOSCHT 2021-08-26 01:29:59 +02:00
parent b3bf0fc42b
commit 9cd11ed62f
3 changed files with 46 additions and 19 deletions

View file

@ -1,7 +1,8 @@
import React from "react"; import React, { useState } from "react";
import SerialPort from "serialport";
import { createGlobalStyle } from "styled-components"; import { createGlobalStyle } from "styled-components";
import SerialPort from "serialport";
import Knob from "./Components/Knob"; import Knob from "./Components/Knob";
import SerialConnection from "./SerialConnection";
const GlobalStyle = createGlobalStyle` const GlobalStyle = createGlobalStyle`
html { html {
@ -15,15 +16,17 @@ const GlobalStyle = createGlobalStyle`
} }
`; `;
const port = SerialConnection();
const App = () => { const App = () => {
const port = new SerialPort("COM5", { const [counter, setCounter] = useState(0);
baudRate: 9600,
autoOpen: false,
});
const sendIncreaseHandler = () => { const sendIncreaseHandler = () => {
setCounter(counter + 1);
port.write("2i"); port.write("2i");
}; };
const sendDecreaseHandler = () => { const sendDecreaseHandler = () => {
setCounter(counter - 1);
port.write("2d"); port.write("2d");
}; };
const sendToggleHandler = () => { const sendToggleHandler = () => {
@ -37,6 +40,7 @@ const App = () => {
increase={sendIncreaseHandler} increase={sendIncreaseHandler}
decrease={sendDecreaseHandler} decrease={sendDecreaseHandler}
toggle={sendToggleHandler} toggle={sendToggleHandler}
status={counter}
/> />
<button <button
type="button" type="button"
@ -46,6 +50,14 @@ const App = () => {
> >
open open
</button> </button>
<button
type="button"
onClick={() => {
setCounter(counter + 1);
}}
>
increase
</button>
<button <button
type="button" type="button"
onClick={() => { onClick={() => {

View file

@ -1,4 +1,4 @@
import React, { useRef } from "react"; import React, { useRef, useState } from "react";
import styled from "styled-components"; import styled from "styled-components";
import { remote } from "electron"; import { remote } from "electron";
@ -24,6 +24,8 @@ const KnobSVG = styled.svg`
} }
#Status { #Status {
user-select: none; user-select: none;
vertical-align: middle;
display: inline-block;
} }
`; `;
@ -31,13 +33,15 @@ interface Proptypes {
increase: () => void; increase: () => void;
decrease: () => void; decrease: () => void;
toggle: () => void; toggle: () => void;
status: number;
} }
const Knob = (props: Proptypes) => { const Knob = ({
increase, decrease, toggle, status,
}: Proptypes) => {
const overlayRef = useRef<SVGGElement>(null); const overlayRef = useRef<SVGGElement>(null);
const [isMouseDown, setisMouseDown] = useState(false);
let isMouseDown = false; const [prevAngle, setPrevAngle] = useState(0);
let prevAngle = 0;
const move = (e: React.MouseEvent<SVGElement, MouseEvent>) => { const move = (e: React.MouseEvent<SVGElement, MouseEvent>) => {
const relativePosX = e.nativeEvent.offsetX - SvgHeight / 2; const relativePosX = e.nativeEvent.offsetX - SvgHeight / 2;
@ -60,23 +64,23 @@ const Knob = (props: Proptypes) => {
if (angle % steps === Math.round(angle % steps)) { if (angle % steps === Math.round(angle % steps)) {
if (prevAngle < angle) { if (prevAngle < angle) {
props.increase(); increase();
} else if (prevAngle > angle) { } else if (prevAngle > angle) {
props.decrease(); decrease();
} }
} }
prevAngle = angle; setPrevAngle(angle);
}; };
const mouseDownHandler: React.MouseEventHandler<SVGElement> = (e) => { const mouseDownHandler: React.MouseEventHandler<SVGElement> = (e) => {
if (e.button !== 2) { if (e.button !== 2) {
move(e); move(e);
isMouseDown = true; setisMouseDown(true);
} }
}; };
const mouseUpHandler: React.MouseEventHandler<SVGElement> = () => { const mouseUpHandler: React.MouseEventHandler<SVGElement> = () => {
isMouseDown = false; setisMouseDown(false);
}; };
const mouseMoveHandler: React.MouseEventHandler<SVGElement> = (e) => { const mouseMoveHandler: React.MouseEventHandler<SVGElement> = (e) => {
@ -91,7 +95,7 @@ const Knob = (props: Proptypes) => {
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 141.73 141.73" viewBox="0 0 141.73 141.73"
height={`${SvgHeight}px`} height={`${SvgHeight}px`}
onContextMenu={() => { props.toggle(); }} onContextMenu={() => { toggle(); }}
onMouseDown={mouseDownHandler} onMouseDown={mouseDownHandler}
onMouseUp={mouseUpHandler} onMouseUp={mouseUpHandler}
onMouseMove={mouseMoveHandler} onMouseMove={mouseMoveHandler}
@ -104,8 +108,8 @@ const Knob = (props: Proptypes) => {
/> />
</g> </g>
<g id="Status"> <g id="Status">
<text className="cls-2" transform="translate(53.61 77.41)"> <text className="cls-2" transform="translate(70.41 77.41)" textAnchor="middle">
255 {status}
</text> </text>
</g> </g>
<g id="Overlay" ref={overlayRef}> <g id="Overlay" ref={overlayRef}>

View file

@ -0,0 +1,11 @@
import SerialPort from "serialport";
const newPort = () => {
const port = new SerialPort("COM5", {
baudRate: 9600,
autoOpen: false,
});
return port;
};
export default newPort;