Skip to content

Commit

Permalink
Add clock, char to code map, remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
retrogeek46 committed Feb 9, 2023
1 parent f438320 commit d904d4f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 55 deletions.
53 changes: 40 additions & 13 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ const options = {
const systemInfoInterval = constants.SYSTEM_INFO_INTERVAL;
let systemInfoTimer = null;

Date.prototype.toShortFormat = function () {
const monthNames = [
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
"AUG",
"SEP",
"OCT",
"NOV",
"DEC",
];

const day = this.getDate();
const monthIndex = this.getMonth();
const monthName = monthNames[monthIndex];
const year = this.getFullYear().toString().substring(2);

return `${day}-${monthName}-${year}`;
};

const server = async (electronObj) => {
// const server = https.createServer(options, app);
const server = http.createServer(app);
Expand Down Expand Up @@ -66,6 +90,7 @@ const server = async (electronObj) => {
// XXX: stop sending cpu data to keyboard till rgb/oled is added to keeb
// keyboardQmk.updateKeyboard(3, parseInt(cpuUsageRaw));
keyboardQmk.updateKeyboard(2);
keyboardQmk.updateKeyboard(8, formatDateTime());
// let cpuVoltage = systemInfoValues["Value2"]["value"];
const msg = `CPU Temp: ${cpuTemp}, CPU Voltage: ${cpuVoltage}, CPU Usage: ${cpuUsage}`;
// logger.sysinfo(msg);
Expand All @@ -78,15 +103,6 @@ const server = async (electronObj) => {
clearInterval(systemInfoTimer);
};

// exports.startSystemInfoTimer = async () => {
// const systemData = await systemInfo.getSystemInfo();
// // logger.info(systemData);
// const systemInfoValues = systemData["HKCU\\SOFTWARE\\HWiNFO64\\VSB"]["values"];
// const msg = `CPU Voltage: ${systemInfoValues["Value2"]["value"]}`;
// logger.info(msg);
// this.emitMessage("systemInfo", msg);
// };

app.get("/", (req, res) => {
res.sendFile(__dirname + "/static/keybKontroller.html");
});
Expand All @@ -112,8 +128,9 @@ const server = async (electronObj) => {
});

app.post("/updateCurrentMedia", (req, res) => {
const currentMediaTitle = req.body.currentMediaTitle.toLowerCase().replace(/[^a-z ]/g, "");
const currentArtist = req.body.currentArtist.toLowerCase().replace(/[^a-z ]/g, "");
const currentMediaTitle = req.body.currentMediaTitle.toUpperCase().replace(/[^A-Z ]/g, "");
const currentArtist = req.body.currentArtist.toUpperCase().replace(/[^A-Z ]/g, "");
// console.log(currentArtist);
const [mediaTitleArray, mediaArtistArray] = formatMediaInfo(
currentMediaTitle,
currentArtist
Expand All @@ -131,15 +148,25 @@ const server = async (electronObj) => {
// remove artist name from title
currentMediaTitle = currentMediaTitle.replace(currentArtist, "");
currentMediaTitle = currentMediaTitle.trim();
let mediaTitleArray = [...currentMediaTitle].map(i => i.charCodeAt(0));
let mediaTitleArray = [...currentMediaTitle].map(i => constants.CHAR_TO_CODE_MAPPING[i]);
mediaTitleArray = mediaTitleArray.slice(0, 21);

let mediaArtistArray = [...currentArtist].map((i) => i.charCodeAt(0));
let mediaArtistArray = [...currentArtist].map((i) => constants.CHAR_TO_CODE_MAPPING[i]);
mediaArtistArray = mediaArtistArray.slice(0, 21);

return [mediaTitleArray, mediaArtistArray]
}

const formatDateTime = () => {
let now = new Date();
let hours = now.getHours() > 9 ? now.getHours() : "0" + now.getHours();
let minutes = now.getMinutes() > 9 ? now.getMinutes() : "0" + now.getMinutes();
let seconds = now.getSeconds() > 9 ? now.getSeconds() : "0" + now.getSeconds();
let formattedDate = now.toShortFormat() + ` ${hours}:${minutes}:${seconds}`;
let formattedDateArray = [...formattedDate].map(i => constants.CHAR_TO_CODE_MAPPING[i]);
return formattedDateArray;
}

exports.emitMessage = (tag, message) => {
io.emit(tag, message);
};
Expand Down
33 changes: 3 additions & 30 deletions app/services/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ let keyboard = null;
let dataListenerAttached = false

const connectKeyboard = () => {
// logger.info("connect keyboard called");
if (!keyboard) {
const devices = hid.devices();
// logger.info(devices);
Expand All @@ -32,7 +31,6 @@ const connectKeyboard = () => {
) {
keyboard = new hid.HID(d.path);
logger.info("Keyboard connected");
// attachDataListener();
break;
}
}
Expand All @@ -42,12 +40,6 @@ const connectKeyboard = () => {
const attachDataListener = () => {
logger.info("attaching data listener");
keyboard.on("data", (val) => {
// logger.info(
// val[0] + "-" +
// val[1] + "-" +
// val[2] + " " +
// new Date()
// );
if (val[0] == 23) {
let encoderState = val[1];
let layerState = val[2];
Expand All @@ -73,8 +65,9 @@ const attachErrorListener = () => {
3 set_cpu_usage_rgb
4 update_os_state
5 test_rgb_value
6
7
6 media title
7 media artist
8 clock
*/
exports.updateKeyboard = (value, extraValues=0) => {
try {
Expand Down Expand Up @@ -102,26 +95,6 @@ exports.getKeyboard = () => {
return keyboard;
}

// exports.getEncoderState = () => {
// let encoderState = null;
// try {
// if (!keyboard) {
// connectKeyboard();
// }
// // if (!dataListenerAttached) {
// // attachDataListener()
// // }
// // if (!errorListenerAttached) {
// // attachErrorListener();
// // }
// keyboard.write([1, 11]);
// return encoderState;
// } catch (ex) {
// this.resetKeyboard();
// return 0;
// }
// }

exports.resetKeyboard = () => {
logger.info("reset keyboard connection");
keyboard = null;
Expand Down
69 changes: 57 additions & 12 deletions app/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = Object.freeze({
OS: {
WINDOWS: "Windows",
MAC: "MacOS",
LINUX: "Linux"
LINUX: "Linux",
},
SYSINFO_PATH: "logs/systemInfo.log",
WINDOW_MSG_OK: "",
Expand All @@ -18,16 +18,61 @@ module.exports = Object.freeze({
"MIDI",
"FUNC",
"FUNC_MAC",
"MAPS"
"MAPS",
],
ENCODER_STATES: [
"_",
"MACROS",
"SCROLL"
],
OS_STATES:[
"WIN",
"MAC"
],
ALPHABET: "abcdefghijklmnopqrstuvwxyz"
ENCODER_STATES: ["_", "MACROS", "SCROLL"],
OS_STATES: ["WIN", "MAC"],
CHAR_TO_CODE_MAPPING: {
" ": 1,
A: 4,
B: 5,
C: 6,
D: 7,
E: 8,
F: 9,
G: 10,
H: 11,
I: 12,
J: 13,
K: 14,
L: 15,
M: 16,
N: 17,
O: 18,
P: 19,
Q: 20,
R: 21,
S: 22,
T: 23,
U: 24,
V: 25,
W: 26,
X: 27,
Y: 28,
Z: 29,
1: 30,
2: 31,
3: 32,
4: 33,
5: 34,
6: 35,
7: 36,
8: 37,
9: 38,
0: 39,
_: 44,
"-": 45,
"=": 46,
"[": 47,
"]": 48,
"\\": 49,
"#": 50,
";": 51,
"'": 52,
"`": 53,
",": 54,
".": 55,
"/": 56,
":": 57,
},
});

0 comments on commit d904d4f

Please sign in to comment.