2021-08-26 11:39:34 +02:00
|
|
|
import React, { useEffect, useState } from "react";
|
2021-07-10 16:19:37 +02:00
|
|
|
import { createGlobalStyle } from "styled-components";
|
2021-08-26 01:29:59 +02:00
|
|
|
import SerialPort from "serialport";
|
2021-07-10 12:00:50 +02:00
|
|
|
import Knob from "./Components/Knob";
|
2021-08-26 11:39:34 +02:00
|
|
|
import { port, parser } from "./SerialConnection";
|
2021-06-28 17:53:59 +02:00
|
|
|
|
2021-07-10 16:19:37 +02:00
|
|
|
const GlobalStyle = createGlobalStyle`
|
|
|
|
html {
|
|
|
|
border-style: solid;
|
|
|
|
border-color: #363636;
|
|
|
|
border-width: 1px;
|
|
|
|
box-sizing: border-box;
|
|
|
|
height: 100%;
|
|
|
|
border-bottom-style: hidden;
|
|
|
|
border-right-style: hidden;
|
|
|
|
}
|
|
|
|
`;
|
2021-07-09 13:54:25 +02:00
|
|
|
|
2021-07-10 16:19:37 +02:00
|
|
|
const App = () => {
|
2021-08-26 11:39:34 +02:00
|
|
|
const [status, setStatus] = useState(0);
|
|
|
|
const SerialDataListener = (data: string) => {
|
|
|
|
const parsedData = data.split(",");
|
2021-08-26 13:20:26 +02:00
|
|
|
console.log(parsedData);
|
2021-08-26 11:39:34 +02:00
|
|
|
setStatus(parseInt(parsedData[2], 10));
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
parser.on("data", SerialDataListener);
|
|
|
|
port.open();
|
|
|
|
return () => {
|
|
|
|
parser.removeListener("data", SerialDataListener);
|
|
|
|
port.close();
|
|
|
|
};
|
|
|
|
}, []);
|
2021-08-26 01:29:59 +02:00
|
|
|
|
2021-07-10 16:28:20 +02:00
|
|
|
const sendIncreaseHandler = () => {
|
2021-08-26 11:39:34 +02:00
|
|
|
setStatus(status + 1);
|
2021-07-10 16:28:20 +02:00
|
|
|
port.write("2i");
|
|
|
|
};
|
|
|
|
const sendDecreaseHandler = () => {
|
2021-08-26 11:39:34 +02:00
|
|
|
setStatus(status - 1);
|
2021-07-10 16:28:20 +02:00
|
|
|
port.write("2d");
|
|
|
|
};
|
|
|
|
const sendToggleHandler = () => {
|
|
|
|
port.write("2t");
|
2021-07-10 16:19:37 +02:00
|
|
|
};
|
2021-06-29 23:30:22 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
2021-07-10 16:19:37 +02:00
|
|
|
<GlobalStyle />
|
2021-07-10 16:28:20 +02:00
|
|
|
<Knob
|
|
|
|
increase={sendIncreaseHandler}
|
|
|
|
decrease={sendDecreaseHandler}
|
|
|
|
toggle={sendToggleHandler}
|
2021-08-26 11:39:34 +02:00
|
|
|
status={status}
|
2021-07-10 16:28:20 +02:00
|
|
|
/>
|
2021-07-10 16:19:37 +02:00
|
|
|
<button
|
2021-06-29 23:30:22 +02:00
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
|
|
|
port.open();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
open
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
|
|
|
port.close();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
close
|
|
|
|
</button>
|
2021-06-30 09:51:39 +02:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={() => {
|
|
|
|
const list = SerialPort.list();
|
2021-07-10 12:00:50 +02:00
|
|
|
list.then((arg) => {
|
|
|
|
console.log(arg);
|
|
|
|
});
|
2021-06-30 09:51:39 +02:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
list
|
2021-07-10 16:19:37 +02:00
|
|
|
</button>
|
2021-06-29 23:30:22 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
export default App;
|