even better display code, some options (WIP)

master
Henry Jameson 8 months ago
parent 73ce94692b
commit 87ec7b2f49
  1. 21
      index.js
  2. 1
      package.json
  3. 41
      statuslogger.js
  4. 5
      yarn.lock

@ -1,11 +1,28 @@
import { NFC } from 'nfc-pcsc'
import fetch from 'node-fetch'
import fs from 'fs'
import { program } from 'commander'
import { Feedback } from './statuslogger.js'
program
.option('-i, --i2c-bus <num>', 'i2c bus to use for LCD display')
.option('--noLCD', 'don\'t use LCD display, not recommended for production use')
.option('-e, --emulator-port <num>', 'port of the card emulator')
program.parse();
const opts = program.opts()
const CARDS_LOCATION = '/home/dietpi/YACards/'
const logger = Feedback()
logger.initLCD()
const logger = Feedback({ bus: opts.i2cBus, noLCD: opts.noLCD })
if (opts.noLCD) {
logger.info('[EABU]: Skipping LCD initialization on user\'s request')
} else if (opts.i2cBus) {
logger.initLCD()
} else {
logger.error('[EABU]: No i2c bus is specified, nor --noLCD is used, aborting')
process.exit(1)
}
logger.info('[EABU]: Starting up (if this hangs check if pcscd is running)')
const nfc = new NFC()

@ -2,6 +2,7 @@
"type": "module",
"dependencies": {
"chalk": "^5.0.1",
"commander": "^9.4.1",
"nfc-pcsc": "^0.8.1",
"node-fetch": "^3.2.9",
"raspberrypi-liquid-crystal": "^1.18.0",

@ -6,7 +6,13 @@ const err = chalk.bold.redBright
const inf = chalk.blueBright
const wrn = chalk.yellowBright
const SPINNER = ['/', '-', '\\', '|']
const SPIN_CHARS = [
[0x14,0x1c,0x15,0x1,0x6,0x0,0x00,0x0],
[0x14,0x1c,0x15,0x1,0x6,0x0,0x10,0x0],
[0x14,0x1c,0x15,0x1,0x6,0x0,0x14,0x0],
[0x14,0x1c,0x15,0x1,0x6,0x0,0x15,0x0]
]
const SPINNER = SPIN_CHARS.map((c, i) => i)
const STATUS_LINE = [
'Status: _______'
@ -78,7 +84,7 @@ const THANKS_FOR_PLAYING = [
'time. t. hj',
]
export const Feedback = () => {
export const Feedback = ({ bus, noLCD }) => {
const status = {
hasLCD: false,
spinnerIndex: 0,
@ -98,7 +104,7 @@ export const Feedback = () => {
}
const quipSelect = () => {
if (status.messageCoundown !== 0) {
if (status.messageCoundown > 0) {
switch (status.messageType) {
case 'cardPresent':
if (status.mgmgtCard === 'guest') {
@ -118,9 +124,15 @@ export const Feedback = () => {
return RENEW_CARD
case 'renew_ok':
return RENEW_CARD_OK
default:
return [
'--------------------',
'ERROR: INVALID MSG!!',
'--------------------',
]
}
} else {
if (!status.playing) {
if (!status.mgmtCard) {
return IDLE
} else {
if (status.mgmtCard === 'guest') {
@ -133,10 +145,10 @@ export const Feedback = () => {
}
const statusLineGenerator = () => {
let countdownI = countdown > 0
let countdownI = status.countdown > 0
? `${status.countdown}`.padStart(3, ' ')
: ' '
let mgmtCardI = 0
let mgmtCardI = '-'
if (status.mgmtCard === true) mgmtCardI = 'P'
if (status.mgmtCard === 'guest') mgmtCardI = 'G'
let emuCardI = status.emuCard ? 'C' : '_'
@ -152,7 +164,7 @@ export const Feedback = () => {
return STATUS_LINE[0].replace('_______', statusLineGenerator())
}
const bufferGen = () => [ ...lineGen, ...quipSelect ]
const bufferGen = () => [ lineGen(), ...quipSelect() ]
return {
setMgmtCard(MgmtCard) {
@ -176,21 +188,21 @@ export const Feedback = () => {
},
warn(...string) {
console.warn(wrn('[!]') + ' ' + string.join(' '))
this.advanceSpinner()
},
info(...string) {
console.info(inf('[ ]') + ' ' + string.join(' '))
this.advanceSpinner()
},
error(...string) {
console.error(err('[!]') + ' ' + string.join(' '))
this.advanceSpinner()
},
initLCD() {
this.info('Initializing LCD...')
const loop = () => {
const buffer = bufferGen()
this.advanceSpinner()
//lcd.clearSync()
buffer.forEach((line, i) => lcd.printLineSync(i, line.substring(0, 20)))
if (status.messageCountdown > 0) {
status.messageCountdown--
@ -202,7 +214,7 @@ export const Feedback = () => {
setTimeout(loop, 500)
}
lcd = new LCD( 1, 0x27, 20, 4 );
lcd = new LCD(Number(bus), 0x27, 20, 4);
lcd
.begin()
.then(() => {
@ -213,6 +225,9 @@ export const Feedback = () => {
lcd.printLineSync(1, ' Maximum Tune 3DX+')
lcd.printLineSync(2, ' IC card support ')
lcd.printLineSync(3, ' by HJ (and GXTX)')
SPIN_CHARS.forEach((c, i) => {
lcd.createCharSync(i, c)
})
setTimeout(loop, 3000)
})
@ -223,7 +238,9 @@ export const Feedback = () => {
},
advanceSpinner() {
if (status.spinnerIndex >= SPINNER.length) status.spinnerIndex = 0
status.spinner = SPINNER[status.spinnerIndex++]
console.log(lcd)
status.spinner = LCD.getChar(status.spinnerIndex++)
console.log(status.spinner)
}
}
}

@ -22,6 +22,11 @@ chalk@^5.0.1:
resolved "https://registry.yarnpkg.com/chalk/-/chalk-5.0.1.tgz#ca57d71e82bb534a296df63bbacc4a1c22b2a4b6"
integrity sha512-Fo07WOYGqMfCWHOzSXOt2CxDbC6skS/jO9ynEcmpANMoPrD+W1r1K6Vx7iNm+AQmETU1Xr2t+n8nzkV9t6xh3w==
commander@^9.4.1:
version "9.4.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-9.4.1.tgz#d1dd8f2ce6faf93147295c0df13c7c21141cfbdd"
integrity sha512-5EEkTNyHNGFPD2H+c/dXXfQZYa/scCKasxWcXJaWnNJ99pnQN9Vnmqow+p+PlFPE63Q6mThaZws1T+HxfpgtPw==
data-uri-to-buffer@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/data-uri-to-buffer/-/data-uri-to-buffer-4.0.0.tgz#b5db46aea50f6176428ac05b73be39a57701a64b"

Loading…
Cancel
Save