Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "osb-action-button",
"version": "0.0.0",
"version": "1.0.1",
"type": "commonjs",
"scripts": {
"dev": "ts-node-dev --respawn ./src/sounder.ts",
Expand All @@ -17,6 +17,7 @@
"devDependencies": {
"@faker-js/faker": "^10.0.0",
"@types/express": "^5.0.3",
"@types/node": "^25.6.0",
"@types/node-cron": "^3.0.11",
"@types/play-sound": "^1.1.2",
"@vitest/coverage-v8": "^3.2.4",
Expand Down
19 changes: 15 additions & 4 deletions src/button.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cron from 'node-cron'
import fs from 'fs'
import path from 'path'
import {Gpio, type High, type Low} from 'onoff'

Check warning on line 4 in src/button.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

'Low' is defined but never used. Allowed unused vars must match /^ignored/u

Check warning on line 4 in src/button.ts

View workflow job for this annotation

GitHub Actions / ⬣ ESLint

'High' is defined but never used. Allowed unused vars must match /^ignored/u

import {log} from './utils/log'
import {led} from './utils/led'
Expand All @@ -9,6 +9,7 @@
import {updateConfig} from './bin/update-config'
import {buttonApi} from './utils/button-api'
import {VERSION} from './constants'
import {setState, getState} from './utils/button-state'

const {writeFile} = fs.promises

Expand Down Expand Up @@ -40,7 +41,7 @@
}
}

const newConfig = await updateConfig()
const newConfig = await updateConfig(false)

if (newConfig.ledPin !== config.ledPin) {
ledInterface.setLEDPin(newConfig.ledPin)
Expand All @@ -65,7 +66,7 @@
let holdTimeout: Timeout | null = null
let cancelled: boolean = false

let buttonPressed: number = new Date().getTime()
setState(false)

btn.watch(async (err, value) => {
if (err) {
Expand All @@ -76,7 +77,9 @@
if (value === 1) {
// Button has been pressed.
await log('👇 Button Pressed')
buttonPressed = new Date().getTime()

// Set the button press state to true.
setState(true)

if (triggerTimeout && !triggerTimeout._destroyed) {
// There is currently a trigger timeout, clear it and return the LED to on.
Expand Down Expand Up @@ -106,15 +109,23 @@

If the button was not last pressed within double the hold duration (or 10 seconds) ignore this release.
*/
const releaseBuffer =
/*const releaseBuffer =
config.holdDuration === 0 ? 10 : config.holdDuration * 2
if (buttonPressed < new Date().getTime() - releaseBuffer * 1000) {
await log(
`⚠️ Button press was more than ${releaseBuffer} seconds ago, ignoring release.`
)
return
}*/

if (getState() === false) {
// The button state is currently false (not pressed) so should not have been releasable
await log(`⚠️ Button was released while not pressed.`)
}

// This is a valid release so set the state to false, before any of the early returns
setState(false)

if (holdTimeout && !holdTimeout._destroyed) {
// There is a hold timeout that can be cleared
clearTimeout(holdTimeout)
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const VERSION = '1.0.0'
export const VERSION = '1.0.1'
17 changes: 17 additions & 0 deletions src/utils/button-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
declare global {
// This prevents us from making multiple connections to the db when the
// require cache is cleared.
var __buttonState: boolean | undefined
}

const defaultButtonState = false

export const getState = () => {
return global.__buttonState !== undefined
? global.__buttonState
: defaultButtonState
}

export const setState = (newState: boolean) => {
global.__buttonState = newState
}
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
"types": [
"node"
] /* Specify type package names to be included without being referenced in a source file. */,
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
Expand Down Expand Up @@ -111,4 +113,4 @@
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
}
Loading