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

View file

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