• @user130485

    downloaded your code and gave my understanding of it a shot...

    Three things that bothers me about the code are:

    1. What are the prints still do? May be I missed the memo that it does not matter anymore for the console buffer to get full...
    2. The intervalled function checking for meter LED stuck on keeps going, and since its code messes with D1 and D2, it may mess with the actual process. (see also 3.).
    3. A clearWatch() without arguments seems never to be fine with me (same as clearInterval() and clearTimeout() with no arguments have undesired effects).

    And there is a 4th thing - not really adding to the code - is the source formatting... if it is structurally off, I get mislead in the logic... So my first action was to adjust it to support my (human) reading abilities and my understanding what happens when and in what relation to other sections of the code.

    Regarding code design: setInterval() is very powerful and allows straight forward and very concise code. Unfortunately, the outside world that we try to catch with the intervalled code is 'not so intervally', and thtt is why I usually resort to a monitored state machine: state machine for tracking the all state transitions, and monitored to detect irregularities in the timing of states and transitions.

    Intervalled code is fine if it does not interfere with state. (Sure one can have gazillions of synchronization flags / semaphores, but that does make the code not more robust or components more loosely coupled).

  • was not meant to be a production code =) just something I was putting together for myself
    Prints are just for debugging. I was under the impression that unless you actually connect to the console it will make no difference. Device is usually just passively sending the data via BLE and I only connect to it to see if all ok or to flash an update
    When LED was getting stuck, it was draining the battery quickly. I was adding the code to minimize the drain.
    clearWatch - as per API, if no parameter is supplied, all watches will be removed. This is just in the onInit stage and not used anywhere else. I think it was in the original code, so I just left it there
    Did not understand the state part

About

Avatar for user130485 @user130485 started