Persistent "background" variables

Posted on
  • I've just begun trying to program my first app for bangle.js 2, a very simple reminder. Idea is, you activate it by pressing button 1 three times. While it is active, a small widget is shown and it vibrates every x minutes. You can deactivate it by pressing the button again three times. You can also automatically activate it at x o'clock, for example to gently remind you to take medicine or not forget something without needing to be "snoozed" like a mobile phone alarm.

    I've realised it before with an Amazfit Bip and Tasker which was very convoluted but turned out to be really helpfull to remind me of stuff. (Timed activation to take afternoon medication, spontaneous activation to not forget doing x while doing something else).

    I have the basic software running, but as I am completely new to js and bangle (but have experience in C an Python) I don't fully understand variable and action "scopes". I have the code saved as annoy.boot.js so it is executed automatically and just starts a listener for a button event. I've also defined variables there if the function is active or not. However, everytime I change back from the launcher to clock or app to clock, I think the settings are lost.

    Is there a good primer on js/bangle.js, which "scope" (are they called frames in js?) is active at which status and what happens/what is deleted when switching from app to clock?

    Thanks in advance and huge thanks to Gordon and the team for making it super easy to go from no idea in js to first prototype app running on actual hardware in about a day!

  • There is no global variable store on Bangle. The only way to persist a value across loading one app to another is to write what you want to share to a json file. This is fairly easy to do. Looking at any app that has a settings file will show you how to do this. You can then load your state back into the app when it starts up. The other way is to make a widget that stays loaded but again widgets get started and stopped so need to save any state or variables they want to persist across reloads.

  • As @HughB says, all RAM is wiped between apps. Given the open nature of apps it makes it much harder to get in the situation where one rouge app can make your watch unusable.

    There is a E.on('kill', ...) event that you can use to write the current state of your app into before everything shuts down, so there's no need to write to files all the time - just when the app is closed.

  • Ah, good to know, thanks! The ammount of data written is so small that there is no risk of memory/flash wear I assume?

  • I imagine you will be hard pressed to wear the flash out by writing to it too many times. It might be possible if you deliberately wrote an app to do this millions of times.

    There is 8MB of storage so plenty of space for a few variables in a json string.

    BTW Javascript is a lot easier to work with than Python or C. If you are familiar with C then most of the block structure, loops, for, while etc will be known to you already. Worth looking at W3schools for basic exmaples of Javascript one concept at a time.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview

Persistent "background" variables

Posted by Avatar for Jonathan @Jonathan