Skip to content

Commit

Permalink
Merge pull request #89 from AllenShintani/master
Browse files Browse the repository at this point in the history
add ESP32 devices
  • Loading branch information
AllenShintani authored May 1, 2024
2 parents 30e5e1d + 62bc925 commit 8b658a8
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 44 deletions.
5 changes: 4 additions & 1 deletion src/declarative/examples/ VibrationMotorModule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const App: React.FC = () => {
const [value, setValue] = useState(0)

return (
<Board port={'/dev/ttyUSB0'}>
<Board
port={'/dev/ttyUSB0'}
baudRate={115200}
>
<Button
pin={8}
triggered={() => {
Expand Down
5 changes: 4 additions & 1 deletion src/declarative/examples/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { Led } from '../components/output/Led'

const App: React.FC = () => {
return (
<Board port={'/dev/ttyUSB0'}>
<Board
port={'/dev/ttyUSB0'}
baudRate={115200}
>
<Led
pin={13}
blink={500}
Expand Down
5 changes: 4 additions & 1 deletion src/declarative/examples/CollisionSensor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const App: React.FC = () => {
const [isOn, setIsOn] = useState(false)

return (
<Board port={'/dev/ttyUSB0'}>
<Board
port={'/dev/ttyUSB0'}
baudRate={115200}
>
<Collision
pin={8}
triggered={() => setIsOn(true)}
Expand Down
5 changes: 4 additions & 1 deletion src/declarative/examples/DigiTaltiltSensor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const App: React.FC = () => {
const [isOn, setIsOn] = useState(false)

return (
<Board port={'/dev/ttyUSB0'}>
<Board
port={'/dev/ttyUSB0'}
baudRate={115200}
>
<DigitalTilt
pin={8}
triggered={() => {
Expand Down
5 changes: 4 additions & 1 deletion src/declarative/examples/HallEffectSensor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ const App: React.FC = () => {
const [isOn, setIsOn] = useState(false)

return (
<Board port={'/dev/ttyUSB0'}>
<Board
port={'/dev/ttyUSB0'}
baudRate={115200}
>
<HallEffective
pin={8}
triggered={() => setIsOn(true)}
Expand Down
5 changes: 4 additions & 1 deletion src/declarative/examples/Inputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const App: React.FC = () => {
const [isOn1, setIsOn1] = useState(false)

return (
<Board port={'/dev/ttyUSB0'}>
<Board
port={'/dev/ttyUSB0'}
baudRate={115200}
>
<Button
pin={8}
triggered={() => setIsOn(true)}
Expand Down
5 changes: 4 additions & 1 deletion src/declarative/examples/Led.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const App: React.FC = () => {
const [ledOne, setLedOne] = useState(false)

return (
<Board port={'/dev/ttyUSB0'}>
<Board
port={'/dev/ttyUSB0'}
baudRate={115200}
>
<Button
pin={8}
triggered={() => setLedOne(true)}
Expand Down
5 changes: 4 additions & 1 deletion src/declarative/examples/PIRMotionSensor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const App: React.FC = () => {
const [isOn, setIsOn] = useState(false)

return (
<Board port={'/dev/ttyUSB0'}>
<Board
port={'/dev/ttyUSB0'}
baudRate={115200}
>
<PIRMotion
pin={8}
triggered={() => {
Expand Down
5 changes: 4 additions & 1 deletion src/declarative/examples/PhotoInterrupter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ const App: React.FC = () => {
const [isOn, setIsOn] = useState(false)

return (
<Board port={'/dev/ttyUSB0'}>
<Board
port={'/dev/ttyUSB0'}
baudRate={115200}
>
<PhotoInterrupter
pin={8}
triggered={() => {
Expand Down
5 changes: 4 additions & 1 deletion src/declarative/examples/Servo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ const App: React.FC = () => {
}

return (
<Board port={'/dev/ttyUSB0'}>
<Board
port={'/dev/ttyUSB0'}
baudRate={115200}
>
<Button
pin={8}
triggered={handlePress}
Expand Down
5 changes: 3 additions & 2 deletions src/declarative/utils/Board.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ export const BoardContext = createContext<SerialPort | null>(null)
type BoardProps = {
children: React.ReactNode
port: string
baudRate: number
}

export const Board: React.FC<BoardProps> = ({ children, port }) => {
export const Board: React.FC<BoardProps> = ({ children, port, baudRate }) => {
const currentPort = board.getCurrentPort()

if (currentPort) {
return (
<BoardContext.Provider value={null}>{children}</BoardContext.Provider>
)
}
board.connectManual(port)
board.connectManual(port, baudRate)
return <BoardContext.Provider value={null}>{children}</BoardContext.Provider>
}
78 changes: 46 additions & 32 deletions src/procedure/utils/board.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,62 @@
import { EventEmitter } from 'events'
import { SerialPort } from 'serialport'
import ora from 'ora'
import { delay } from './delay'

const boardEmitter = new EventEmitter()
let currentPort: SerialPort | null = null
let isPortActive = false

const MAX_RECENT_LISTENERS = 6

const connectManual = (arduinoPath: string) => {
const confirmConnection = async (port: SerialPort) => {
const endTimeout = Date.now() + 10000

while (Date.now() < endTimeout) {
port.write(Buffer.from([0xf0, 0x79, 0xf7]))
if (isPortActive) {
break
}
await delay(100)
}
}

const connectManual = async (path: string, baudRate: number) => {
const spinner = ora('Now Connecting to Device...').start()

if (currentPort) {
spinner.fail('Port is already used.')
process.exit(1)
}
const timeoutId = setTimeout(() => {
if (!isPortActive) {
spinner.fail('Failed to connect to device within 10 seconds.')
process.exit(1)
}
}, 10000)

try {
const port = new SerialPort(
{ path: arduinoPath, baudRate: 57600 },
(error) => {
if (error) {
spinner.fail(
'Failed to open port\n ' +
error +
`.\n ---------------------------------------------------------\nWSL: If you are using WSL, run the command
const port = new SerialPort({ path, baudRate }, (error) => {
if (error) {
spinner.fail(
'Failed to open port\n ' +
error +
`.\n ---------------------------------------------------------\nWSL: If you are using WSL, run the command
\`\`\`sh
ls/dev/tty*
\`\`\`
----------------------------------------------------------
to see if the path you are passing to the "port" property of the <Board> component exists.\n
Windows: the "port" is the COMx uploaded to microconputar.`,
)
currentPort = null
process.exit(1)
}
},
)
)
currentPort = null
process.exit(1)
}
})

currentPort = port

const onData = (/*data*/) => {
const onData = () => {
if (!isPortActive) {
spinner.succeed('Device is connected successfully!')
boardEmitter.emit('ready', port)
isPortActive = true
}

const allListeners = port.listeners('data') as ((...args: []) => void)[]
const oldListeners = allListeners.slice(0, -MAX_RECENT_LISTENERS)

Expand All @@ -55,25 +66,28 @@ Windows: the "port" is the COMx uploaded to microconputar.`,
port.removeListener('data', listener)
}
})

if (!isPortActive) {
clearTimeout(timeoutId)
spinner.succeed('Device is connected successfully!')
boardEmitter.emit('ready', port)
isPortActive = true
}
}
port.on('data', onData)

port.on('close', () => {
spinner.fail('Board is closed.')
spinner.fail('Port is closed.')
currentPort = null
port.removeAllListeners()
isPortActive = false
process.exit(1)
})
port.on('data', onData)

port.on('error', (error) => {
console.error('Serial port error:', error)
})

await confirmConnection(port)

if (!isPortActive) {
spinner.fail('Failed to connect to device within 10 seconds.')
process.exit(1)
}
} catch (error) {
clearTimeout(timeoutId)
spinner.fail('Failed to open port: ' + error)
currentPort = null
process.exit(1)
Expand Down

0 comments on commit 8b658a8

Please sign in to comment.