Skip to content

Commit

Permalink
Merge pull request #68 from AllenShintani/edison-feature-56
Browse files Browse the repository at this point in the history
Declarative ui is developed
  • Loading branch information
AllenShintani authored Feb 9, 2024
2 parents d65251b + 14270e9 commit 1629a4a
Show file tree
Hide file tree
Showing 81 changed files with 163 additions and 435 deletions.
25 changes: 25 additions & 0 deletions edison-cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// // edison.js
// const { renderToString } = require('react-dom/server');
// const path = require('path');
// const { createRequire } = require('module');

// const run = async (filePath) => {
// const require = createRequire(import.meta.url);
// const absPath = path.resolve(filePath);
// const { App } = require(absPath);

// try {
// const content = renderToString(<App />);
// console.log(content);
// } catch (error) {
// console.error('Failed to render the app:', error);
// }
// };

// const filePath = process.argv[2];
// if (!filePath) {
// console.error('Please provide a file path to run');
// process.exit(1);
// }

// run(filePath);
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"dependencies": {
"@types/microtime": "^2.1.2",
"@types/react": "^18.2.31",
"@types/react-dom": "^18.2.14",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rxjs": "^7.8.1",
Expand All @@ -12,6 +10,8 @@
},
"devDependencies": {
"@types/node": "^20.8.8",
"@types/react": "^18.2.52",
"@types/react-dom": "^18.2.18",
"@types/serialport": "^8.0.3",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"dotenv-cli": "^7.3.0",
Expand All @@ -26,7 +26,7 @@
},
"name": "edison",
"type": "module",
"version": "0.1.32",
"version": "0.1.33",
"exports": {
".": {
"import": "./dist/esm/index.mjs",
Expand Down
15 changes: 15 additions & 0 deletions src/declarative/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import { Board } from './utils/Board'
import { Led } from './factory/output/uniqueDevice/Led'
import { renderToString } from 'react-dom/server'

const App: React.FC = () => {
return (
<Board port={'/dev/ttyUSB0'}>
<Led pin={13} />
</Board>
)
}

renderToString(<App />)
export { App }
26 changes: 26 additions & 0 deletions src/declarative/factory/output/uniqueDevice/Buzzer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type React from 'react'
import { attachBuzzer } from '../../../../procedure/factory/output/uniqueDevice/buzzer'
import { board } from '../../../../procedure/utils/board'
import type { SerialPort } from 'serialport'

type LEDProps = {
pin: number
isOn?: boolean
blink?: number
}

export const Buzzer: React.FC<LEDProps> = ({ pin, isOn }) => {
board.on('ready', (port: SerialPort) => {
const buzzer = attachBuzzer(port, pin)

if (isOn === true) {
buzzer.on()
}

if (isOn === false) {
buzzer.off()
}
})

return null
}
30 changes: 30 additions & 0 deletions src/declarative/factory/output/uniqueDevice/Led.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type React from 'react'
import { attachLed } from '../../../../procedure/factory/output/uniqueDevice/led'
import { board } from '../../../../procedure/utils/board'
import type { SerialPort } from 'serialport'

type LEDProps = {
pin: number
isOn?: boolean
blink?: number
}

export const Led: React.FC<LEDProps> = ({ pin, isOn, blink }) => {
board.on('ready', (port: SerialPort) => {
const led = attachLed(port, pin)

if (isOn === true) {
led.on()
}

if (isOn === false) {
led.off()
}

if (blink) {
led.blink(blink)
}
})

return null
}
15 changes: 15 additions & 0 deletions src/declarative/utils/Board.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React, { createContext } from 'react'
import type { SerialPort } from 'serialport'
import { board } from '../../procedure/utils/board'

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

type BoardProps = {
children: React.ReactNode
port: string
}

export const Board: React.FC<BoardProps> = ({ children, port }) => {
board.connectManual(port)
return <BoardContext.Provider value={null}>{children}</BoardContext.Provider>
}
25 changes: 14 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
export { board } from './utils/board'
export { attachLed } from './factory/output/uniqueDevice/led'
export { attachBuzzer } from './factory/output/uniqueDevice/buzzer'
export { attachPressureSensor } from './factory/analog/uniqueDevice/pressureSensor'
export { attachServo } from './factory/servo/uniqueDevice/servo'
export { board } from './procedure/utils/board'
export { attachLed } from './procedure/factory/output/uniqueDevice/led'
export { attachBuzzer } from './procedure/factory/output/uniqueDevice/buzzer'
export { attachPressureSensor } from './procedure/factory/analog/uniqueDevice/pressureSensor'
export { attachServo } from './procedure/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 { attachHallEffectSensor } from './factory/input/uniqueDevice/hallEffectSensor'
export { attachButton } from './factory/input/uniqueDevice/pushButton'
export { attachRgbLed } from './procedure/factory/output/uniqueDevice/rgbLed'
export { attachCollisionSensor } from './procedure/factory/input/uniqueDevice/collisionSensor'
export { attachHallEffectSensor } from './procedure/factory/input/uniqueDevice/hallEffectSensor'
export { attachButton } from './procedure/factory/input/uniqueDevice/pushButton'
//export { attachLineTracking } from './factory/input/uniqueDevice/lineTracking'
export { attachOutput } from './factory/output/uniqueDevice/output'
export { attachOutput } from './procedure/factory/output/uniqueDevice/output'
//export { attachInfraredObstacleAvoidanceSensor } from './factory/input/uniqueDevice/infraredObstacleAvoidanceSensor'
export { delay } from './utils/delay'
export { delay } from './procedure/utils/delay'
export { Board } from './declarative/utils/Board'
export { Led } from './declarative/factory/output/uniqueDevice/Led'
export { App } from './declarative/App'
export { SerialPort } from 'serialport'
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { SerialPort } from 'serialport'
import { setAnalogState } from '../../uniqueDevice/setAnalogState'
import { AnalogPin, Sensor, analogPinMapping } from '../../types/analog/analog'
import { Subscription } from 'rxjs'
import type { AnalogPin, Sensor } from '../../types/analog/analog'
import { analogPinMapping } from '../../types/analog/analog'
import type { Subscription } from 'rxjs'

export const analogPort = (port: SerialPort) => {
return (analogPin: AnalogPin) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { SerialPort } from 'serialport'
import { AnalogPin, Sensor } from '../../../types/analog/analog'
import type { AnalogPin, Sensor } from '../../../types/analog/analog'
import { analogPort } from '../analogPort'

export const attachPressureSensor = (port: SerialPort, pin: AnalogPin) => {
Expand All @@ -19,11 +19,11 @@ export const attachPressureSensor = (port: SerialPort, pin: AnalogPin) => {
}
})
},
onOver: async (
method: Sensor,
func: () => Promise<void> | Promise<number> | void | number,
): Promise<void> => {
return
},
// onOver: async (
// method: Sensor,
// func: () => Promise<void> | Promise<number> | void | number,
// ): Promise<void> => {
// return
// },
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { outputPort } from '../output/outputPort'
import { inputPort } from '../input/inputPort'
import { delay } from '../../index'
import type { SerialPort } from '../../index'
import { delay } from '../../../index'
import type { SerialPort } from '../../../index'
import type { Sensor } from '../../types/analog/analog'

export const attachUltrasonicSensor = (
Expand All @@ -17,18 +17,16 @@ export const attachUltrasonicSensor = (
method: Sensor,
func: () => Promise<void> | Promise<number> | void | number,
): Promise<void> => {
// echo.readを一度だけ呼び出し
await echo.read(method, async () => {
await func()
})

// 無限ループでTrigのon/offのみを繰り返す
// eslint-disable-next-line no-constant-condition
while (true) {
//console.log('Trig')
await trig.on() // TrigピンをHIGHにする
await trig.on()
await delay(20)
await trig.off()
await delay(20)
await trig.off() // TrigピンをLOWにする
await delay(20) // 次の測定までの待機時間
}
},
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { inputPort } from '../inputPort'
import type { SerialPort } from '../../../index'
import type { SerialPort } from '../../../../index'
import type { Sensor } from '../../../types/analog/analog'

export const attachCollisionSensor = (port: SerialPort, pin: number) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { inputPort } from '../inputPort'
import type { SerialPort } from 'serialport'

// export const attachFlameSensor = (port: SerialPort, pin: number) => {
// const flameSensor = inputPort(port)(pin)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { inputPort } from '../inputPort'
import type { SerialPort } from 'serialport'
import { Sensor } from '../../../types/analog/analog'
import type { Sensor } from '../../../types/analog/analog'

export const attachHallEffectSensor = (port: SerialPort, pin: number) => {
const collisionSensor = inputPort(port)(pin)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { inputPort } from '../inputPort'
import type { SerialPort } from 'serialport'

// export const attachInfraredObstacleAvoidanceSensor = (
// port: SerialPort,
// pin: number,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { inputPort } from '../inputPort'
import type { SerialPort } from 'serialport'

// export const attachKnockSensor = (port: SerialPort, pin: number) => {
// const knockSensor = inputPort(port)(pin)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { inputPort } from '../inputPort'
import type { SerialPort } from 'serialport'

// export const attachLineTracking = (port: SerialPort, pin: number) => {
// const buzzer = inputPort(port)(pin)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { inputPort } from '../inputPort'
import type { SerialPort } from 'serialport'

// export const attachPhotoInterrupter = (port: SerialPort, pin: number) => {
// const photoInterrupter = inputPort(port)(pin)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { inputPort } from '../inputPort'
import type { SerialPort } from 'serialport'

// export const attachPirMorionSensor = (port: SerialPort, pin: number) => {
// const PirmotionSensor = inputPort(port)(pin)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { inputPort } from '../inputPort'
import type { SerialPort } from 'serialport'

// export const attachReadSensor = (port: SerialPort, pin: number) => {
// const readSensor = inputPort(port)(pin)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { inputPort } from '../inputPort'
import type { SerialPort } from 'serialport'

// export const attachTiltSensor = (port: SerialPort, pin: number) => {
// const tiltSensor = inputPort(port)(pin)

Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const attachLed = (port: SerialPort, pin: number) => {

return {
blink: async (duration: number) => {
// eslint-disable-next-line no-constant-condition
while (true) {
await led.on()
await delay(duration)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ export const servoPort = (port: SerialPort) => {
return (pin: number) => {
return {
rotate: async (angle: number) => {
////console.log(4)
await setPinToServo(pin, port)
await setServoAngle(pin, angle, port)

////console.log(`Rotating to ${angle} degrees`);
//console.log(`Rotating to ${angle} degrees`);
const DELAY_TIME = 180 + Math.abs(angle - 90) * 2.5
await delay(DELAY_TIME)
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export const attachServo = (port: SerialPort, pin: number) => {

return {
rotate: async (angle: number) => {
const duration = 290 + Math.abs(angle - 90) * 2.5
await servo.rotate(angle)
},
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { SerialPort } from 'serialport'

export const setPinAnalog = (pin: number, port: SerialPort): Promise<void> => {
const SET_PIN_MODE = 0xf4
const REPORT_ANALOG = 0xc0 // Enable analog reporting for pin
const ANALOG_MESSAGE = 0xe0 // Analog message command
// const REPORT_ANALOG = 0xc0 // Enable analog reporting for pin
// const ANALOG_MESSAGE = 0xe0 // Analog message command
return new Promise((resolve, reject) => {
const setPinModeOutput = Buffer.from([SET_PIN_MODE, pin, 2])
port.write(setPinModeOutput, (err) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ export const setInputState = (
if (data[0] === 0x91 && pin < 8) return

const buffer = data.length <= 3 ? data : data.subarray(0, 3)
if (buffer[1] === 0x01) {
observer.next(true)
return
}
const currentState = !!(buffer[1] & (1 << pin % 8))

console.log('buffer:', buffer)
if (currentState !== lastState) {
lastState = currentState
observer.next(currentState)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const setOutputState = async (
const bufferValue = 1 << (pin & 0x07)
const buffer = Buffer.from([IOMESSAGE + (pin >> 3), bufferValue, 0x00])
await bufferWrite(port, buffer)
//console.log('on')
return
}

Expand All @@ -24,14 +23,14 @@ export const setOutputState = async (
const bufferValue = 1 << 0x00
const buffer = Buffer.from([IOMESSAGE + (pin >> 3), bufferValue, 0x00])
await bufferWrite(port, buffer)
//console.log('off')
return
}

if (onoff) {
on()
await delay(20)
} else {
}
if (!onoff) {
off()
await delay(20)
}
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export const setPinToServo = (pin: number, port: SerialPort): Promise<void> => {
]
port.write(Buffer.from(data))
////console.log('setpintoservo,',data)
return
return Promise.resolve()
}
Loading

0 comments on commit 1629a4a

Please sign in to comment.