[BJv2, 95% done] An app for soft reboot?

Posted on
  • hi,
    i would sometimes like to reboot the bangle v2 using app, without using the button.

    i asked GPT4 for this code:

    (function() {
      function reboot() {
        E.reboot();
      }
    
      Bangle.on('lcdPower', function(on) {
        if (on) {
          Bangle.setLCDMode('on');
          reboot();
        }
      });
    
      // Draw a user interface if needed, or execute reboot directly
      g.clear();
      g.setFontAlign(0, 0); // center alignment
      g.setFont("Vector", 20);
      g.drawString("Rebooting...", g.getWidth() / 2, g.getHeight() / 2);
      setTimeout(reboot, 2000); // Execute reboot after 2 seconds for the user to read the screen
    })();
    

    it worked in the IDE when i connect my real bangle to it, and paste this code on the RHS and press "RAM".

    I am going to follow the guideline and make it into an app, is that okay? any suggestions? thanks

    wanna name it softreboot for short and SoftReBoot for the plain text name

    following this tutorial:
    https://www.espruino.com/Bangle.js+First­+App

  • yeah, looks good.
    uploaded as *.js with the *.info now.

    i think i'll use it for several days,
    if nothing wrong,
    will borrow an icon from web and see if the store need it.

    ps, for icon, i can use open source icon without worry correct?
    thanks

  • next step:
    pop up a random number from 0-10 and only reboot if user choose correct from odd vs even.

    ps: the writing of the 1st code in #1 is good for gpt4,
    when adding logic to it, gpt4 didn't work well.
    need human intervention.

    it keeps on doing stupid things.

  • hi, originally i wann let user choose odd vs even for a randomly displayed number,
    but gpt4 not good at it.

    i simply ask it to make a "screen tap" to confirm the reboot,
    and one can escape by using the "switch back to the clock" app's swipe <-- this agree with my idea that at least one swipe should be kept for app control.

    thanks

    i'll see if the store will need this app,

    console.log('Starting script.');
    
    (function() {
      let rebootConfirmed = false;
    
      function reboot() {
        console.log('Attempting to reboot.');
        E.reboot();
        console.log('Should have rebooted.'); // This may not print if the reboot occurs immediately
      }
    
      console.log('Setting up LCD power event listener.');
      Bangle.on('lcdPower', function(on) {
        console.log('LCD power event triggered. Power state:', on);
        if (on && rebootConfirmed) {
          console.log('LCD is on and reboot is confirmed.');
          Bangle.setLCDMode('on');
          reboot();
        } else if (on) {
          console.log('LCD is on but reboot is not yet confirmed.');
        } else {
          console.log('LCD is off.');
        }
      });
    
      console.log('Clearing the screen and displaying confirmation message.');
      g.clear();
      g.setFontAlign(0, 0); // center alignment
      g.setFont("Vector", 15); // Adjusted the font size
      g.drawString("Tap to confirm", g.getWidth() / 2, g.getHeight() / 2 - 10);
      g.drawString("reboot", g.getWidth() / 2, g.getHeight() / 2 + 10);
    
      console.log('Setting up touch event listener.');
      Bangle.on('touch', function(e) {
        console.log('Touch event triggered. Event data:', e);
        if (e && !rebootConfirmed) {
          console.log('Touch detected and reboot is not yet confirmed.');
          rebootConfirmed = true;
    
          console.log('Clearing the screen and displaying rebooting message.');
          g.clear();
          g.drawString("Rebooting...", g.getWidth() / 2, g.getHeight() / 2);
    
          console.log('Waiting for 2 seconds before reboot.');
          setTimeout(() => {
            console.log('2 seconds elapsed, initiating reboot.');
            reboot();
          }, 2000);
        } else if (e) {
          console.log('Touch detected but reboot is already confirmed.');
        } else {
          console.log('Touch event triggered but no touch detected.');
        }
      });
    })();
    
    console.log('Script setup complete.');
    
  • @Gordon
    hi, i think this app is useful, i think i didn't found such an app in the store,
    am i wellcomed to submit it to the store?
    currently the *.js and *.info is done, have not prepared the icon.
    thanks

    ps i am using it right now.

    btw, for my currently another project, makeing "swipe back to clock" to use swipe up to down (stage 1) then with a menu (stage 2).
    stage 2 wont be a recent work, while i think stage 1 is still useful.
    for such a minor modification do you think i should keep it to myself or still submit to the store and rename it somehow? thx

  • same code as above, i put on github,
    using MIT license

    https://github.com/ccchan234/bangle2-Sof­tReBoot/blob/main/v002

    thank you

  • I'll say it's useful,

    today after i go out and wanna connect my bangle to PC, but no success.
    it worked after a soft reboot.

  • ps: this is the info file:

    require("Storage").write("softreboot.inf­o",{
    "id":"softreboot",
    "name":"SoftReBoot",
    "src":"softreboot.app.js"
    });

    coverting an icon and sending to the app store need more effect than writing one...
    i'' skip it at the moment
    reference here:
    https://www.espruino.com/Bangle.js+First­+App

  • I'd probably have tried to do this with Bangle.showClock() called on a specific tap event. If you want some suggestion 🙂

    https://www.espruino.com/ReferenceBANGLE­JS2#l_Bangle_tap

  • hi, thx for reply.

    myself completely comfort with the original code (generated by gpt4, i knew nothing about js, but general programming knowledge on pascal, C etc).

    yet i input your feed back to gpt4 and it gave a modified code,
    i just attach here as untested and see if anyone will prefer that and could report back.

    thanks.

    You:

    my js for bangle.js v2, for an app to reboot:

    console.log('Starting script.');
    (function() {
      let rebootConfirmed = false;
      function reboot() {
        console.log('Attempting to reboot.');
        E.reboot();
        console.log('Should have rebooted.'); // This may not print if the reboot occurs immediately
      }
      console.log('Setting up LCD power event listener.');
      Bangle.on('lcdPower', function(on) {
        console.log('LCD power event triggered. Power state:', on);
        if (on && rebootConfirmed) {
          console.log('LCD is on and reboot is confirmed.');
          Bangle.setLCDMode('on');
          reboot();
        } else if (on) {
          console.log('LCD is on but reboot is not yet confirmed.');
        } else {
          console.log('LCD is off.');
        }
      });
      console.log('Clearing the screen and displaying confirmation message.');
      g.clear();
      g.setFontAlign(0, 0); // center alignment
      g.setFont("Vector", 15); // Adjusted the font size
      g.drawString("Tap to confirm", g.getWidth() / 2, g.getHeight() / 2 - 10);
      g.drawString("reboot", g.getWidth() / 2, g.getHeight() / 2 + 10);
      console.log('Setting up touch event listener.');
      Bangle.on('touch', function(e) {
        console.log('Touch event triggered. Event data:', e);
        if (e && !rebootConfirmed) {
          console.log('Touch detected and reboot is not yet confirmed.');
          rebootConfirmed = true;
          console.log('Clearing the screen and displaying rebooting message.');
          g.clear();
          g.drawString("Rebooting...", g.getWidth() / 2, g.getHeight() / 2);
          console.log('Waiting for 2 seconds before reboot.');
          setTimeout(() => {
            console.log('2 seconds elapsed, initiating reboot.');
            reboot();
          }, 2000);
        } else if (e) {
          console.log('Touch detected but reboot is already confirmed.');
        } else {
          console.log('Touch event triggered but no touch detected.');
        }
      });
    })();
    console.log('Script setup complete.');
    

    ChatGPT:

    Your JavaScript code for the Bangle.js v2 smartwatch appears to be designed to create an app that allows the user to reboot the device through a simple interface. Here's a breakdown of how your script works:

    Initialization: The script starts by logging "Starting script."

    Reboot Function: A function reboot() is defined to handle the actual reboot process. It logs "Attempting to reboot," calls E.reboot() to restart the device, and then logs "Should have rebooted."

    LCD Power Event Listener: The script listens for changes in the LCD's power state. If the LCD is turned on and a reboot has been confirmed, it initiates the reboot process.

    Displaying Confirmation Message: The script clears the screen and displays a message asking the user to tap the screen to confirm the reboot.

    Touch Event Listener: When the screen is touched, if the reboot hasn't been confirmed yet, it sets rebootConfirmed to true, displays a "Rebooting..." message, and then waits for 2 seconds before actually calling the reboot() function.

    Final Log: After setting up everything, the script logs "Script setup complete."

    This script effectively sets up an interactive reboot mechanism on the Bangle.js v2 smartwatch. By leveraging the touch screen and LCD power state, it provides a user-friendly way to reboot the device. The use of console.log statements throughout the script is helpful for debugging and understanding the flow of events.


    You:

    my frd: Ganblejs
    I'd probably have tried to do this with Bangle.showClock() called on a specific tap event. If you want some suggestion 🙂

    https://www.espruino.com/ReferenceBANGLE­JS2#l_Bangle_tap


    ChatGPT:

    Your friend's suggestion to use Bangle.showClock() in combination with a specific tap event on the Bangle.js smartwatch is a good idea, especially if you're looking to integrate the reboot functionality into a more standard watch behavior.

    Here's a revised approach based on your friend's suggestion:

    Use Tap Event Instead of Touch Event: The Bangle.js v2 has a Bangle.on('tap', callback) event listener, which can be used to detect tap gestures. This can be a more intuitive way for users to trigger actions like rebooting, as tapping is a common interaction pattern on smartwatches.

    Implement Bangle.showClock(): The Bangle.showClock() function can be used to return the watch to its default clock display. This could be a good way to exit the reboot confirmation screen if the user decides not to proceed with the reboot.

    Revised Script Structure:

    Initialize: Start with the usual setup and logging.
    Reboot Function: Keep the reboot() function as is.
    LCD Power Event Listener: This can remain the same or be adjusted based on how you want the screen behavior to work with the new tap interaction.
    Displaying Confirmation Message: Similar to before, but you might include instructions for how to confirm or cancel the reboot (e.g., "Tap to confirm reboot, show clock to cancel").
    Tap Event Listener: Replace the touch event listener with a tap event listener. On a tap, you could toggle the reboot confirmation. If confirmed, proceed with the reboot sequence; if not, call Bangle.showClock() to return to the clock display.
    Final Log: Indicate the script setup is complete.

    This approach would be more aligned with the common user interactions on the Bangle.js v2 and might feel more natural to users. It also adds a clear way to cancel the reboot action, which is a good practice for user interface design.

    For more detailed information and examples, you can refer to the Espruino Bangle.js Reference, specifically the Bangle.tap section.


    You:

    show me the new code


    ChatGPT:

    Certainly! Based on the suggestion to use a tap event and Bangle.showClock(), here's how you could modify your script for the Bangle.js v2:

    javascript
    Copy code

    console.log('Starting script.');
    
    (function() {
      let rebootConfirmed = false;
    
      function reboot() {
        console.log('Attempting to reboot.');
        E.reboot();
        console.log('Should have rebooted.'); // This may not print if the reboot occurs immediately
      }
    
      console.log('Setting up LCD power event listener.');
      Bangle.on('lcdPower', function(on) {
        console.log('LCD power event triggered. Power state:', on);
        if (on && rebootConfirmed) {
          console.log('LCD is on and reboot is confirmed.');
          Bangle.setLCDMode('on');
          reboot();
        }
      });
    
      console.log('Clearing the screen and displaying confirmation message.');
      g.clear();
      g.setFontAlign(0, 0); // center alignment
      g.setFont("Vector", 15); // Adjusted the font size
      g.drawString("Tap to confirm reboot", g.getWidth() / 2, g.getHeight() / 2 - 10);
      g.drawString("Show clock to cancel", g.getWidth() / 2, g.getHeight() / 2 + 10);
    
      console.log('Setting up tap event listener.');
      Bangle.on('tap', function() {
        console.log('Tap event triggered.');
        if (!rebootConfirmed) {
          console.log('Reboot confirmed by tap.');
          rebootConfirmed = true;
          g.clear();
          g.drawString("Rebooting...", g.getWidth() / 2, g.getHeight() / 2);
          console.log('Waiting for 2 seconds before reboot.');
          setTimeout(() => {
            console.log('2 seconds elapsed, initiating reboot.');
            reboot();
          }, 2000);
        } else {
          console.log('Reboot already confirmed.');
        }
      });
    
      // Cancel reboot and show clock
      setWatch(function() {
        console.log('Reboot canceled, showing clock.');
        rebootConfirmed = false;
        Bangle.showClock();
      }, BTN2, { repeat: true, edge: 'falling' });
    
    })();
    
    console.log('Script setup complete.');
    

    In this modified script:

    Tap Event Listener: The script now listens for tap events to confirm the reboot.

    Cancel Reboot: A watch function is set on BTN2 (the middle button on Bangle.js v2), which cancels the reboot process and returns to the clock display when pressed.

    Confirmation Message: The screen displays instructions for confirming or canceling the reboot.

    This approach integrates a more intuitive interaction for confirming the reboot and provides an easy way to cancel it, enhancing the user experience.


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

[BJv2, 95% done] An app for soft reboot?

Posted by Avatar for ccchan @ccchan

Actions