sweepclock: Added a memory printout to the console so the memory usage can be followed when connected

pull/743/head
adrian w kirk 2021-05-16 20:04:32 +01:00
parent a2ba365a07
commit 193b880ffb
1 changed files with 182 additions and 166 deletions

View File

@ -1,8 +1,8 @@
/**
* Adrian Kirk 2021-03
* Simple Clock showing 1 numeral for the hour
* with a smooth sweep second.
*/
* Adrian Kirk 2021-03
* Simple Clock showing 1 numeral for the hour
* with a smooth sweep second.
*/
const screen_center_x = g.getWidth()/2;
const screen_center_y = 10 + g.getHeight()/2;
@ -36,14 +36,14 @@ const color_schemes = [
background : [0.4,0.7,1.0],
second_hand: [0.5,0.5,0.5],
}
];
];
let color_scheme_index = 0;
var WHITE = [1.0,1.0,1.0];
const WHITE = [1.0,1.0,1.0];
function default_white(color){
if(color == null){
return WHITE
return WHITE;
} else {
return color;
}
@ -269,7 +269,7 @@ var local = require('locale');
var last_date = null;
var last_datestr = null;
var last_coords = null;
var date_coords = [
const date_coords = [
{ name: "topright", coords:[180,30]},
{ name: "bottomright", coords:[180,220]},
{ name: "bottomleft", coords: [5,220]},
@ -354,9 +354,9 @@ function draw_hours(date){
}
/**
* We want to be able to change the font so we set up
* pure virtual for all fonts implementtions to use
*/
* We want to be able to change the font so we set up
* pure virtual for all fonts implementtions to use
*/
class NumeralFont {
/**
* The screen dimensions of what we are going to
@ -386,29 +386,34 @@ class NoFont extends NumeralFont{
getName(){return "NoFont";}
}
const COPASET_DIM_20x58 = [20,58];
const COPASET_DIM_30x58 = [30,58];
const COPASET_DIM_40x58 = [40,58];
const COPASET_DIM_50x58 = [50,58];
class CopasetFont extends NumeralFont{
constructor(){
super();
}
getDimensions(hour){
switch(hour){
case 1: return [20,58];
case 1: return COPASET_DIM_20x58;
case 2:
case 3:
case 4:
case 5:
case 7:
return [30,58];
return COPASET_DIM_30x58;
case 6:
case 8:
case 9:
case 11:
case 12:
return [40,58];
return COPASET_DIM_40x58;
case 10:
return [50,58];
return COPASET_DIM_50x58;
default:
return [30,58];
return COPASET_DIM_30x58;
}
}
hour_txt(hour){ return hour.toString(); }
@ -431,7 +436,13 @@ class CopasetFont extends NumeralFont{
getName(){return "Copaset";}
}
const ROMAN_DIM_10x40 = [10,40];
const ROMAN_DIM_20x40 = [20,40];
const ROMAN_DIM_25x40 = [25,40];
const ROMAN_DIM_30x40 = [30,40];
const ROMAN_DIM_40x40 = [40,40];
const ROMAN_DIM_60x40 = [60,40];
const ROMAN_DIM_70x40 = [70,40];
class RomanNumeralFont extends NumeralFont{
constructor(){
super();
@ -456,26 +467,26 @@ class RomanNumeralFont extends NumeralFont{
getDimensions(hour){
switch (hour){
case 1:
return [10,40];
return ROMAN_DIM_10x40;
case 2:
return [25,40];
return ROMAN_DIM_25x40;
case 3:
case 4:
case 6:
case 9:
case 11:
case 12:
return [40,40];
return ROMAN_DIM_40x40;
case 5:
return [30,40];
return ROMAN_DIM_30x40;
case 7:
return [60,40];
return ROMAN_DIM_60x40;
case 8:
return [70,40];
return ROMAN_DIM_70x40;
case 10:
return [20,40];
return ROMAN_DIM_20x40;
default:
return [40,40];
return ROMAN_DIM_40x40;
}
}
hour_txt(hour){ return this.getText(hour); }
@ -524,10 +535,10 @@ function rebasePositive(angle){
}
/**
* The Hour Scriber is responsible for drawing the numeral
* on the screen at the requested angle.
* It allows for the font to be changed on the fly.
*/
* The Hour Scriber is responsible for drawing the numeral
* on the screen at the requested angle.
* It allows for the font to be changed on the fly.
*/
class HourScriber {
constructor(radius, numeral_font, draw_test){
this.radius = radius;
@ -617,8 +628,8 @@ class HourScriber {
let numeral_fonts = [new CopasetFont(), new RomanNumeralFont(), new NoFont()];
let numeral_fonts_index = 0;
/**
* predicate for deciding when the digit has to be redrawn
*/
* predicate for deciding when the digit has to be redrawn
*/
let hour_numeral_redraw = function(angle_from, angle_to, last_draw_time){
var seconds_hand_angle = seconds_hand.angle;
// we have to cope with the 12 problem where the
@ -642,11 +653,11 @@ let hour_numeral_redraw = function(angle_from, angle_to, last_draw_time){
let hour_scriber = new HourScriber(70,
numeral_fonts[numeral_fonts_index],
hour_numeral_redraw
);
);
/**
* Called from button 1 to change the numerals that are
* displayed on the clock face
*/
* Called from button 1 to change the numerals that are
* displayed on the clock face
*/
function next_font(){
numeral_fonts_index = numeral_fonts_index + 1;
if(numeral_fonts_index >= numeral_fonts.length){
@ -689,9 +700,9 @@ function next_colorscheme(){
}
/**
* called from load_settings on startup to
* set the color scheme to named value
*/
* called from load_settings on startup to
* set the color scheme to named value
*/
function set_colorscheme(colorscheme_name){
console.log("setting color scheme:" + colorscheme_name);
for (var i=0; i < color_schemes.length; i++) {
@ -705,9 +716,9 @@ function set_colorscheme(colorscheme_name){
}
/**
* called from load_settings on startup
* to set the font to named value
*/
* called from load_settings on startup
* to set the font to named value
*/
function set_font(font_name){
console.log("setting font:" + font_name);
for (var i=0; i < numeral_fonts.length; i++) {
@ -722,8 +733,8 @@ function set_font(font_name){
}
/**
* Called on startup to set the watch to the last preference settings
*/
* Called on startup to set the watch to the last preference settings
*/
function load_settings(){
try{
var settings = require("Storage").readJSON("sweepclock.settings.json");
@ -746,9 +757,15 @@ function load_settings(){
}
}
function print_memoryusage(){
var m = process.memory();
var pc = Math.round(m.usage*100/m.total);
console.log("memory usage: " + pc + "%");
}
/**
* Called on button press to save down the last preference settings
*/
* Called on button press to save down the last preference settings
*/
function save_settings(){
var settings = {
font : numeral_fonts[numeral_fonts_index].getName(),
@ -757,6 +774,7 @@ function save_settings(){
};
console.log("saving:" + JSON.stringify(settings));
require("Storage").writeJSON("sweepclock.settings.json",settings);
print_memoryusage();
}
// Boiler plate code for setting up the clock,
@ -785,14 +803,12 @@ function scheduleDrawClock(){
}
function reset_clock(){
g.clear();
force_redraw = true;
}
Bangle.on('lcdPower', (on) => {
if (on) {
console.log("lcdPower: on");
Bangle.drawWidgets();
reset_clock();
startTimers();
} else {