Skip to content

Commit

Permalink
separating original repo
Browse files Browse the repository at this point in the history
  • Loading branch information
sminliwu committed Aug 28, 2022
1 parent a257f11 commit c8581b9
Show file tree
Hide file tree
Showing 14 changed files with 1,535 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 0_main/PiFakePedals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { LoomHandle } = require('../2_loomDB/LoomTCPClient');
const { PedalServer } = require('../1_pedals/pedalServer');
const { DBPipe } = require('../2_loomDB/firebaseDBPipe');

var usingPi = true;
var realLoom = true;
var realPedals = false;

const loom = new LoomHandle(6, 3, realLoom);

const defaults = {
clk: 23, // PI23 / GPIO11 / SCLK
shift: 24, // PI24 / [~W/S] GPIO8 / SPI-CE0
pedals: 21, // PI21 / GPIO9 / SPI-MISO
loomRelay: 22, // PI22 / GPIO25
countPins: [29, 31, 33, 35], // PI 29-31-33-35 / GPIO 5-6-13-19
dir: './emul-pedalHTTP/'
}
const pedals = new PedalServer(defaults, realPedals, usingPi);

const dbcon = new DBPipe(loom, pedals);
// console.log(dbcon);

dbcon.keepAlive();
// loom.vacuumOn();
25 changes: 25 additions & 0 deletions 0_main/PiPedalsOnly.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { LoomHandle } = require('../2_loomDB/LoomTCPClient');
const { PedalServer } = require('../1_pedals/pedalServer');
const { DBPipe } = require('../2_loomDB/firebaseDBPipe');

var usingPi = true;
var realLoom = false;
var realPedals = true;

const loom = new LoomHandle(6, 3, realLoom, usingPi);

const defaults = {
clk: 23, // PI23 / GPIO11 / SCLK
shift: 24, // PI24 / [~W/S] GPIO8 / SPI-CE0
pedals: 21, // PI21 / GPIO9 / SPI-MISO
loomRelay: 22, // PI22 / GPIO25
countPins: [29, 31, 33, 35], // PI 29-31-33-35 / GPIO 5-6-13-19
dir: './emul-pedalHTTP/'
}
const pedals = new PedalServer(defaults, realPedals, usingPi);

const dbcon = new DBPipe(loom, pedals);
// console.log(dbcon);

dbcon.keepAlive();
// loom.vacuumOn();
25 changes: 25 additions & 0 deletions 0_main/allOnPC.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { LoomHandle } = require('../2_loomDB/LoomTCPClient');
const { PedalServer } = require('../1_pedals/pedalServer_PC');
const { DBPipe } = require('../2_loomDB/firebaseDBPipe');

var usingPi = false;
var realLoom = false;
var realPedals = false;

const loom = new LoomHandle(6, 3, realLoom);

const defaults = {
clk: 23, // PI23 / GPIO11 / SCLK
shift: 24, // PI24 / [~W/S] GPIO8 / SPI-CE0
pedals: 21, // PI21 / GPIO9 / SPI-MISO
loomRelay: 22, // PI22 / GPIO25
countPins: [5, 6, 13, 19], // PI 29-31-33-35 / GPIO 5-6-13-19
dir: './emul-pedalHTTP/'
}
const pedals = new PedalServer(defaults, realPedals, usingPi);

const dbcon = new DBPipe(loom, pedals);
// console.log(dbcon);

dbcon.keepAlive();
// loom.vacuumOn();
25 changes: 25 additions & 0 deletions 0_main/allOnPi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { LoomHandle } = require('../2_loomDB/LoomTCPClient');
const { PedalServer } = require('../1_pedals/pedalServer');
const { DBPipe } = require('../2_loomDB/firebaseDBPipe');

var usingPi = true;
var realLoom = true;
var realPedals = true;

const loom = new LoomHandle(6, 3, realLoom);

const defaults = {
clk: 23, // PI23 / GPIO11 / SCLK
shift: 24, // PI24 / [~W/S] GPIO8 / SPI-CE0
pedals: 21, // PI21 / GPIO9 / SPI-MISO
loomRelay: 22, // PI22 / GPIO25
countPins: [29, 31, 33, 35], // PI 29-31-33-35 / GPIO 5-6-13-19
dir: './emul-pedalHTTP/'
}
const pedals = new PedalServer(defaults, realPedals, usingPi);

const dbcon = new DBPipe(loom, pedals);
// console.log(dbcon);

dbcon.keepAlive();
// loom.vacuumOn();
84 changes: 84 additions & 0 deletions 1_pedals/emul-pedalHTTP/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* FILE: client.js
* served with index.html
*/

class Pedal {
constructor(n) {
this.id = n;
this.idStr = this.id.toString();
this.state = false;
this.element = document.createElement("button");
this.element.id = this.idStr;
this.update();
}

update() {
if (this.state) {
this.element.innerText = this.idStr + ": true";
} else {
this.element.innerText = this.idStr + ": false";
}
}

toggle() {
this.state = !this.state;
this.update();
}
}

const ws = new WebSocket('ws://localhost:8080');
const pedalArrayDiv = document.getElementById('pedal-array');
const addButton = document.getElementById('add-pedal');
const remButton = document.getElementById('rem-pedal');
var numPedals = 0;
var pedalsArray = [];

