resetup led
This commit is contained in:
15
src/controller/index.d.ts
vendored
Normal file
15
src/controller/index.d.ts
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
export type digitalValue = 'ON' | 'OFF';
|
||||
export type voltage = 'HIGH' | 'LOW';
|
||||
export type sPinModes = 'INPUT' | 'OUTPUT' | 'ANALOG' | 'PWM' | 'SERVO' |'SHIFT' |'I2C' |'ONEWIRE' |'STEPPER' |'SERIAL' |'PULLUP' |'IGNORE' |'PING_READ' | 'UNKOWN';
|
||||
|
||||
export interface ChannelPins {
|
||||
pin: number,
|
||||
value: digitalValue
|
||||
}
|
||||
|
||||
export interface ColorChannel {
|
||||
r: string,
|
||||
g: string,
|
||||
b: string
|
||||
}
|
||||
@@ -1,15 +1,12 @@
|
||||
import { Request, Response } from "express";
|
||||
import { board } from "../setup";
|
||||
import { ChannelPins, digitalValue, voltage } from ".";
|
||||
import { digitalRead } from "../promises";
|
||||
|
||||
export interface ColorChannel {
|
||||
r: string,
|
||||
g: string,
|
||||
b: string
|
||||
}
|
||||
|
||||
export async function readLed (req: Request, res: Response) {
|
||||
const { p } = req.params;
|
||||
const pin = Number.parseInt(p);
|
||||
const pin: number = Number.parseInt(p);
|
||||
|
||||
if (Number.isNaN(pin)) {
|
||||
return res.status(400).json({
|
||||
@@ -18,18 +15,23 @@ export async function readLed (req: Request, res: Response) {
|
||||
});
|
||||
}
|
||||
|
||||
const pinState = board.pins[pin].value == 1 ? 'ON' : 'OFF';
|
||||
const pinState: digitalValue = board.pins[pin].value == 1 ? 'ON' : 'OFF';
|
||||
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
message: `Pin ${pin} state ${pinState}`
|
||||
pin_state: {
|
||||
[pin]: pinState
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function writeLed (req: Request, res: Response) {
|
||||
const { p, a } = req.params;
|
||||
const act = a.toLocaleLowerCase();
|
||||
const pin = Number.parseInt(p);
|
||||
const act: string = a.toLocaleLowerCase();
|
||||
const pin: number = Number.parseInt(p);
|
||||
|
||||
let state: digitalValue;
|
||||
let volt: voltage;
|
||||
|
||||
try {
|
||||
if (Number.isNaN(pin)) {
|
||||
@@ -41,20 +43,108 @@ export async function writeLed (req: Request, res: Response) {
|
||||
|
||||
switch (act) {
|
||||
case 'on':
|
||||
board.digitalWrite(pin, board.HIGH);
|
||||
console.log(`${req.hostname} | ${pin} | LED: ${act.toLocaleUpperCase()}`);
|
||||
state = 'ON';
|
||||
volt = 'HIGH';
|
||||
console.log(`${req.hostname} | ${pin} | LED: ${state}`);
|
||||
break;
|
||||
case 'off':
|
||||
board.digitalWrite(pin, board.LOW);
|
||||
console.log(`${req.hostname} | ${pin} | LED: ${act.toLocaleUpperCase()}`);
|
||||
state = 'OFF';
|
||||
volt = 'LOW';
|
||||
console.log(`${req.hostname} | ${pin} | LED: ${state}`);
|
||||
break;
|
||||
default:
|
||||
console.log(`${req.hostname} | ${pin} | LED: INVALID ACT`);
|
||||
return res.status(400).json({
|
||||
status: 400,
|
||||
message: `Invalid act ${act}`
|
||||
});
|
||||
}
|
||||
|
||||
board.digitalWrite(pin, board[volt]);
|
||||
|
||||
res.status(200).json({
|
||||
status: 200,
|
||||
message: `Success changed pin ${pin} to state ${act}`
|
||||
message: `Success changed pin ${pin} to state ${state}`
|
||||
});
|
||||
}
|
||||
catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).json({
|
||||
status: 500,
|
||||
message: "Internal Server Error"
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
export async function readRgbLed (req: Request, res: Response) {
|
||||
const r: number = Number.parseInt(req.body.r);
|
||||
const g: number = Number.parseInt(req.body.b);
|
||||
const b: number = Number.parseInt(req.body.b);
|
||||
|
||||
const rgbPins: number[] = Object.values({ r, g, b });
|
||||
const pinStates: number[] = [];
|
||||
|
||||
for (let i = 0; i < rgbPins.length; i++) {
|
||||
const pin = rgbPins[i];
|
||||
|
||||
if (Number.isNaN(pin)) {
|
||||
return res.status(400).json({
|
||||
status: 400,
|
||||
message: `Invalid pin ${pin} param, it should be integer`
|
||||
});
|
||||
}
|
||||
|
||||
const state: number = await digitalRead(board, pin);
|
||||
pinStates.push(state);
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
pin_state: {
|
||||
[r]: pinStates[0],
|
||||
[g]: pinStates[1],
|
||||
[b]: pinStates[2]
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function writeRgbLed (req: Request, res: Response) {
|
||||
const r: ChannelPins = req.body.r;
|
||||
const g: ChannelPins = req.body.b;
|
||||
const b: ChannelPins = req.body.b;
|
||||
|
||||
const rgbLeds: ChannelPins[] = Object.values({ r, g, b });
|
||||
|
||||
try {
|
||||
rgbLeds.forEach((led, i) => {
|
||||
if (Number.isNaN(led.pin)) {
|
||||
return res.status(400).json({
|
||||
status: 400,
|
||||
message: `Invalid pin ${led.pin} param, it should be integer`
|
||||
});
|
||||
}
|
||||
|
||||
switch (led.value) {
|
||||
case 'ON':
|
||||
board.analogWrite(led.pin, board.HIGH);
|
||||
console.log(`${req.hostname} | ${led.pin} | LED: ${led.value}`);
|
||||
break;
|
||||
case 'OFF':
|
||||
board.analogWrite(led.pin, board.LOW);
|
||||
console.log(`${req.hostname} | ${led.pin} | LED: ${led.value}`);
|
||||
break;
|
||||
default:
|
||||
console.log(`${req.hostname} | ${led.pin} | LED: INVALID VALUE`);
|
||||
}
|
||||
})
|
||||
|
||||
const pins: string[] = rgbLeds.map(c => `${c.pin}, `);
|
||||
const values: string[] = rgbLeds.map(c => `${c.value}, `);
|
||||
|
||||
res.status(200).json({
|
||||
status: 200,
|
||||
message: `Success changed pins ${pins} to state ${values}`
|
||||
});
|
||||
}
|
||||
catch (err) {
|
||||
|
||||
46
src/controller/pin.ts
Normal file
46
src/controller/pin.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Request, Response } from "express";
|
||||
import { board, suBoard } from "../setup";
|
||||
import { sPinModes } from ".";
|
||||
|
||||
export function readPin (req: Request, res: Response) {
|
||||
const pin: number = Number.parseInt(req.params.p);
|
||||
|
||||
if (Number.isNaN(pin)) {
|
||||
return res.status(400).json({
|
||||
status: 400,
|
||||
message: 'Invalid pin param, it should be integer'
|
||||
});
|
||||
}
|
||||
|
||||
const { mode } = board.pins[pin];
|
||||
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
state: {
|
||||
used: true
|
||||
},
|
||||
message: `Pin ${pin} is ${mode}`
|
||||
});
|
||||
}
|
||||
|
||||
export function setPin (req: Request, res: Response) {
|
||||
const pin: number = Number.parseInt(req.params.p);
|
||||
const mode: sPinModes | string = req.params.m.toUpperCase();
|
||||
|
||||
if (Number.isNaN(pin)) {
|
||||
return res.status(400).json({
|
||||
status: 400,
|
||||
message: 'Invalid pin param, it should be integer'
|
||||
});
|
||||
}
|
||||
|
||||
board.pinMode(pin, board.MODES[mode]);
|
||||
|
||||
suBoard.PINS.pwm.push(pin);
|
||||
suBoard.sort();
|
||||
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
message: `Pin ${pin} setted as ${mode}`
|
||||
});
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import chalk from 'chalk';
|
||||
|
||||
import { board, suBoard, comport } from './setup';
|
||||
|
||||
import { isBoardConnected } from './middleware/connection';
|
||||
import view from './routes/view';
|
||||
import api from './routes/api';
|
||||
|
||||
@@ -19,10 +20,11 @@ app.use(express.json());
|
||||
app.use(express.static('client'));
|
||||
|
||||
app.use('/', view);
|
||||
app.use('/api-arduino', api);
|
||||
app.use('/api-arduino', isBoardConnected, api);
|
||||
|
||||
app.listen(port, host, () => {
|
||||
console.log(`Server is running in ${host} at port ${port} 🗣️🗣️🗣️`);
|
||||
console.log(`Server is connected and running in ${host} at port ${port} 🗣️🗣️🗣️`);
|
||||
console.log(`Press CTRL+C to exit`);
|
||||
console.log(`URL: http://${host}:${port}/\n`);
|
||||
console.log(chalk.yellow(`Connecting to Board`));
|
||||
|
||||
|
||||
22
src/middleware/pin.ts
Normal file
22
src/middleware/pin.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { suBoard } from "../setup";
|
||||
|
||||
export function isPinBeingUsed(req: Request, res: Response, next: NextFunction) {
|
||||
const pin: number = Number.parseInt(req.params.p);
|
||||
|
||||
if (Number.isNaN(pin)) {
|
||||
return res.status(400).json({
|
||||
status: 400,
|
||||
message: 'Invalid pin param, it should be integer'
|
||||
});
|
||||
}
|
||||
|
||||
if (suBoard.PINS.pwm.indexOf(pin) != -1) {
|
||||
return res.status(400).json({
|
||||
status: 400,
|
||||
message: `Pin ${pin}, is used`
|
||||
});
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
function digitalRead (board, pin) {
|
||||
return new Promise((resolve, reject) => {
|
||||
board.digitalRead(pin, (val) => {
|
||||
resolve(val);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { digitalRead };
|
||||
23
src/promises/index.ts
Normal file
23
src/promises/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import Board, { PIN_MODE } from "firmata";
|
||||
|
||||
export function digitalRead (board: Board, pin: number): Promise<PIN_MODE | number> {
|
||||
return new Promise((resolve, reject) => {
|
||||
board.digitalRead(pin, (val) => {
|
||||
resolve(val);
|
||||
});
|
||||
setTimeout(() => {
|
||||
reject(new Error("Reading digital timeout"));
|
||||
}, 5000);
|
||||
});
|
||||
}
|
||||
|
||||
export function analogRead (board: Board, pin: number): Promise<number> {
|
||||
return new Promise((resolve, reject) => {
|
||||
board.analogRead(pin, (val) => {
|
||||
resolve(val);
|
||||
})
|
||||
setTimeout(() => {
|
||||
reject(new Error("Reading analog timeout"));
|
||||
}, 5000);
|
||||
})
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Response } from "express";
|
||||
import { Router } from "express";
|
||||
import { readLed, writeLed } from "../controller/led";
|
||||
import { isBoardConnected } from "../middleware/connection";
|
||||
import { readLed, readRgbLed, writeLed, writeRgbLed } from "../controller/led";
|
||||
import { readPin, setPin } from "../controller/pin";
|
||||
|
||||
const router = Router();
|
||||
|
||||
@@ -9,7 +9,13 @@ router.get('/hello', (req, res: Response) => {
|
||||
res.status(200).send("Hello");
|
||||
})
|
||||
|
||||
router.get('/led/:p', isBoardConnected, readLed);
|
||||
router.patch('/led/:p/:a', isBoardConnected, writeLed);
|
||||
router.get('/pin/:p', readPin);
|
||||
router.patch('/pin/:p/:m', setPin);
|
||||
|
||||
router.get('/led/:p', readLed);
|
||||
router.patch('/led/:p/:a', writeLed);
|
||||
|
||||
router.get('/rgb-led', readRgbLed);
|
||||
router.patch('/rgb-led/', writeRgbLed);
|
||||
|
||||
export default router;
|
||||
@@ -1,16 +1,7 @@
|
||||
import Firmata from 'firmata';
|
||||
import { SuBoard } from './support';
|
||||
|
||||
export const comport: string = '/dev/ttyUSB0';
|
||||
export const comport: string = '/dev/ttyUSB1';
|
||||
export const board: Firmata = new Firmata(comport);
|
||||
|
||||
export const suBoard = {
|
||||
connected: false,
|
||||
PIN: {
|
||||
servo: 0,
|
||||
rgb_led: {
|
||||
r: 10,
|
||||
g: 9,
|
||||
b: 8
|
||||
}
|
||||
}
|
||||
}
|
||||
export const suBoard: SuBoard = new SuBoard();
|
||||
20
src/setup/support.ts
Normal file
20
src/setup/support.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
export interface SupportBoard {
|
||||
connected: boolean,
|
||||
PINS: {
|
||||
pwm: number[],
|
||||
analog: number[],
|
||||
}
|
||||
sort: () => void
|
||||
}
|
||||
|
||||
export class SuBoard implements SupportBoard {
|
||||
public connected = false;
|
||||
public PINS = {
|
||||
pwm: [],
|
||||
analog: []
|
||||
};
|
||||
public sort() {
|
||||
this.PINS.pwm = this.PINS.pwm.sort((a, b) => b - a);
|
||||
this.PINS.analog = this.PINS.analog.sort((a, b) => b - a);
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
const Firmata = require('firmata');
|
||||
const board = new Firmata('/dev/ttyUSB0');
|
||||
|
||||
board.on('ready', async () => {
|
||||
const led = 12;
|
||||
|
||||
board.pinMode(led, board.MODES.OUTPUT);
|
||||
|
||||
while (true) {
|
||||
board.digitalWrite(led, board.HIGH);
|
||||
console.log("ON");
|
||||
await new Promise(resolve => setTimeout(resolve, 300));
|
||||
board.digitalWrite(led, board.LOW);
|
||||
console.log("OFF");
|
||||
await new Promise(resolve => setTimeout(resolve, 300));
|
||||
}
|
||||
});
|
||||
@@ -1,20 +0,0 @@
|
||||
const Firmata = require('firmata');
|
||||
const board = new Firmata('/dev/ttyUSB0');
|
||||
|
||||
board.on('ready', async () => {
|
||||
const led = 12;
|
||||
|
||||
board.pinMode(led, board.MODES.OUTPUT);
|
||||
|
||||
let state = board.LOW;
|
||||
|
||||
board.digitalWrite(led, board.HIGH);
|
||||
|
||||
state = board.pins[led].value;
|
||||
|
||||
while (true) {
|
||||
console.log(state);
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user