Merge pull request #450 from ikkala/eslint

Eslint
pull/452/head
Gordon Williams 2020-05-26 16:50:45 +01:00 committed by GitHub
commit 43c81b06dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 437 additions and 280 deletions

152
apps/.eslintrc.json Normal file
View File

@ -0,0 +1,152 @@
{
"env": {
// TODO: "espruino": false
// TODO: "banglejs": false
},
"extends": "eslint:recommended",
"globals": {
// Methods and Fields at https://banglejs.com/reference
"Array": "readonly",
"ArrayBuffer": "readonly",
"ArrayBufferView": "readonly",
"Bangle": "readonly",
"BluetoothDevice": "readonly",
"BluetoothRemoteGATTCharacteristic": "readonly",
"BluetoothRemoteGATTServer": "readonly",
"BluetoothRemoteGATTService": "readonly",
"Boolean": "readonly",
"console": "readonly",
"DataView": "readonly",
"Date": "readonly",
"E": "readonly",
"Error": "readonly",
"Flash": "readonly",
"Float32Array": "readonly",
"Float64Array": "readonly",
"fs": "readonly",
"Function": "readonly",
"Graphics": "readonly",
"heatshrink": "readonly",
"I2C": "readonly",
"Int16Array": "readonly",
"Int32Array": "readonly",
"Int8Array": "readonly",
"InternalError": "readonly",
"JSON": "readonly",
"Math": "readonly",
"Modules": "readonly",
"NRF": "readonly",
"Number": "readonly",
"Object": "readonly",
"OneWire": "readonly",
"Pin": "readonly",
"process": "readonly",
"Promise": "readonly",
"ReferenceError": "readonly",
"RegExp": "readonly",
"Serial": "readonly",
"SPI": "readonly",
"Storage": "readonly",
"StorageFile": "readonly",
"String": "readonly",
"SyntaxError": "readonly",
"tensorflow": "readonly",
"TFMicroInterpreter": "readonly",
"TypeError": "readonly",
"Uint16Array": "readonly",
"Uint24Array": "readonly",
"Uint32Array": "readonly",
"Uint8Array": "readonly",
"Uint8ClampedArray": "readonly",
"Waveform": "readonly",
// Methods and Fields at https://banglejs.com/reference
"analogRead": "readonly",
"analogWrite": "readonly",
"arguments": "readonly",
"atob": "readonly",
"Bluetooth": "readonly",
"BTN": "readonly",
"BTN1": "readonly",
"BTN2": "readonly",
"BTN3": "readonly",
"BTN4": "readonly",
"BTN5": "readonly",
"btoa": "readonly",
"changeInterval": "readonly",
"clearInterval": "readonly",
"clearTimeout": "readonly",
"clearWatch": "readonly",
"decodeURIComponent": "readonly",
"digitalPulse": "readonly",
"digitalRead": "readonly",
"digitalWrite": "readonly",
"dump": "readonly",
"echo": "readonly",
"edit": "readonly",
"encodeURIComponent": "readonly",
"eval": "readonly",
"getPinMode": "readonly",
"getSerial": "readonly",
"getTime": "readonly",
"global": "readonly",
"HIGH": "readonly",
"I2C1": "readonly",
"Infinity": "readonly",
"isFinite": "readonly",
"isNaN": "readonly",
"LED": "readonly",
"LED1": "readonly",
"LED2": "readonly",
"load": "readonly",
"LoopbackA": "readonly",
"LoopbackB": "readonly",
"LOW": "readonly",
"NaN": "readonly",
"parseFloat": "readonly",
"parseInt": "readonly",
"peek16": "readonly",
"peek32": "readonly",
"peek8": "readonly",
"pinMode": "readonly",
"poke16": "readonly",
"poke32": "readonly",
"poke8": "readonly",
"print": "readonly",
"require": "readonly",
"reset": "readonly",
"save": "readonly",
"Serial1": "readonly",
"setBusyIndicator": "readonly",
"setInterval": "readonly",
"setSleepIndicator": "readonly",
"setTime": "readonly",
"setTimeout": "readonly",
"setWatch": "readonly",
"shiftOut": "readonly",
"SPI1": "readonly",
"Terminal": "readonly",
"trace": "readonly",
"VIBRATE": "readonly",
// Aliases and not defined at https://banglejs.com/reference
"g": "readonly",
"WIDGETS": "readonly"
},
"parserOptions": {
"ecmaVersion": 11
},
"rules": {
"no-case-declarations": "off",
"no-constant-condition": "off",
"no-delete-var": "off",
"no-empty": "off",
"no-global-assign": "off",
"no-inner-declarations": "off",
"no-octal": "off",
"no-prototype-builtins": "off",
"no-redeclare": "off",
// TODO: "no-undef": "warn",
"no-undef": "off",
"no-unused-vars": "off",
"no-useless-escape": "off"
}
}

