Skip to content

Commit

Permalink
Merge pull request #92 from AllenShintani/master
Browse files Browse the repository at this point in the history
Add some analog sensor devices!
  • Loading branch information
AllenShintani authored May 22, 2024
2 parents 4d7d758 + 0329b62 commit 83dfe70
Show file tree
Hide file tree
Showing 49 changed files with 886 additions and 159 deletions.
14 changes: 6 additions & 8 deletions src/__tests__/procedure/utils/board.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { board } from '../../../procedure/utils/board'

vi.mock('serialport')
vi.mock('ora')
const ARDUINO_PATH = '/dev/ttyUSB0'
const BAUD_RATE = 57600

describe('board', () => {
let mockPort: SerialPort
Expand All @@ -31,8 +33,7 @@ describe('board', () => {
it('should handle port close event', async () => {
vi.spyOn(process, 'exit').mockImplementation(vi.fn())

const arduinoPath = '/dev/ttyUSB0'
board.connectManual(arduinoPath)
board.connectManual(ARDUINO_PATH, BAUD_RATE)

mockPort.on('close', () => {
expect(mockSpinner.fail).toHaveBeenCalledWith('Board is closed.')
Expand All @@ -53,8 +54,7 @@ describe('board', () => {
const listener = vi.fn()
board.on('ready', listener)

const arduinoPath = '/dev/ttyUSB0'
board.connectManual(arduinoPath)
board.connectManual(ARDUINO_PATH, BAUD_RATE)

mockPort.on('data', () => {
expect(board.isReady()).toBe(true)
Expand All @@ -74,8 +74,7 @@ describe('board', () => {
board.on('ready', listener)
board.off('ready', listener)

const arduinoPath = '/dev/ttyUSB0'
board.connectManual(arduinoPath)
board.connectManual(ARDUINO_PATH, BAUD_RATE)
mockPort.emit('data', '*data*')
await new Promise((resolve) => setTimeout(resolve, 0))

Expand All @@ -89,8 +88,7 @@ describe('board', () => {

expect(board.isReady()).toBe(false)

const arduinoPath = '/dev/ttyUSB0'
board.connectManual(arduinoPath)
board.connectManual(ARDUINO_PATH, BAUD_RATE)

mockPort.on('data', () => {
expect(board.isReady()).toBe(true)
Expand Down
61 changes: 61 additions & 0 deletions src/declarative/components/analog/AnalogRotation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { attachAnalog } from '../../../procedure/analog/uniqueDevice/analog'
import type { AnalogPin } from '../../../procedure/types/analog/analog'
import { board } from '../../../procedure/utils/board'
import React, { createContext, useEffect, useState } from 'react'
import type { SerialPort } from 'serialport'

export const AnalogRotationContext = createContext<SerialPort | null>(null)

type AnalogRotationProps = {
pin: AnalogPin
onValueChange?: (value: number) => void
children: (value: number) => React.ReactNode
}

export const AnalogRotation: React.FC<AnalogRotationProps> = ({
pin,
onValueChange,
children,
}) => {
const [port, setPort] = useState<SerialPort | null>(null)
const [value, setValue] = useState<number>(0)

useEffect(() => {
const setupSensor = (port: SerialPort) => {
const waterSensor = attachAnalog(port, pin)

waterSensor.read('change', async (sensorValue: number) => {
setValue(sensorValue)
if (onValueChange) {
onValueChange(sensorValue)
}
})
}

const handleReady = (port: SerialPort) => {
setupSensor(port)
setPort(port)
board.off('ready', handleReady)
}

if (board.isReady()) {
const currentPort = board.getCurrentPort()
if (currentPort) {
setupSensor(currentPort)
setPort(currentPort)
}
} else {
board.on('ready', handleReady)
}

return () => {
board.off('ready', handleReady)
}
}, [pin, onValueChange])

return (
<AnalogRotationContext.Provider value={port}>
{children(value)}
</AnalogRotationContext.Provider>
)
}
61 changes: 61 additions & 0 deletions src/declarative/components/analog/AnalogSoilMoisture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { attachAnalog } from '../../../procedure/analog/uniqueDevice/analog'
import type { AnalogPin } from '../../../procedure/types/analog/analog'
import { board } from '../../../procedure/utils/board'
import React, { createContext, useEffect, useState } from 'react'
import type { SerialPort } from 'serialport'

export const AnalogSoilMoistureContext = createContext<SerialPort | null>(null)

type AnalogSoilMoistureProps = {
pin: AnalogPin
onValueChange?: (value: number) => void
children: (value: number) => React.ReactNode
}

export const AnalogSoilMoisture: React.FC<AnalogSoilMoistureProps> = ({
pin,
onValueChange,
children,
}) => {
const [port, setPort] = useState<SerialPort | null>(null)
const [value, setValue] = useState<number>(0)

useEffect(() => {
const setupSensor = (port: SerialPort) => {
const waterSensor = attachAnalog(port, pin)

waterSensor.read('change', async (sensorValue: number) => {
setValue(sensorValue)
if (onValueChange) {
onValueChange(sensorValue)
}
})
}

const handleReady = (port: SerialPort) => {
setupSensor(port)
setPort(port)
board.off('ready', handleReady)
}

if (board.isReady()) {
const currentPort = board.getCurrentPort()
if (currentPort) {
setupSensor(currentPort)
setPort(currentPort)
}
} else {
board.on('ready', handleReady)
}

return () => {
board.off('ready', handleReady)
}
}, [pin, onValueChange])

return (
<AnalogSoilMoistureContext.Provider value={port}>
{children(value)}
</AnalogSoilMoistureContext.Provider>
)
}
61 changes: 61 additions & 0 deletions src/declarative/components/analog/AnalogSteam.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { attachAnalog } from '../../../procedure/analog/uniqueDevice/analog'
import type { AnalogPin } from '../../../procedure/types/analog/analog'
import { board } from '../../../procedure/utils/board'
import React, { createContext, useEffect, useState } from 'react'
import type { SerialPort } from 'serialport'

export const AnalogSteamContext = createContext<SerialPort | null>(null)

type AnalogSteamProps = {
pin: AnalogPin
onValueChange?: (value: number) => void
children: (value: number) => React.ReactNode
}

export const AnalogSteam: React.FC<AnalogSteamProps> = ({
pin,
onValueChange,
children,
}) => {
const [port, setPort] = useState<SerialPort | null>(null)
const [value, setValue] = useState<number>(0)

useEffect(() => {
const setupSensor = (port: SerialPort) => {
const waterSensor = attachAnalog(port, pin)

waterSensor.read('change', async (sensorValue: number) => {
setValue(sensorValue)
if (onValueChange) {
onValueChange(sensorValue)
}
})
}

const handleReady = (port: SerialPort) => {
setupSensor(port)
setPort(port)
board.off('ready', handleReady)
}

if (board.isReady()) {
const currentPort = board.getCurrentPort()
if (currentPort) {
setupSensor(currentPort)
setPort(currentPort)
}
} else {
board.on('ready', handleReady)
}

return () => {
board.off('ready', handleReady)
}
}, [pin, onValueChange])

return (
<AnalogSteamContext.Provider value={port}>
{children(value)}
</AnalogSteamContext.Provider>
)
}
63 changes: 63 additions & 0 deletions src/declarative/components/analog/AnalogTemperature.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { attachAnalog } from '../../../procedure/analog/uniqueDevice/analog'
import type { AnalogPin } from '../../../procedure/types/analog/analog'
import { board } from '../../../procedure/utils/board'
import React, { createContext, useEffect, useState } from 'react'
import type { SerialPort } from 'serialport'

export const AnalogTemperatureContext = createContext<SerialPort | null>(null)

type AnalogTemperatureProps = {
pin: AnalogPin
onValueChange?: (value: number) => void
children: (value: number) => React.ReactNode
}

export const AnalogTemperature: React.FC<AnalogTemperatureProps> = ({
pin,
onValueChange,
children,
}) => {
const [port, setPort] = useState<SerialPort | null>(null)
const [value, setValue] = useState<number>(0)

useEffect(() => {
const setupSensor = (port: SerialPort) => {
const waterSensor = attachAnalog(port, pin)

waterSensor.read('change', async (sensorValue: number) => {
const fenya = (sensorValue / 1023) * 5
const r = ((5 - fenya) / fenya) * 4700
setValue(1 / (Math.log(r / 10000) / 3950 + 1 / (25 + 273.15)) - 273.15)
if (onValueChange) {
onValueChange(sensorValue)
}
})
}

const handleReady = (port: SerialPort) => {
setupSensor(port)
setPort(port)
board.off('ready', handleReady)
}

if (board.isReady()) {
const currentPort = board.getCurrentPort()
if (currentPort) {
setupSensor(currentPort)
setPort(currentPort)
}
} else {
board.on('ready', handleReady)
}

return () => {
board.off('ready', handleReady)
}
}, [pin, onValueChange])

return (
<AnalogTemperatureContext.Provider value={port}>
{children(value)}
</AnalogTemperatureContext.Provider>
)
}
61 changes: 61 additions & 0 deletions src/declarative/components/analog/AnalogVibration.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { attachAnalog } from '../../../procedure/analog/uniqueDevice/analog'
import type { AnalogPin } from '../../../procedure/types/analog/analog'
import { board } from '../../../procedure/utils/board'
import React, { createContext, useEffect, useState } from 'react'
import type { SerialPort } from 'serialport'

export const AnalogVibrationContext = createContext<SerialPort | null>(null)

type AnalogVibrationProps = {
pin: AnalogPin
onValueChange?: (value: number) => void
children: (value: number) => React.ReactNode
}

export const AnalogVibration: React.FC<AnalogVibrationProps> = ({
pin,
onValueChange,
children,
}) => {
const [port, setPort] = useState<SerialPort | null>(null)
const [value, setValue] = useState<number>(0)

useEffect(() => {
const setupSensor = (port: SerialPort) => {
const waterSensor = attachAnalog(port, pin)

waterSensor.read('change', async (sensorValue: number) => {
setValue(sensorValue)
if (onValueChange) {
onValueChange(sensorValue)
}
})
}

const handleReady = (port: SerialPort) => {
setupSensor(port)
setPort(port)
board.off('ready', handleReady)
}

if (board.isReady()) {
const currentPort = board.getCurrentPort()
if (currentPort) {
setupSensor(currentPort)
setPort(currentPort)
}
} else {
board.on('ready', handleReady)
}

return () => {
board.off('ready', handleReady)
}
}, [pin, onValueChange])

return (
<AnalogVibrationContext.Provider value={port}>
{children(value)}
</AnalogVibrationContext.Provider>
)
}
Loading

0 comments on commit 83dfe70

Please sign in to comment.