Skip to content

Commit

Permalink
Merge pull request #91 from AllenShintani/master
Browse files Browse the repository at this point in the history
fix some input device
  • Loading branch information
AllenShintani authored May 8, 2024
2 parents 8b658a8 + 306f226 commit 4d7d758
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 63 deletions.
20 changes: 10 additions & 10 deletions src/declarative/components/servo/Servo.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -9,15 +8,16 @@ type ServoProps = {
}

export const Servo: React.FC<ServoProps> = ({ 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
}
31 changes: 14 additions & 17 deletions src/declarative/examples/Servo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Board
port={'/dev/ttyUSB0'}
baudRate={115200}
port={'/dev/cu.usbserial-140'}
baudRate={57600}
>
<Button
pin={8}
triggered={handlePress}
untriggered={handleRelease}
>
<Led
pin={13}
isOn={isOn}
/>
<Servo
pin={10}
angle={angle}
/>
<Servo
pin={12}
angle={angle}
/>
</Button>
</Board>
)
Expand Down
16 changes: 8 additions & 8 deletions src/procedure/examples/input/inputPort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
Expand All @@ -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()
}
Expand Down
31 changes: 4 additions & 27 deletions src/procedure/helper/Input/setInputState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
)
}
2 changes: 1 addition & 1 deletion src/procedure/utils/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4d7d758

Please sign in to comment.