Hook up interface to serial connection
This commit is contained in:
parent
a7af6cc8de
commit
0c41a85733
4 changed files with 39 additions and 51 deletions
|
@ -1,6 +1,8 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import styled from "styled-components";
|
import styled from "styled-components";
|
||||||
// import { RootState } from "typesafe-actions";
|
import { useAppDispatch, useAppSelector } from "../redux/hooks";
|
||||||
|
import { sendMessage } from "../redux/slices/serialConnectionSlice";
|
||||||
|
import Knob from "./Knob";
|
||||||
|
|
||||||
const KnobContainer = styled.div`
|
const KnobContainer = styled.div`
|
||||||
display: flex;
|
display: flex;
|
||||||
|
@ -8,58 +10,35 @@ justify-content: space-between;
|
||||||
margin: 1rem;
|
margin: 1rem;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const KnobSection = () =>
|
const ids: number[] = [0, 1, 2, 3];
|
||||||
// const [status, setStatus] = useState([{ id: 0, value: 0 }, { id: 1, value: 0 }, { id: 2, value: 0 }, { id: 3, value: 0 }]);
|
|
||||||
// const serialConnection = useSelector<RootState, RootState["serialConnection"]>((state) => state.serialConnection);
|
|
||||||
|
|
||||||
// const SerialDataListener = (data: string) => {
|
const KnobSection = () => {
|
||||||
// const parsedData = data.split(",");
|
const serialConnection = useAppSelector((state) => state.serialConnection);
|
||||||
// console.log(parsedData);
|
const dispatch = useAppDispatch();
|
||||||
// setStatus(status.map((element) => ({ ...element, value: parseInt(parsedData[element.id], 10) })));
|
const sendIncreaseHandler = (index: number) => {
|
||||||
// };
|
dispatch(sendMessage(`${index}i`));
|
||||||
|
console.log(index);
|
||||||
// useEffect(() => {
|
};
|
||||||
// if (serialConnection.portController !== null) {
|
const sendDecreaseHandler = (index: number) => {
|
||||||
// serialConnection.portController.parser.on("data", SerialDataListener);
|
dispatch(sendMessage(`${index}d`));
|
||||||
// }
|
console.log(index);
|
||||||
|
};
|
||||||
// return () => {
|
const sendToggleHandler = (index: number) => {
|
||||||
// if (serialConnection.portController !== null) {
|
dispatch(sendMessage(`${index}t`));
|
||||||
// serialConnection.portController.parser.removeListener("data", SerialDataListener);
|
console.log(index);
|
||||||
// }
|
};
|
||||||
// };
|
return (
|
||||||
// }, [serialConnection]);
|
|
||||||
|
|
||||||
// const sendIncreaseHandler = (index: number) => {
|
|
||||||
// // if (serialConnection.portController !== null && serialConnection.portController.port !== null) {
|
|
||||||
// // serialConnection.portController.port.write(`${index}i`);
|
|
||||||
// // }
|
|
||||||
// console.log(index);
|
|
||||||
// };
|
|
||||||
// const sendDecreaseHandler = (index: number) => {
|
|
||||||
// // if (serialConnection.portController !== null && serialConnection.portController.port !== null) {
|
|
||||||
// // serialConnection.portController.port.write(`${index}d`);
|
|
||||||
// // }
|
|
||||||
// console.log(index);
|
|
||||||
// };
|
|
||||||
// const sendToggleHandler = (index: number) => {
|
|
||||||
// // if (serialConnection.portController !== null && serialConnection.portController.port !== null) {
|
|
||||||
// // serialConnection.portController.port.write(`${index}t`);
|
|
||||||
// // }
|
|
||||||
// console.log(index);
|
|
||||||
// };
|
|
||||||
// eslint-disable-next-line implicit-arrow-linebreak
|
|
||||||
(
|
|
||||||
<KnobContainer>
|
<KnobContainer>
|
||||||
{/* {status.map((element) => (
|
{ids.map((id) => (
|
||||||
<Knob
|
<Knob
|
||||||
key={element.id}
|
key={id}
|
||||||
increase={() => { sendIncreaseHandler(element.id); }}
|
increase={() => { sendIncreaseHandler(id); }}
|
||||||
decrease={() => { sendDecreaseHandler(element.id); }}
|
decrease={() => { sendDecreaseHandler(id); }}
|
||||||
toggle={() => { sendToggleHandler(element.id); }}
|
toggle={() => { sendToggleHandler(id); }}
|
||||||
status={Math.floor((element.value / 255) * 100)}
|
status={Math.floor((parseInt(serialConnection.message.split(",")[id], 10) / 255) * 100)}
|
||||||
/>
|
/>
|
||||||
))} */}
|
))}
|
||||||
</KnobContainer>
|
</KnobContainer>
|
||||||
);
|
);
|
||||||
|
};
|
||||||
export default KnobSection;
|
export default KnobSection;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { Middleware, Action } from "@reduxjs/toolkit";
|
import { Middleware, Action } from "@reduxjs/toolkit";
|
||||||
import { RootState } from "../store/types.d";
|
import { RootState } from "../store/types.d";
|
||||||
import {
|
import {
|
||||||
connect, disconnect, setSerialPort, setMessage,
|
connect, disconnect, setSerialPort, setMessage, sendMessage,
|
||||||
} from "../slices/serialConnectionSlice";
|
} from "../slices/serialConnectionSlice";
|
||||||
import PortController from "../../serial/PortController";
|
import PortController from "../../serial/PortController";
|
||||||
import store from "../store";
|
import store from "../store";
|
||||||
|
@ -22,6 +22,8 @@ const serialConnection: Middleware<{}, RootState> = () => (next) => (action: Act
|
||||||
} else if (setSerialPort.match(action)) {
|
} else if (setSerialPort.match(action)) {
|
||||||
serialPort = new PortController(action.payload);
|
serialPort = new PortController(action.payload);
|
||||||
console.log("setSerialPort");
|
console.log("setSerialPort");
|
||||||
|
} else if (sendMessage.match(action)) {
|
||||||
|
serialPort?.write(action.payload);
|
||||||
}
|
}
|
||||||
return next(action);
|
return next(action);
|
||||||
};
|
};
|
||||||
|
|
|
@ -45,12 +45,15 @@ export const serialConnectionSlice = createSlice({
|
||||||
setMessage: (state, action: PayloadAction<string>) => {
|
setMessage: (state, action: PayloadAction<string>) => {
|
||||||
state.message = action.payload;
|
state.message = action.payload;
|
||||||
},
|
},
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
|
sendMessage: (_state, _action: PayloadAction<string>) => {
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export const {
|
export const {
|
||||||
setSerialPort, connectionStart, connectionSuccess, connectionFailure, connectionEnd,
|
setSerialPort, connectionStart, connectionSuccess, connectionFailure, connectionEnd,
|
||||||
connect, disconnect, setMessage,
|
connect, disconnect, setMessage, sendMessage,
|
||||||
} = serialConnectionSlice.actions;
|
} = serialConnectionSlice.actions;
|
||||||
|
|
||||||
// Other code such as selectors can use the imported `RootState` type
|
// Other code such as selectors can use the imported `RootState` type
|
||||||
|
|
|
@ -46,6 +46,10 @@ class PortController {
|
||||||
this.port.removeListener("data", this.onCloseCallback);
|
this.port.removeListener("data", this.onCloseCallback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
write(message: string) {
|
||||||
|
this.port.write(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default PortController;
|
export default PortController;
|
||||||
|
|
Reference in a new issue