Skip to content

Commit

Permalink
Merge pull request #64 from AllenShintani/edison-feature-63
Browse files Browse the repository at this point in the history
inputState bug reviced and refactored
  • Loading branch information
AllenShintani authored Feb 1, 2024
2 parents 39c6a3c + 30168ef commit d65251b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 74 deletions.
File renamed without changes.
52 changes: 10 additions & 42 deletions src/factory/input/inputPort.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import type { SerialPort } from 'serialport'
import { setInputState } from '../../helper/Input/setInputState'
import { Subscription } from 'rxjs'
import { Sensor } from '../../types/analog/analog'
import microtime from 'microtime'
import type { Sensor } from '../../types/analog/analog'

export const inputPort = (port: SerialPort) => {
return (pin: number) => {
let subscription: Subscription
let prevValue: boolean
// let startTime: number
// let endTime: number

return {
read: async (
Expand All @@ -18,42 +13,15 @@ export const inputPort = (port: SerialPort) => {
): Promise<void> => {
const observable = setInputState(pin, port)

subscription = observable.subscribe((value: boolean) => {
//console.log('value: ', value, 'prevValue: ', prevValue)
const currentTime = microtime.now() // 現在の時刻を取得

if (typeof prevValue !== 'undefined') {
// first emit will be skipped
if (value && method === 'on') {
func()
} else if (value === false && method === 'off') {
//console.log(value)
func()
} else if (method === 'change' && value !== prevValue) {
// is value changed?
func()
}
/*
else if (method === 'sonic' && value !== prevValue) {
if (value && !prevValue) {
// EchoピンがLOWからHIGHに変わった瞬間
startTime = currentTime
} else if (!value && prevValue) {
// EchoピンがHIGHからLOWに変わった瞬間
endTime = currentTime
// 時間差を計算(パルス幅)
const pulseWidth = endTime - startTime
console.log(`Pulse Width: ${pulseWidth} us`)
// 往復距離を半分にして距離を計算
const distance = ((pulseWidth / 2) * 340 * 100) / 1000000 // 音速を340m/sに設定
console.log(`Distance: ${distance} cm`)
// funcに距離を渡す
//func(distance)
}
}
*/
observable.subscribe((value: boolean) => {
if (value && method === 'off') {
func()
}
if (value === false && method === 'on') {
func()
}
if (method === 'change' && value !== prevValue) {
func()
}
prevValue = value
})
Expand Down
2 changes: 1 addition & 1 deletion src/factory/input/uniqueDevice/collisionSensor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inputPort } from '../inputPort'
import type { SerialPort } from '../../../index'
import { Sensor } from '../../../types/analog/analog'
import type { Sensor } from '../../../types/analog/analog'

export const attachCollisionSensor = (port: SerialPort, pin: number) => {
const collisionSensor = inputPort(port)(pin)
Expand Down
2 changes: 1 addition & 1 deletion src/factory/input/uniqueDevice/pushButton.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Sensor } from '../../../types/analog/analog'
import type { Sensor } from '../../../types/analog/analog'
import { inputPort } from '../inputPort'
import type { SerialPort } from 'serialport'

Expand Down
37 changes: 12 additions & 25 deletions src/helper/Input/setInputState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,20 @@ export const setInputState = (
bufferWrite(port, buffer2)

return new Observable<boolean>((observer) => {
let buffer = Buffer.alloc(0) // Initialize an empty buffer
let isObserver = false // Prevent events from firing in succession.
port.on('data', (data: Buffer) => {
let lastState: undefined | boolean = undefined

port.on('data', (data) => {
// Check if the new data starts with 0x91 or 0x90, if so, reset the buffer
if (
data.length > 0 &&
4 > data.length &&
(data[0] === 0x91 || data[0] === 0x90)
) {
buffer = data // Reset buffer with new data
} else if (4 > data.length) {
const dataToAppend = data.slice(0, 3) // dataToAppend is the new data
buffer = Buffer.concat([buffer, dataToAppend])
}
if (data.length === 0) return
if (data[0] !== 0x90 && data[0] !== 0x91) return
if (data[0] === 0x90 && pin >= 8) return
if (data[0] === 0x91 && pin < 8) return

const buffer = data.length <= 3 ? data : data.subarray(0, 3)
const currentState = !!(buffer[1] & (1 << pin % 8))

if (
((buffer[0] === 0x90 && pin < 8) || (buffer[0] === 0x91 && pin >= 8)) &&
buffer.length === 3
) {
if (buffer[1] & (1 << pin % 8) && isObserver === true) {
isObserver = false
observer.next(false)
} else if (isObserver === false) {
isObserver = true
observer.next(true)
}
if (currentState !== lastState) {
lastState = currentState
observer.next(currentState)
}
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export { attachServo } from './factory/servo/uniqueDevice/servo'
//export { attachUltrasonicSensor } from './factory/complex/ultrasonicSensor'
export { attachRgbLed } from './factory/output/uniqueDevice/rgbLed'
export { attachCollisionSensor } from './factory/input/uniqueDevice/collisionSensor'
//export { attachLineTracking } from './factory/input/uniqueDevice/lineTracking'
export { attachOutput } from './factory/output/uniqueDevice/output'
export { attachHallEffectSensor } from './factory/input/uniqueDevice/hallEffectSensor'
export { attachButton } from './factory/input/uniqueDevice/pushButton'
//export { attachLineTracking } from './factory/input/uniqueDevice/lineTracking'
export { attachOutput } from './factory/output/uniqueDevice/output'
//export { attachInfraredObstacleAvoidanceSensor } from './factory/input/uniqueDevice/infraredObstacleAvoidanceSensor'
export { delay } from './utils/delay'
export { SerialPort } from 'serialport'
5 changes: 2 additions & 3 deletions src/tsxTest/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import { LED } from './LedComponent'
const App: React.FC = () => {
const [isLedOn, setLedOn] = useState(false)

// ここでセンサーや他の入力を監視して、isLedOnの状態を更新することができます
useEffect(() => {
// センサーの読み取りやボタンのクリックを監視して、setLedOnを呼び出してLEDの状態を更新します
// Monitor sensor readings and button clicks and call setLedOn to update LED state
}, [])

return (
<Board onReady={(port) => console.log('Board is ready!')}>
<Board>
<LED
pin={13}
isOn={isLedOn}
Expand Down

0 comments on commit d65251b

Please sign in to comment.