Piezo buzzer, portlist and update documentation

This commit is contained in:
2024-03-03 18:47:55 +07:00
parent 71c4ec5072
commit f90387bab9
11 changed files with 259 additions and 19 deletions

28
src/controller/piezo.ts Normal file
View File

@@ -0,0 +1,28 @@
import { Request, Response } from "express";
import { Piezo } from "johnny-five";
import { Pitch } from "../melodies";
export function piezoTone (req: Request, res: 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({
song: note.toUpperCase(),
beats: 1/2,
tempo: 100
})
return res.status(200).json({
status: 200,
message: `Piezo ${pin} tone ${note}`
});
}

View File

@@ -1,5 +1,5 @@
import http from 'node:http';
import express, { Request, Response } from 'express';
import express from 'express';
import { Server } from 'socket.io';
import chalk from 'chalk';
@@ -8,6 +8,7 @@ import { board, suBoard, comport } from './setup';
import { isBoardConnected } from './middleware/connection';
import view from './routes/view';
import api from './routes/api';
import { selectPort } from './ports';
const app = express();
const server = http.createServer(app);
@@ -22,10 +23,11 @@ app.use(express.static('client'));
app.use('/', view);
app.use('/api-arduino', isBoardConnected, api);
console.log("\nRunning Server...");
app.listen(port, host, () => {
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(`* Press ${chalk.bold(chalk.yellow("CTRL+C"))} to exit`);
console.log(`* URL: ${chalk.bold(`http://${host}:${port}/\n`)}`);
console.log(chalk.yellow(`Connecting to Board`));
board.on('ready', () => {

91
src/melodies/index.ts Normal file
View File

@@ -0,0 +1,91 @@
export const Pitch = {
B0: 31,
C1: 33,
CS1: 35,
D1: 37,
DS1: 39,
E1: 41,
F1: 44,
FS1: 46,
G1: 49,
GS1: 52,
A1: 55,
AS1: 58,
B1: 62,
C2: 65,
CS2: 69,
D2: 73,
DS2: 78,
E2: 82,
F2: 87,
FS2: 93,
G2: 98,
GS2: 104,
A2: 110,
AS2: 117,
B2: 123,
C3: 131,
CS3: 139,
D3: 147,
DS3: 156,
E3: 165,
F3: 175,
FS3: 185,
G3: 196,
GS3: 208,
A3: 220,
AS3: 233,
B3: 247,
C4: 262,
CS4: 277,
D4: 294,
DS4: 311,
E4: 330,
F4: 349,
FS4: 370,
G4: 392,
GS4: 415,
A4: 440,
AS4: 466,
B4: 494,
C5: 523,
CS5: 554,
D5: 587,
DS5: 622,
E5: 659,
F5: 698,
FS5: 740,
G5: 784,
GS5: 831,
A5: 880,
AS5: 932,
B5: 988,
C6: 1047,
CS6: 1109,
D6: 1175,
DS6: 1245,
E6: 1319,
F6: 1397,
FS6: 1480,
G6: 1568,
GS6: 1661,
A6: 1760,
AS6: 1865,
B6: 1976,
C7: 2093,
CS7: 2217,
D7: 2349,
DS7: 2489,
E7: 2637,
F7: 2794,
FS7: 2960,
G7: 3136,
GS7: 3322,
A7: 3520,
AS7: 3729,
B7: 3951,
C8: 4186,
CS8: 4435,
D8: 4699,
DS8: 4978
}

29
src/ports/index.ts Normal file
View File

@@ -0,0 +1,29 @@
import readline from 'node:readline/promises';
import { ReadPorts } from "./read";
import { board, suBoard } from '../setup';
export async function selectPort (cb: () => void) {
const ports = await ReadPorts();
const line =readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log(`Select number of ports (0-${ports.length})`);
ports.forEach((port, i) => {
console.log(`${i+1}. ${port}`);
})
const selectedInput: string = await line.question("Enter number: ");
const selectedNumber: number = Number.parseInt(selectedInput);
const selectedPort: string = ports[selectedNumber-1];
suBoard.port = selectedPort;
board.port = selectedPort;
console.log(`Selected port: ${selectedPort}`);
line.close();
cb();
}

8
src/ports/read.ts Normal file
View File

@@ -0,0 +1,8 @@
import * as SerialPort from 'serialport';
export async function ReadPorts (): Promise<string[]> {
const ports = await SerialPort.SerialPort.list();
return ports
.filter(port => port.pnpId != null)
.map(port => port.path)
}

View File

@@ -1,12 +1,14 @@
import { Response } from "express";
import { Router } from "express";
import { readLed, readRgbLed, writeLed, writeRgbLed } from "../controller/led";
import { readPin, setPin } from "../controller/pin";
import { piezoTone } from "../controller/piezo";
const router = Router();
const router: Router = Router();
router.get('/hello', (req, res: Response) => {
res.status(200).send("Hello");
router.get('/hello', (req, res: Response): Response<string> => {
return res.status(200).send("Hello");
})
router.get('/pin/:p', readPin);
@@ -18,4 +20,6 @@ router.patch('/led/:p/:a', writeLed);
router.get('/rgb-led', readRgbLed);
router.patch('/rgb-led/', writeRgbLed);
router.patch('/piezo/:p/:n', piezoTone);
export default router;

View File

@@ -1,11 +1,14 @@
import { SuBoard } from './support';
import { Board } from 'johnny-five';
import { config } from 'dotenv';
export const comport: string = '/dev/ttyUSB0';
export const board: Board = new Board({
config();
export const comport: string = process.env.SERIAL_PORT || '/dev/ttyUSB0';
export const suBoard: SuBoard = new SuBoard();
export const board: Board | null = new Board({
port: comport,
repl: false,
debug: false
});
export const suBoard: SuBoard = new SuBoard();
});

View File

@@ -1,4 +1,5 @@
export interface SupportBoard {
port: string | null,
connected: boolean,
PINS: {
pwm: number[],
@@ -8,6 +9,7 @@ export interface SupportBoard {
}
export class SuBoard implements SupportBoard {
public port = null;
public connected = false;
public PINS = {
pwm: [],