function addPedal() {
var p = new Pedal(numPedals);
pedalsArray.push(p);
numPedals += 1;
p.element.onclick = togglePedal;
pedalArrayDiv.appendChild(p.element);
checkNumPedals();
}

function removePedal() {
var remove = pedalsArray.pop();
pedalArrayDiv.removeChild(remove.element);
numPedals -= 1;
checkNumPedals();
}

function checkNumPedals() {
if (numPedals < 1) {
remButton.setAttribute('disabled', true);
addButton.removeAttribute('disabled');
} else if (numPedals >= 15) {
remButton.removeAttribute('disabled');
addButton.setAttribute('disabled', true);
} else {
remButton.removeAttribute('disabled');
addButton.removeAttribute('disabled');
}
ws.send('n,'+ numPedals.toString()+','+pedalArrayString());
}

function pedalArrayString() {
var str = "";
for (var i in pedalsArray) {
str += (pedalsArray[i].state ? 't' : 'f');
}
return str;
}

function togglePedal(e) {
let id = e.target.id;
console.log(e.target.id);
pedalsArray[parseInt(id)].toggle();
ws.send('p,'+id+','+pedalsArray[e.target.id].state + ',' + pedalArrayString());
}

function toggleVacuum() {
ws.send('v');
}
19 changes: 19 additions & 0 deletions 1_pedals/emul-pedalHTTP/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>

<head>
<title>Fake Pedals</title>
</head>

<body>
<div id="pedals">
<div id="button-row">
<button id="add-pedal" onclick="addPedal();">Add Pedal</button>
<button id="rem-pedal" disabled="true" onclick="removePedal();">Remove Pedal</button>
<button id="vacuum" onclick="toggleVacuum();" style="margin-left: 10pt">Vacuum</button>
</div>
<div id="pedal-array" style = "border: 1pt solid #000; margin: 10pt; padding: 10pt;"></div>
</div>
</body>
<script src="client.js"></script>

</html>
109 changes: 109 additions & 0 deletions 1_pedals/emul-pedalHTTP/pedalHTTPServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* FILE: pedalEmul.js
*
* HTTP server for a page that mimics an array of pedals
*/

const http = require("http");
const fs = require('fs');
const url = require('url-parse');
const ws = require('ws');
const { EventEmitter } = require('stream');

/**
* @event `'count'`
* -- properties: `{ numPedals: number, pedalStates: Array<boolean> }`
* @event `'states'`
* -- properties: `{ id: number, state: boolean, all: Array<boolean> }`
* @event `'vacuum'` (emulator ONLY)
* -- properties: none, indicates user pressed vacuum toggle button on
* emulator HTTP interface
*/
class PedalHTTPServer extends EventEmitter {
constructor(dir) {
super();
const filePath = dir;
const pedalEvents = this;
const wsServer = new ws.Server({
port: 8080
});

// console.log(__dirname);
console.log("files at ", filePath);
const httpServer = http.createServer(function (request, response) {

var pathname = new url(request.url);//parse(request.url).pathname;
console.log("Request for " + pathname + " received.");

response.writeHead(200);

if(pathname == "/") {
let html = fs.readFileSync(__dirname+"/index.html", "utf8");
response.write(html);
} else if (pathname == "/client.js") {
let script = fs.readFileSync(__dirname+"/client.js", "utf8");
response.write(script);
}

response.end();
}).listen(8888);

this.httpServer = httpServer;
console.log("HTTP server: port 8888");

let sockets = [];
wsServer.on('connection', function(socket) {
sockets.push(socket);

// When you receive a message, send that message to every socket.
socket.on('message', function(msg) {
// console.log(msg.toString());
let unpacked = msg.toString().split(',');
console.log(unpacked);
if (unpacked[0] == 'n') {
console.log("pedalEmul: num pedals change");
pedalEvents.emit('count', {
numPedals: parseInt(unpacked[1]),
pedalStates: pedalEvents.parsePedalArray(unpacked[2])
});
} else if (unpacked[0] == 'p') {
console.log("pedalEmul: pedal state change");
pedalEvents.emit('states', {
id: parseInt(unpacked[1]),
state: (unpacked[2] == 'true' ? true : false),
all: pedalEvents.parsePedalArray(unpacked[3])
});
} else if (unpacked[0] == 'v') {
console.log("pedalEmul: vacuum toggle");
pedalEvents.emit('vacuum');
}
});

// When a socket closes, or disconnects, remove it from the array.
socket.on('close', function() {
sockets = sockets.filter(s => s !== socket);
});
});

console.log("Websockets server: port 8080");
}

parsePedalArray(str) {
// console.log('parsing state string: ', str);
var stateArray = [];
for (var i=0; i < str.length; i++) {
// console.log(str.charAt(i));
stateArray.push(str.charAt(i) == 't' ? true : false);
}
// console.log(stateArray);
return stateArray;
}
}

module.exports = {
PedalHTTPServer
}

if (require.main === module) {
const fakePedals = new PedalHTTPServer("./");
}
Loading

0 comments on commit c8581b9

Please sign in to comment.