Refact json response and fixed piezo tone

This commit is contained in:
2024-03-10 13:56:38 +07:00
parent 76e9ae14e5
commit e064e198a3
6 changed files with 198 additions and 31 deletions

View File

@@ -9,13 +9,14 @@ export function readLed (req: Request, res: Response): Response<string | any> {
const { p } = req.params;
const pin: number = Number.parseInt(p);
const pinState: digitalValue = board.pins[pin].value == 1 ? 'ON' : 'OFF';
const pinState: voltage = board.pins[pin].value == 1 ? 'HIGH' : 'LOW';
return res.status(200).json({
status: 200,
pin_state: {
[pin]: pinState
}
},
message: `Led pin ${pin} is ${pinState}`
});
}
catch (err) {
@@ -32,20 +33,18 @@ export function writeLed (req: Request, res: Response): Response<string | any> {
const act: string = a.toLocaleLowerCase();
const pin: number = Number.parseInt(p);
let state: digitalValue;
let state: voltage;
let volt: voltage;
try {
switch (act) {
case 'on':
state = 'ON';
case 'on' || 'high':
state = 'HIGH';
volt = 1;
console.log(`${req.hostname} | ${pin} | LED: ${state}`);
break;
case 'off':
state = 'OFF';
case 'off' || 'low':
state = 'LOW';
volt = 0;
console.log(`${req.hostname} | ${pin} | LED: ${state}`);
break;
default:
console.log(`${req.hostname} | ${pin} | LED: INVALID ACT`);
@@ -54,12 +53,17 @@ export function writeLed (req: Request, res: Response): Response<string | any> {
message: `Invalid act ${act}`
});
}
board.digitalWrite(pin, volt);
console.log(`${req.hostname} | ${pin} | LED: ${state}`);
res.status(200).json({
status: 200,
message: `Success changed pin ${pin} to state ${state}`
pin_state: {
[pin]: state
},
message: `Changed pin state ${pin} to ${state}`
});
}
catch (err) {
@@ -147,19 +151,26 @@ export function writeRgbLed (req: Request, res: Response): Response<string | any
isAnode: true
})
const isHigh: boolean | string = true || 'HIGH' || 'high';
led.red = new Led(r.pin);
led.green = new Led(g.pin);
led.blue = new Led(b.pin);
if (r.value == true) led.red.on(); else led.red.off();
if (g.value == true) led.green.on(); else led.green.off();
if (b.value == true) led.blue.on(); else led.blue.off();
if (r.value == isHigh) led.red.on(); else led.red.off();
if (g.value == isHigh) led.green.on(); else led.green.off();
if (b.value == isHigh) led.blue.on(); else led.blue.off();
const pins: string = rgbLeds.map(c => c.pin.toString()).join(", ");
const values: string = rgbLeds.map(c => `${c.value}`).join(", ");
return res.status(200).json({
status: 200,
pin_state: {
[r.pin]: led.red.isOn,
[g.pin]: led.green.isOn,
[b.pin]: led.blue.isOn
},
message: `Success changed pins ${pins} to state ${values}`
});
}

View File

@@ -3,7 +3,32 @@ import { Piezo } from "johnny-five";
import { Pitch } from "../melodies";
export function piezoTone (req: Request, res: Response): Response {
const pin: number = Number.parseInt(req.params.p);
const frequency: number = Number.parseInt(req.params.f);
const piezo: Piezo = new Piezo(pin);
piezo.tone(frequency * 7.3, 100);
return res.status(200).json({
status: 200,
pin_tone: {
[pin]: frequency
},
message: `Piezo ${pin} tone ${frequency}`
});
}
export function piezoNote (req: Request, res: Response): Response {
const { pin, note }: { pin: number, note: string } = req.body;
const notePitch = Pitch[note.toUpperCase()];
if (notePitch == null) {
return res.status(400).json({
status: 400,
message: `Invalid note ${note}`
});
}
const piezo: Piezo = new Piezo(pin);
@@ -15,7 +40,13 @@ export function piezoTone (req: Request, res: Response): Response {
return res.status(200).json({
status: 200,
message: `Piezo ${pin} tone ${note}`
pin_tone: {
[pin]: notePitch
},
pin_note: {
[pin]: note.toUpperCase()
},
message: `Piezo ${pin} tone note ${note}`
});
}
@@ -45,12 +76,17 @@ export function piezoPlayNotes (req: Request, res: Response): Response {
})
.filter((note) => note[0].trim() != "-");
const notesS: string = notes.join(", ");
const notesS: string = notes.length > 4 ?
notes.slice(0, 4).join(", ")+"..." :
notes.join(", ");
piezo.play({ song, tempo });
return res.status(200).json({
status: 200,
pin_notes: {
[pin]: notes
},
message: `Piezo ${pin} play notes ${notesS}`
});
}

View File

@@ -18,17 +18,22 @@ const io: Server = new Server(server, {
}
}); // I have no experience at WebSocket, so.. forgive me :)
// Server configuration
const host: string = 'localhost';
const port: number = 3000;
// Express middleware
app.use(express.json());
app.use(express.static('client'));
// Socket.io event handlers
io.on('connection', socketHandler);
// HTTP Routes
app.use('/', view);
app.use('/api-arduino', isBoardConnected, api);
app.use('/api-arduino', isBoardConnected, api); // Board API Controllers
// Run server
console.log("\nRunning Server...");
server.listen(port, () => {
console.log(`Server is connected and running in ${host} at port ${port} 🗣️🗣️🗣️`);

View File

@@ -3,7 +3,7 @@ import { Router } from "express";
import { readLed, readRgbLed, writeLed, writeRgbLed } from "../controller/led";
import { readPin, setPin } from "../controller/pin";
import { piezoNoTone, piezoPlayNotes, piezoTone } from "../controller/piezo";
import { piezoNoTone, piezoNote, piezoPlayNotes, piezoTone } from "../controller/piezo";
import { rotateServo } from "../controller/servo";
import { readResistor } from "../controller/photoresistor";
@@ -11,22 +11,28 @@ import { isPinNumeric } from "../middleware/pin";
const router: Router = Router();
// Client page
router.get('/hello', (req, res: Response): Response<string> => {
return res.status(200).send("Hello");
})
// PinMode
router.get('/pin/:p', readPin);
router.patch('/pin/:p/:m', setPin);
// LED
router.get('/led/:p', isPinNumeric, readLed);
router.patch('/led/:p/:a', isPinNumeric, writeLed);
router.get('/rgb-led', readRgbLed);
// RGB LED
router.post('/rgb-led', readRgbLed);
router.patch('/rgb-led/', writeRgbLed);
router.patch('/piezo/', piezoTone);
router.patch('/piezo/stop/', piezoNoTone);
// Piezo
router.patch('/piezo/:p/:f', isPinNumeric, piezoTone);
router.patch('/piezo/note', piezoNote);
router.patch('/piezo/music/', piezoPlayNotes);
router.patch('/piezo/stop/', piezoNoTone);
// for real-time communication is deprecated and not recommended
// use other protocol like websocket instead