-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from AllenShintani/master
Add some analog sensor devices!
- Loading branch information
Showing
49 changed files
with
886 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
) | ||
} |
Oops, something went wrong.