Piezo music, setup socket events
This commit is contained in:
parent
f90387bab9
commit
f897b3bf50
@ -1,7 +1,6 @@
|
||||
import { Request, Response } from "express";
|
||||
import { board } from "../setup";
|
||||
import { ChannelPins, digitalValue, voltage } from ".";
|
||||
import { analogRead, digitalRead } from "../promises";
|
||||
import { Led } from "johnny-five";
|
||||
|
||||
|
||||
@ -9,13 +8,6 @@ export async function readLed (req: Request, res: Response) {
|
||||
const { p } = req.params;
|
||||
const pin: number = Number.parseInt(p);
|
||||
|
||||
if (Number.isNaN(pin)) {
|
||||
return res.status(400).json({
|
||||
status: 400,
|
||||
message: 'Invalid pin param, it should be integer'
|
||||
});
|
||||
}
|
||||
|
||||
const pinState: digitalValue = board.pins[pin].value == 1 ? 'ON' : 'OFF';
|
||||
|
||||
return res.status(200).json({
|
||||
@ -35,13 +27,6 @@ export async function writeLed (req: Request, res: Response) {
|
||||
let volt: voltage;
|
||||
|
||||
try {
|
||||
if (Number.isNaN(pin)) {
|
||||
return res.status(400).json({
|
||||
status: 400,
|
||||
message: 'Invalid pin param, it should be integer'
|
||||
});
|
||||
}
|
||||
|
||||
switch (act) {
|
||||
case 'on':
|
||||
state = 'ON';
|
||||
@ -126,7 +111,7 @@ export async function writeRgbLed (req: Request, res: Response) {
|
||||
const rgbLeds: ChannelPins[] = Object.values({ r, g, b });
|
||||
|
||||
try {
|
||||
rgbLeds.forEach((led, i) => {
|
||||
rgbLeds.forEach(led => {
|
||||
if (Number.isNaN(led.pin)) {
|
||||
return res.status(400).json({
|
||||
status: 400,
|
||||
|
@ -2,17 +2,10 @@ import { Request, Response } from "express";
|
||||
import { Piezo } from "johnny-five";
|
||||
import { Pitch } from "../melodies";
|
||||
|
||||
export function piezoTone (req: Request, res: Response) {
|
||||
export function piezoTone (req: Request, res: Response): Response {
|
||||
const pin: number = Number.parseInt(req.params.p);
|
||||
const note: string = req.params.n;
|
||||
|
||||
if (Number.isNaN(pin)) {
|
||||
return res.status(400).json({
|
||||
status: 400,
|
||||
message: 'Invalid pin param, it should be integer'
|
||||
});
|
||||
}
|
||||
|
||||
const piezo = new Piezo(pin);
|
||||
|
||||
piezo.play({
|
||||
@ -26,3 +19,46 @@ export function piezoTone (req: Request, res: Response) {
|
||||
message: `Piezo ${pin} tone ${note}`
|
||||
});
|
||||
}
|
||||
|
||||
export function piezoNoTone (req: Request, res: Response): 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'
|
||||
});
|
||||
}
|
||||
|
||||
new Piezo(pin).noTone();
|
||||
|
||||
return res.sendStatus(200);
|
||||
}
|
||||
|
||||
interface MusicSheet {
|
||||
notes: string[];
|
||||
beats: number;
|
||||
tempo: 100
|
||||
}
|
||||
|
||||
export function piezoPlayNotes (req: Request, res: Response): Response {
|
||||
const pin: number = Number.parseInt(req.params.p);
|
||||
const { notes, beats, tempo }: MusicSheet = req.body;
|
||||
|
||||
if (Number.isNaN(pin)) {
|
||||
return res.status(400).json({
|
||||
status: 400,
|
||||
message: 'Invalid pin param, it should be integer'
|
||||
});
|
||||
}
|
||||
|
||||
const piezo: Piezo = new Piezo(pin);
|
||||
const song: [frequency: string, duration: number][] = notes.map((note): [frequency: string, duration: number] => { return [note, beats]});
|
||||
const notesS: string = notes.join(", ");
|
||||
|
||||
piezo.play({ song, tempo });
|
||||
|
||||
return res.status(200).json({
|
||||
status: 200,
|
||||
message: `Piezo ${pin} play notes ${notesS}`
|
||||
});
|
||||
}
|
@ -6,13 +6,6 @@ import { Pin } from "johnny-five";
|
||||
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({
|
||||
@ -28,13 +21,6 @@ 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, Pin[mode]);
|
||||
|
||||
suBoard.PINS.pwm.push(pin);
|
||||
|
0
src/controller/servo.ts
Normal file
0
src/controller/servo.ts
Normal file
17
src/handlers/socketHandler.ts
Normal file
17
src/handlers/socketHandler.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { Socket } from "socket.io";
|
||||
import { board } from "../setup";
|
||||
|
||||
export default (socket: Socket) => {
|
||||
console.log(`${socket.id} | ${socket.client.request.headers.host} | Joined`);
|
||||
|
||||
socket.on("control-servo", (pin: number, angle: number) => {
|
||||
board.servoWrite(pin, angle);
|
||||
console.log();
|
||||
})
|
||||
|
||||
socket.on("read-photoresistor", (pin: string) => {
|
||||
board.analogRead(pin, (value) => {
|
||||
socket.emit("read-photoresistor", value);
|
||||
});
|
||||
})
|
||||
}
|
@ -9,6 +9,7 @@ import { isBoardConnected } from './middleware/connection';
|
||||
import view from './routes/view';
|
||||
import api from './routes/api';
|
||||
import { selectPort } from './ports';
|
||||
import socketHandler from './handlers/socketHandler';
|
||||
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
@ -20,6 +21,8 @@ const port = 3000;
|
||||
app.use(express.json());
|
||||
app.use(express.static('client'));
|
||||
|
||||
io.on('connection', socketHandler);
|
||||
|
||||
app.use('/', view);
|
||||
app.use('/api-arduino', isBoardConnected, api);
|
||||
|
||||
|
@ -1,6 +1,19 @@
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { suBoard } from "../setup";
|
||||
|
||||
export function isPinNumeric (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'
|
||||
});
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
export function isPinBeingUsed (req: Request, res: Response, next: NextFunction) {
|
||||
const pin: number = Number.parseInt(req.params.p);
|
||||
|
||||
|
@ -3,7 +3,8 @@ import { Router } from "express";
|
||||
|
||||
import { readLed, readRgbLed, writeLed, writeRgbLed } from "../controller/led";
|
||||
import { readPin, setPin } from "../controller/pin";
|
||||
import { piezoTone } from "../controller/piezo";
|
||||
import { piezoNoTone, piezoPlayNotes, piezoTone } from "../controller/piezo";
|
||||
import { isPinNumeric } from "../middleware/pin";
|
||||
|
||||
const router: Router = Router();
|
||||
|
||||
@ -11,15 +12,17 @@ router.get('/hello', (req, res: Response): Response<string> => {
|
||||
return res.status(200).send("Hello");
|
||||
})
|
||||
|
||||
router.get('/pin/:p', readPin);
|
||||
router.patch('/pin/:p/:m', setPin);
|
||||
router.get('/pin/:p', isPinNumeric, readPin);
|
||||
router.patch('/pin/:p/:m', isPinNumeric, setPin);
|
||||
|
||||
router.get('/led/:p', readLed);
|
||||
router.patch('/led/:p/:a', writeLed);
|
||||
router.get('/led/:p', isPinNumeric, readLed);
|
||||
router.patch('/led/:p/:a', isPinNumeric, writeLed);
|
||||
|
||||
router.get('/rgb-led', readRgbLed);
|
||||
router.patch('/rgb-led/', writeRgbLed);
|
||||
|
||||
router.patch('/piezo/:p/:n', piezoTone);
|
||||
router.patch('/piezo/:p/:n', isPinNumeric, piezoTone);
|
||||
router.patch('/piezo/stop/:p/', isPinNumeric, piezoNoTone);
|
||||
router.patch('/piezo/music/:p', isPinNumeric, piezoPlayNotes);
|
||||
|
||||
export default router;
|
Loading…
x
Reference in New Issue
Block a user