Skip to content

Commit

Permalink
Merge pull request #79 from AllenShintani/feature-edison/76
Browse files Browse the repository at this point in the history
add terminal option
  • Loading branch information
AllenShintani authored Mar 16, 2024
2 parents 4944d6f + 89eb73d commit 209720e
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ module.exports = {
'no-unreachable': ['error'],
//"brace-style": ["error", "stroustrup"],
},
}
}
18 changes: 8 additions & 10 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.formatOnType": true,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"editor.formatOnPaste": true,
"[javascript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"[javascriptreact]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
},
"[typescript]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
},
"[typescriptreact]": {
"editor.defaultFormatter": "dbaeumer.vscode-eslint"
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"auto-bind": "^5.0.1",
"cli-cursor": "^4.0.0",
"code-excerpt": "^4.0.0",
"firmata": "^2.3.0",
"is-in-ci": "^0.1.0",
"lodash": "^4.17.21",
"ora": "^8.0.1",
"rxjs": "^7.8.1",
"serialport": "^12.0.0",
"stack-utils": "^2.0.6",
Expand All @@ -24,6 +24,7 @@
"@types/serialport": "^8.0.3",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@vitejs/plugin-react": "^4.2.1",
"@vitest/coverage-v8": "^1.4.0",
"dotenv-cli": "^7.3.0",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.0.0",
Expand All @@ -37,7 +38,7 @@
"vite": "^5.1.1",
"vite-node": "^1.2.2",
"vite-plugin-checker": "^0.6.2",
"vitest": "^0.34.6"
"vitest": "^1.4.0"
},
"peerDependencies": {
"@types/react": "^18.2.55",
Expand Down Expand Up @@ -67,6 +68,7 @@
"lint": "npx eslint src/**/*.ts* ",
"type-check": "tsc --noEmit",
"test": "vitest",
"test:coverage": "vitest run --coverage",
"format": "prettier --write \"./src/**/*.{ts}\""
},
"repository": {
Expand Down
18 changes: 4 additions & 14 deletions src/declarative/examples/App.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import React from 'react'
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'

const App: React.FC = () => {
const [ledOne, setLedOne] = useState(false)

return (
<Board port={'/dev/ttyUSB0'}>
<Button
pin={12}
onPress={() => setLedOne(true)}
onRelease={() => setLedOne(false)}
>
<Led
pin={13}
isOn={ledOne}
/>
</Button>
<Led
pin={13}
blink={500}
/>
</Board>
)
}
Expand Down
3 changes: 1 addition & 2 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 // 各ピンごとにprevValueを保持
let prevValue: boolean | null = null

return {
read: (
Expand All @@ -26,7 +26,6 @@ export const inputPort = (port: SerialPort) => {
prevValue = value
func()
}
// 'change'の場合は前の値との比較に関係なくfuncを実行
})
},
}
Expand Down
6 changes: 1 addition & 5 deletions src/procedure/helper/Input/setInputState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ export const setInputState = (
pin: number,
port: SerialPort,
): Observable<boolean> => {
// ピンモードをデジタル入力に設定
bufferWrite(port, Buffer.from([SET_PIN_MODE, pin, INPUT_MODE]))
// デジタルポートの状態変化報告を有効にする(ピンが属するポートの計算が必要)
bufferWrite(
port,
Buffer.from([DIGITAL_CHANGE_MESSAGE + Math.floor(pin / 8), 1]),
Expand All @@ -32,19 +30,17 @@ export const setInputState = (
currentBinary: Buffer,
) => {
const pinIndex = pin % 8
const isRelevantPin = (currentBinary[0] & 0x0f) === Math.floor(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 }
}

// 状態が変わった場合のみ emit を true に設定
return {
preBinary: currentBinary,
lastState: currentState,
Expand Down
11 changes: 6 additions & 5 deletions src/procedure/utils/board.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventEmitter } from 'events'
import { SerialPort } from 'serialport'
import ora from 'ora'

const boardEmitter = new EventEmitter()
let currentPort: SerialPort | null = null
Expand All @@ -9,19 +10,18 @@ const MAX_RECENT_LISTENERS = 80

const connectManual = (arduinoPath: string) => {
if (currentPort) {
console.log('Port is already open.')
console.error('Port is already used.')
return
}

const spinner = ora('Now Connecting to Device...').start()
try {
const port = new SerialPort({ path: arduinoPath, baudRate: 57600 })
currentPort = port

const onData = (/*data*/) => {
if (port.listenerCount('data') > MAX_RECENT_LISTENERS) {
const allListeners = port.listeners('data') as ((
...args: unknown[]
) => void)[]
const allListeners = port.listeners('data') as ((...args: []) => void)[]
const oldListeners = allListeners.slice(0, -MAX_RECENT_LISTENERS)

// biome-ignore lint/complexity/noForEach: <explanation>
Expand All @@ -33,7 +33,7 @@ const connectManual = (arduinoPath: string) => {
}

if (!isPortActive) {
console.log('Board is ready.')
spinner.succeed('Device is connected successfully!')
boardEmitter.emit('ready', port)
isPortActive = true
}
Expand All @@ -42,6 +42,7 @@ const connectManual = (arduinoPath: string) => {
port.on('data', onData)

port.on('close', () => {
console.log('Board is closed.')
currentPort = null
port.removeAllListeners()
isPortActive = false
Expand Down
13 changes: 6 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
},
"target": "ES2022",
"module": "ESNext",
"declaration": true,
Expand All @@ -14,11 +18,6 @@
"jsx": "react",
"allowJs": true
},
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"global.d.ts", ],
"exclude": [
"node_modules", "./dist"
]
"include": ["src/**/*.ts", "src/**/*.tsx", "global.d.ts"],
"exclude": ["node_modules", "./dist"]
}
8 changes: 6 additions & 2 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import { resolve } from 'path'

export default defineConfig({
plugins: [react()],
resolve: {
alias: {
'@': resolve(__dirname, './src'),
},
},
})

0 comments on commit 209720e

Please sign in to comment.