Sliding Clock: reorganised the memory management of the initialization

master
Adrian Kirk 2022-10-09 10:32:09 +01:00
parent 1564b2fe38
commit d690d21eed
No known key found for this signature in database
GPG Key ID: 5A448EB0FC623526
1 changed files with 235 additions and 249 deletions

View File

@ -5,18 +5,18 @@
*/
const color_schemes = [
{
name: "white",
background : [1.0,1.0,1.0],
main_bar: [0.0,0.0,0.0],
other_bars: [0.1,0.1,0.1],
},
{
name: "black",
background : [0.0,0.0,0.0],
main_bar: [1.0,0.0,0.0],
other_bars: [0.9,0.9,0.9],
},
{
name: "white",
background : [1.0,1.0,1.0],
main_bar: [0.0,0.0,0.0],
other_bars: [0.1,0.1,0.1],
},
{
name: "red",
background : [1.0,0.0,0.0],
@ -265,7 +265,15 @@ function bangleVersion(){
return (g.getHeight()>200)? 1 : 2;
}
var row_types = {
let row_displays;
function initDisplay(settings) {
if(row_displays != null){
return;
}
if(settings == null){
settings = {};
}
var row_types = {
large: {
color: 'major',
speed: 'medium',
@ -290,33 +298,9 @@ var row_types = {
scroll_in: ['right'],
size: 'small'
}
};
};
let row_displays;
function initDisplay(settings) {
if(row_displays != null){
return;
}
if(settings == null){
settings = {};
}
const row_type_overide = date_formatter.defaultRowTypes();
mergeMaps(row_types,row_type_overide);
mergeMaps(row_types,settings.row_types);
const row_defs = (settings.row_defs != null && settings.row_defs.length > 0)?
settings.row_defs : date_formatter.defaultRowDefs();
row_displays = [];
row_defs.forEach(row_def =>{
const row_type = create_row_type(row_types[row_def.type],row_def);
// we now create the number of rows specified of that type
for(var j=0; j<row_def.rows; j++){
row_displays.push(create_row(row_type,j));
}
});
}
function mergeMaps(map1,map2){
function mergeMaps(map1,map2){
if(map2 == null){
return;
}
@ -327,15 +311,21 @@ function mergeMaps(map1,map2){
map1[key] = map2[key];
}
});
}
}
function mergeObjects(obj1, obj2){
function mergeObjects(obj1, obj2){
const result = {};
Object.keys(obj1).forEach(key => result[key] = (obj2.hasOwnProperty(key))? obj2[key] : obj1[key]);
return result;
}
}
var heights = {
const row_type_overide = date_formatter.defaultRowTypes();
mergeMaps(row_types,row_type_overide);
mergeMaps(row_types,settings.row_types);
const row_defs = (settings.row_defs != null && settings.row_defs.length > 0)?
settings.row_defs : date_formatter.defaultRowDefs();
var heights = {
vvsmall: [15,13],
vsmall: [20,15],
ssmall: [22,17],
@ -345,29 +335,24 @@ var heights = {
mlarge: [45,35],
large: [50,40],
vlarge: [60,50]
};
};
var rotations = {
var rotations = {
0: 0,
90: 3,
180: 2,
270: 1,
};
};
var speeds = {
var speeds = {
fast: 20,
medium: 10,
slow: 5,
vslow: 2,
superslow: 1
};
};
const Y_RESERVED = 20;
const SPACES = ' ';
/**
* takes a json definition for a row type and creates an instance
*/
function create_row_type(row_type, row_def){
function create_row_type(row_type, row_def){
const speed = speeds[row_type.speed];
const rotation = rotations[row_type.angle_to_horizontal];
const height = heights[row_type.size];
@ -416,6 +401,7 @@ function create_row_type(row_type, row_def){
}
var text_formatter = (txt)=>txt;
const SPACES = ' ';
if(row_def.hasOwnProperty("alignment")){
const alignment = row_def.alignment;
if(alignment.startsWith("centre")){
@ -430,6 +416,7 @@ function create_row_type(row_type, row_def){
}
const version = bangleVersion() - 1;
const Y_RESERVED = 20;
return {
row_speed: speed,
row_height: height[version],
@ -441,19 +428,13 @@ function create_row_type(row_type, row_def){
fg_color: () => (row_type.color === 'major')? main_color(): other_color(),
row_text_formatter : text_formatter
};
}
function initComplete(){
row_types = null;
speeds = null;
rotations = null;
heights = null;
const mem = process.memory(true);
console.log("init complete memory:" + mem.usage / mem.total);
}
function create_row(row_type, row_no){
return new ShiftText(row_type.x(row_no),
}
row_displays = [];
row_defs.forEach(row_def =>{
const row_type = create_row_type(row_types[row_def.type],row_def);
// we now create the number of rows specified of that type
for(var row_no=0; row_no<row_def.rows; row_no++){
row_displays.push(new ShiftText(row_type.x(row_no),
row_type.y(row_no),
'',
"Vector",
@ -465,10 +446,17 @@ function create_row(row_type, row_no){
bg_color(),
row_type,
row_type.row_rotation
)
);
}
});
// dereference the setup variables to release the memory
row_types = null;
heights = null;
rotations = null;
speeds = null;
}
function nextColorTheme(){
color_scheme_index += 1;
if(color_scheme_index >= color_schemes.length){
@ -618,13 +606,13 @@ function setColorScheme(colorscheme_name){
}
}
const Locale = require('locale');
/**
var date_formatter;
function setDateformat(shortname){
/**
* Demonstration Date formatter so that we can see the
* clock working in the emulator
*/
class DigitDateTimeFormatter {
class DigitDateTimeFormatter {
constructor() {}
format00(num){
@ -640,8 +628,8 @@ class DigitDateTimeFormatter {
formatDate(now){
const hours = now.getHours() ;
const time_txt = this.format00(hours) + ":" + this.format00(now.getMinutes());
const date_txt = Locale.dow(now,1) + " " + this.format00(now.getDate());
const month_txt = Locale.month(now);
const date_txt = require('locale').dow(now,1) + " " + this.format00(now.getDate());
const month_txt = require('locale').month(now);
return [time_txt, date_txt, month_txt];
}
@ -677,10 +665,7 @@ class DigitDateTimeFormatter {
}
];
}
}
var date_formatter;
function setDateformat(shortname){
}
console.log("setting date format:" + shortname);
try {
if (date_formatter == null) {
@ -692,7 +677,7 @@ function setDateformat(shortname){
}
}
} catch(e){
console.log("Failed to load " + shortname);
console.log("not loaded:" + shortname);
}
if(date_formatter == null){
date_formatter = new DigitDateTimeFormatter();
@ -736,7 +721,8 @@ function loadSettings() {
initDisplay();
updateColorScheme();
}
initComplete();
const mem = process.memory(true);
console.log("init complete memory:" + mem.usage / mem.total);
}
function button3pressed() {