From 306f226be3a2ff390a2ef93662c15474704a8fc2 Mon Sep 17 00:00:00 2001 From: Allen Shintani Date: Thu, 9 May 2024 00:45:29 +0900 Subject: [PATCH] fix some input device --- src/declarative/components/servo/Servo.tsx | 20 ++++++------- src/declarative/examples/Servo.tsx | 31 ++++++++++----------- src/procedure/examples/input/inputPort.ts | 16 +++++------ src/procedure/helper/Input/setInputState.ts | 31 +++------------------ src/procedure/utils/board.ts | 2 +- 5 files changed, 37 insertions(+), 63 deletions(-) diff --git a/src/declarative/components/servo/Servo.tsx b/src/declarative/components/servo/Servo.tsx index b56244f..8da66ef 100644 --- a/src/declarative/components/servo/Servo.tsx +++ b/src/declarative/components/servo/Servo.tsx @@ -1,4 +1,3 @@ -import { useEffect } from 'react' import { attachServo } from '../../../procedure/examples/servo/uniqueDevice/servo' import { board } from '../../../procedure/utils/board' import type React from 'react' @@ -9,15 +8,16 @@ type ServoProps = { } export const Servo: React.FC = ({ pin, angle }) => { - useEffect(() => { - if (board.isReady()) { - const port = board.getCurrentPort() - if (port) { - const servo = attachServo(port, pin) - servo.setAngle(angle) - } - } - }, [angle, pin]) + const port = board.getCurrentPort() + if (!port) { + console.error('Board is not connected.') + return null + } + const servo = attachServo(port, pin) + + if (board.isReady()) { + servo.setAngle(angle) + } return null } diff --git a/src/declarative/examples/Servo.tsx b/src/declarative/examples/Servo.tsx index 228e722..3da6722 100644 --- a/src/declarative/examples/Servo.tsx +++ b/src/declarative/examples/Servo.tsx @@ -3,46 +3,43 @@ import { useState } from 'react' import { Board } from '../utils/Board' import { render } from '../rendere/render' import { Button } from '../components/input/Button' -import { Led } from '../components/output/Led' import { Servo } from '../components/servo/Servo' const App: React.FC = () => { const [angle, setAngle] = useState(0) - const [isOn, setIsOn] = useState(false) const handlePress = () => { - setAngle(angle + 10) - setIsOn(true) - } - - const handleRelease = () => { if (angle >= 150) { setAngle(0) - setIsOn(false) - return } - setAngle(angle + 10) - setIsOn(false) + } + + const handleRelease = () => { + setAngle((prevAngle) => { + const newAngle = prevAngle + 10 + + return newAngle + }) } return ( ) diff --git a/src/procedure/examples/input/inputPort.ts b/src/procedure/examples/input/inputPort.ts index d8d6b7a..ccf95b7 100644 --- a/src/procedure/examples/input/inputPort.ts +++ b/src/procedure/examples/input/inputPort.ts @@ -4,7 +4,7 @@ import type { Sensor } from '../../types/analog/analog' export const inputPort = (port: SerialPort) => { return (pin: number) => { - let prevValue: boolean | null = null + let prevValue: boolean = false return { read: ( @@ -14,15 +14,15 @@ export const inputPort = (port: SerialPort) => { const observable = setInputState(pin, port) observable.subscribe((value: boolean) => { - if (method === 'change') { - if (value !== prevValue) { - prevValue = value - func() - } - } else if (method === 'off' && value && prevValue !== value) { + if (method === 'change' && value !== prevValue) { prevValue = value func() - } else if (method === 'on' && !value && prevValue !== value) { + } + if (method === 'off' && !value && prevValue !== value) { + prevValue = value + func() + } + if (method === 'on' && value && prevValue !== value) { prevValue = value func() } diff --git a/src/procedure/helper/Input/setInputState.ts b/src/procedure/helper/Input/setInputState.ts index 7e73b7a..8073598 100644 --- a/src/procedure/helper/Input/setInputState.ts +++ b/src/procedure/helper/Input/setInputState.ts @@ -2,7 +2,7 @@ import type { SerialPort } from 'serialport' import { bufferWrite } from '../Utils/bufferWrite' import type { Observable } from 'rxjs' import { fromEventPattern } from 'rxjs' -import { filter, map, scan } from 'rxjs/operators' +import { distinctUntilChanged, filter, map } from 'rxjs/operators' import { Buffer } from 'node:buffer' const SET_PIN_MODE = 0xf4 @@ -24,31 +24,8 @@ export const setInputState = ( (handler) => port.removeListener('data', handler), ).pipe( map((data) => (data.length <= 3 ? data : data.subarray(0, 3))), - scan( - ( - acc: { preBinary?: Buffer; lastState?: boolean; emit: boolean }, - currentBinary: Buffer, - ) => { - const pinIndex = pin % 8 - const isRelevantPin = (currentBinary[0] & 0x0f) === Math.floor(pin / 8) - const currentState = - isRelevantPin && ((currentBinary[1] >> pinIndex) & 1) === 1 - if ( - !isRelevantPin || - (acc.preBinary && acc.preBinary.equals(currentBinary)) - ) { - return { ...acc, emit: false } - } - - return { - preBinary: currentBinary, - lastState: currentState, - emit: acc.lastState !== currentState, - } - }, - { emit: false }, - ), - filter((acc) => acc.emit), - map((acc) => acc.lastState as boolean), + filter((data) => (data[0] & 0x0f) === Math.floor(pin / 8)), + map((data) => ((data[1] >> pin % 8) & 1) === 1), + distinctUntilChanged(), ) } diff --git a/src/procedure/utils/board.ts b/src/procedure/utils/board.ts index d79aca0..2be6a0c 100644 --- a/src/procedure/utils/board.ts +++ b/src/procedure/utils/board.ts @@ -7,7 +7,7 @@ const boardEmitter = new EventEmitter() let currentPort: SerialPort | null = null let isPortActive = false -const MAX_RECENT_LISTENERS = 6 +const MAX_RECENT_LISTENERS = 2 const confirmConnection = async (port: SerialPort) => { const endTimeout = Date.now() + 10000