View File

@ -3,7 +3,7 @@ currently-running apps */
(() => {
function draw() {
g.reset(); // reset the graphics context to defaults (color/font/etc)
// add your code
// add your code
g.drawString("X", this.x, this.y);
}

View File

@ -15,7 +15,8 @@
"rules": {
"indent": [
"error",
2
2,
{ "SwitchCase": 1 }
],
"linebreak-style": [
"error",
@ -24,10 +25,11 @@
"quotes": [
"error",
"double"
],
]
/*,
"semi": [
"error",
"always"
]
]*/
}
}

View File

@ -1,153 +1,153 @@
exports = class Exercise {
constructor(params) {
this.completed = false;
this.sets = [];
this.title = params.title;
this.weight = params.weight;
this.weightIncrement = params.weightIncrement;
this.unit = params.unit;
this.restPeriod = params.restPeriod;
this._originalRestPeriod = params.restPeriod;
this._restTimeout = null;
this._restInterval = null;
this._state = null;
}
get humanTitle() {
return `${this.title} ${this.weight}${this.unit}`;
}
get subTitle() {
const totalSets = this.sets.length;
const uncompletedSets = this.sets.filter((set) => !set.isCompleted()).length;
const currentSet = (totalSets - uncompletedSets) + 1;
return `Set ${currentSet} of ${totalSets}`;
}
decRestPeriod() {
this.restPeriod--;
}
addSet(set) {
this.sets.push(set);
}
currentSet() {
return this.sets.filter(set => !set.isCompleted())[0];
}
isLastSet() {
return this.sets.filter(set => !set.isCompleted()).length === 1;
}
isCompleted() {
return !!this.completed;
}
canSetCompleted() {
return this.sets.filter(set => set.isCompleted()).length === this.sets.length;
}
setCompleted() {
if (!this.canSetCompleted()) throw "All sets must be completed";
if (this.canProgress()) this.weight += this.weightIncrement;
this.completed = true;
}
canProgress() {
let completedRepsTotalSum = 0;
let targetRepsTotalSum = 0;
this.sets.forEach(set => completedRepsTotalSum += set.reps);
this.sets.forEach(set => targetRepsTotalSum += set.maxReps);
return (targetRepsTotalSum - completedRepsTotalSum) === 0;
}
startRestTimer(workout) {
this._restTimeout = setTimeout(() => {
this.next(workout);
}, 1000 * this.restPeriod);
this._restInterval = setInterval(() => {
this.decRestPeriod();
if (this.restPeriod < 0) {
this.resetRestTimer();
this.next();
return;
}
workout.emit("redraw");
}, 1000 );
}
resetRestTimer() {
clearTimeout(this._restTimeout);
clearInterval(this._restInterval);
this._restTimeout = null;
this._restInterval = null;
this.restPeriod = this._originalRestPeriod;
}
isRestTimerRunning() {
return this._restTimeout != null;
}
setupStartedButtons(workout) {
clearWatch();
setWatch(() => {
this.currentSet().incReps();
workout.emit("redraw");
}, BTN1, {repeat: true});
setWatch(workout.next.bind(workout), BTN2, {repeat: false});
setWatch(() => {
this.currentSet().decReps();
workout.emit("redraw");
}, BTN3, {repeat: true});
}
setupRestingButtons(workout) {
clearWatch();
setWatch(workout.next.bind(workout), BTN2, {repeat: false});
}
next(workout) {
const STARTED = 1;
const RESTING = 2;
const COMPLETED = 3;
switch(this._state) {
case null:
this._state = STARTED;
this.setupStartedButtons(workout);
break;
case STARTED:
this._state = RESTING;
this.startRestTimer(workout);
this.setupRestingButtons(workout);
break;
case RESTING:
this.resetRestTimer();
this.currentSet().setCompleted();
if (this.canSetCompleted()) {
this._state = COMPLETED;
this.setCompleted();
} else {
this._state = null;
}
// As we are changing state and require it to be reprocessed
// invoke the next step of workout
workout.next();
break;
default:
throw "Exercise: Attempting to move to an unknown state";
}
workout.emit("redraw");
}
}
exports = class Exercise {
constructor(params) {
this.completed = false;
this.sets = [];
this.title = params.title;
this.weight = params.weight;
this.weightIncrement = params.weightIncrement;
this.unit = params.unit;
this.restPeriod = params.restPeriod;
this._originalRestPeriod = params.restPeriod;
this._restTimeout = null;
this._restInterval = null;
this._state = null;
}
get humanTitle() {
return `${this.title} ${this.weight}${this.unit}`;
}
get subTitle() {
const totalSets = this.sets.length;
const uncompletedSets = this.sets.filter((set) => !set.isCompleted()).length;
const currentSet = (totalSets - uncompletedSets) + 1;
return `Set ${currentSet} of ${totalSets}`;
}
decRestPeriod() {
this.restPeriod--;
}
addSet(set) {
this.sets.push(set);
}
currentSet() {
return this.sets.filter(set => !set.isCompleted())[0];
}
isLastSet() {
return this.sets.filter(set => !set.isCompleted()).length === 1;
}
isCompleted() {
return !!this.completed;
}
canSetCompleted() {
return this.sets.filter(set => set.isCompleted()).length === this.sets.length;
}
setCompleted() {
if (!this.canSetCompleted()) throw "All sets must be completed";
if (this.canProgress()) this.weight += this.weightIncrement;
this.completed = true;
}
canProgress() {
let completedRepsTotalSum = 0;
let targetRepsTotalSum = 0;
this.sets.forEach(set => completedRepsTotalSum += set.reps);
this.sets.forEach(set => targetRepsTotalSum += set.maxReps);
return (targetRepsTotalSum - completedRepsTotalSum) === 0;
}
startRestTimer(workout) {
this._restTimeout = setTimeout(() => {
this.next(workout);
}, 1000 * this.restPeriod);
this._restInterval = setInterval(() => {
this.decRestPeriod();
if (this.restPeriod < 0) {
this.resetRestTimer();
this.next();
return;
}
workout.emit("redraw");
}, 1000 );
}
resetRestTimer() {
clearTimeout(this._restTimeout);
clearInterval(this._restInterval);
this._restTimeout = null;
this._restInterval = null;
this.restPeriod = this._originalRestPeriod;
}
isRestTimerRunning() {
return this._restTimeout != null;
}
setupStartedButtons(workout) {
clearWatch();
setWatch(() => {
this.currentSet().incReps();
workout.emit("redraw");
}, BTN1, {repeat: true});
setWatch(workout.next.bind(workout), BTN2, {repeat: false});
setWatch(() => {
this.currentSet().decReps();
workout.emit("redraw");
}, BTN3, {repeat: true});
}
setupRestingButtons(workout) {
clearWatch();
setWatch(workout.next.bind(workout), BTN2, {repeat: false});
}
next(workout) {
const STARTED = 1;
const RESTING = 2;
const COMPLETED = 3;
switch(this._state) {
case null:
this._state = STARTED;
this.setupStartedButtons(workout);
break;
case STARTED:
this._state = RESTING;
this.startRestTimer(workout);
this.setupRestingButtons(workout);
break;
case RESTING:
this.resetRestTimer();
this.currentSet().setCompleted();
if (this.canSetCompleted()) {
this._state = COMPLETED;
this.setCompleted();
} else {
this._state = null;
}
// As we are changing state and require it to be reprocessed
// invoke the next step of workout
workout.next();
break;
default:
throw "Exercise: Attempting to move to an unknown state";
}
workout.emit("redraw");
}
};

View File

@ -1 +1 @@
require("heatshrink").decompress(atob("mEwxH+ACPI5AUSADAtB5vNGFQtBAIfNF95hoF4wwoF5AwmF5BhmXYbAEF/6QbF1QwIF04qB54ADAwIwoF4oRKBoIvsB4gvZ58kkgCDFxoxaF5wuHGDQcMF5IwXDZwLDGDmlDIWlkgJDSwIABCRAwPDQohCFgIABDQIOCFwYABr4RCCQIvQDYguEAAwtFF5owJDZAvHFw4vFOYQvKFAowMBxIvFMQwvPAB4wFUQ4vJGDYvUGC4vNdgyuEGDIsNFwYwGNAgAPExAvMGIdfTIovfTpYvrfRCOkZ44ugF44NGF05gUFyQvKGIoueGKIufGJ4uhG5oupGItfr4vvAAgvlGAQvt/wrEF9oEGF841IF9QGHX0oGIAD8kAAYJOFzwEBBQoMFACA="))
require("heatshrink").decompress(atob("mEwxH+ACPI5AUSADAtB5vNGFQtBAIfNF95hoF4wwoF5AwmF5BhmXYbAEF/6QbF1QwIF04qB54ADAwIwoF4oRKBoIvsB4gvZ58kkgCDFxoxaF5wuHGDQcMF5IwXDZwLDGDmlDIWlkgJDSwIABCRAwPDQohCFgIABDQIOCFwYABr4RCCQIvQDYguEAAwtFF5owJDZAvHFw4vFOYQvKFAowMBxIvFMQwvPAB4wFUQ4vJGDYvUGC4vNdgyuEGDIsNFwYwGNAgAPExAvMGIdfTIovfTpYvrfRCOkZ44ugF44NGF05gUFyQvKGIoueGKIufGJ4uhG5oupGItfr4vvAAgvlGAQvt/wrEF9oEGF841IF9QGHX0oGIAD8kAAYJOFzwEBBQoMFACA="))

View File

@ -1,28 +1,28 @@
exports = class Set {
constructor(maxReps) {
this.completed = false;
this.minReps = 0;
this.reps = 0;
this.maxReps = maxReps;
}
isCompleted() {
return !!this.completed;
}
setCompleted() {
this.completed = true;
}
incReps() {
if (this.completed) return;
if (this.reps >= this.maxReps) return;
this.reps++;
}
decReps() {
if (this.completed) return;
if (this.reps <= this.minReps) return;
this.reps--;
}
}
exports = class Set {
constructor(maxReps) {
this.completed = false;
this.minReps = 0;
this.reps = 0;
this.maxReps = maxReps;
}
isCompleted() {
return !!this.completed;
}
setCompleted() {
this.completed = true;
}
incReps() {
if (this.completed) return;
if (this.reps >= this.maxReps) return;
this.reps++;
}
decReps() {
if (this.completed) return;
if (this.reps <= this.minReps) return;
this.reps--;
}
};

View File

@ -1,83 +1,84 @@
exports = class Workout {
constructor(params) {
this.title = params.title;
this.exercises = [];
this.completed = false;
this.on("redraw", redraw.bind(null, this));
}
addExercises(exercises) {
exercises.forEach(exercise => this.exercises.push(exercise));
}
currentExercise() {
return this.exercises.filter(exercise => !exercise.isCompleted())[0];
}
canComplete() {
return this.exercises.filter(exercise => exercise.isCompleted()).length === this.exercises.length;
}
setCompleted() {
if (!this.canComplete()) throw "All exercises must be completed";
this.completed = true;
}
isCompleted() {
return !!this.completed;
}
static fromJSON(workoutJSON) {
const Set = require("buffgym-set.js");
const Exercise = require("buffgym-exercise.js");
const workout = new this({
title: workoutJSON.title,
});
const exercises = workoutJSON.exercises.map(exerciseJSON => {
const exercise = new Exercise({
title: exerciseJSON.title,
weight: exerciseJSON.weight,
weightIncrement: exerciseJSON.weightIncrement,
unit: exerciseJSON.unit,
restPeriod: exerciseJSON.restPeriod,
});
exerciseJSON.sets.forEach(setJSON => {
exercise.addSet(new Set(setJSON));
});
return exercise;
});
workout.addExercises(exercises);
return workout;
}
toJSON() {
return {
title: this.title,
exercises: this.exercises.map(exercise => {
return {
title: exercise.title,
weight: exercise.weight,
weightIncrement: exercise.weightIncrement,
unit: exercise.unit,
sets: exercise.sets.map(set => set.maxReps),
restPeriod: exercise.restPeriod,
};
}),
};
}
// State machine
next() {
if (this.canComplete()) {
this.setCompleted();
this.emit("redraw");
return;
}
// Call current exercise state machine
this.currentExercise().next(this);
}
}
exports = class Workout {
constructor(params) {
this.title = params.title;
this.exercises = [];
this.completed = false;
this.on("redraw", params.redraw.bind(null, this));
}
addExercises(exercises) {
exercises.forEach(exercise => this.exercises.push(exercise));
}
currentExercise() {
return this.exercises.filter(exercise => !exercise.isCompleted())[0];
}
canComplete() {
return this.exercises.filter(exercise => exercise.isCompleted()).length === this.exercises.length;
}
setCompleted() {
if (!this.canComplete()) throw "All exercises must be completed";
this.completed = true;
}
isCompleted() {
return !!this.completed;
}
static fromJSON(workoutJSON, redraw) {
const Set = require("buffgym-set.js");
const Exercise = require("buffgym-exercise.js");
const workout = new this({
title: workoutJSON.title,
redraw: redraw,
});
const exercises = workoutJSON.exercises.map(exerciseJSON => {
const exercise = new Exercise({
title: exerciseJSON.title,
weight: exerciseJSON.weight,
weightIncrement: exerciseJSON.weightIncrement,
unit: exerciseJSON.unit,
restPeriod: exerciseJSON.restPeriod,
});
exerciseJSON.sets.forEach(setJSON => {
exercise.addSet(new Set(setJSON));
});
return exercise;
});
workout.addExercises(exercises);
return workout;
}
toJSON() {
return {
title: this.title,
exercises: this.exercises.map(exercise => {
return {
title: exercise.title,
weight: exercise.weight,
weightIncrement: exercise.weightIncrement,
unit: exercise.unit,
sets: exercise.sets.map(set => set.maxReps),
restPeriod: exercise.restPeriod,
};
}),
};
}
// State machine
next() {
if (this.canComplete()) {
this.setCompleted();
this.emit("redraw");
return;
}
// Call current exercise state machine
this.currentExercise().next(this);
}
};

View File

@ -248,7 +248,7 @@ function getWorkoutIndex() {
function buildWorkout(fName) {
const Workout = require("buffgym-workout.js");
const workoutJSON = require("Storage").readJSON(fName);
const workout = Workout.fromJSON(workoutJSON);
const workout = Workout.fromJSON(workoutJSON, redraw);
return workout;
}

View File

@ -190,7 +190,7 @@ function setButtons(){
setWatch(nextwp.bind(null,-1), BTN1, {repeat:true,edge:"falling"});
setWatch(doselect, BTN2, {repeat:true,edge:"falling"});
setWatch(nextwp.bind(null,1), BTN3, {repeat:true,edge:"falling"});
};
}
var SCREENACCESS = {
withApp:true,

View File

@ -95,7 +95,7 @@ function startKeyboardHID() {
getCharacter().then(ch => {
return sendHID(KEY[ch]);
}).then(startKeyboardHID);
};
}
if (settings.HID=="kb" || settings.HID=="kbmedia") {
if (settings.HID=="kbmedia") {

View File

@ -65,7 +65,7 @@ Bangle.on('touch', function(button) {
// setting bpm by tapping the screen. Uses the mean time difference between several tappings.
if (tindex < time_diffs.length) {
if (Date.now()-tStart < 5000) {
time_diffs[tindex] = Date.now()-tStart;
time_diffs[tindex] = Date.now()-tStart;
}
} else {
tindex=0;

View File

@ -51,8 +51,8 @@ function drawNum(num,col,x,y,func){
let tx = x*100+25;
let ty = y*104+32;
for (let i=0;i<numerals[num].length;i++){
if (i>0) g.setColor((func==fill)?"#000000":col);
func(translate(tx,ty,numerals[num][i]));
if (i>0) g.setColor((func==fill)?"#000000":col);
func(translate(tx,ty,numerals[num][i]));
}
}

View File

@ -1,7 +1,7 @@
(function(back) {
function updateSettings() {
storage.write('numerals.json', numeralsSettings);
};
}
function resetSettings() {
numeralsSettings = {
color:0,

View File

@ -5,7 +5,7 @@
function draw() {
var width = 24;
g.reset();
g.reset();
g.setFont("6x8", 1);
g.setFontAlign(0, 0);
g.clearRect(this.x,this.y+15,this.x+width,this.y+23); // erase background

View File

@ -1,7 +1,7 @@
(() => {
function draw() {
g.reset();
var m = process.memory();
var m = process.memory();
var pc = Math.round(m.usage*100/m.total);
g.drawImage(atob("BwgBqgP////AVQ=="), this.x+(24-7)/2, this.y+4);
g.setColor(pc>70 ? "#ff0000" : (pc>50 ? "#ffff00" : "#ffffff"));

View File

@ -4,10 +4,12 @@
"author": "Gordon Williams <gw@pur3.co.uk> (http://espruino.com)",
"version": "0.0.1",
"devDependencies": {
"acorn": ""
"acorn": "",
"eslint": "7.1.0"
},
"scripts": {
"test": "node bin/sanitycheck.js",
"lint-apps": "eslint ./apps --ext .js",
"test": "node bin/sanitycheck.js && eslint ./apps --ext .js",
"start": "npx http-server"
},
"dependencies": {