🎂 Houdini 150   🚚 FREE FEDEX SHIPPING from 397 USD   🌎 WORLDWIDE SHIPPING for 25 USD

AtomTime

About

Let's create a basic mini-app to set a time on the SB Watch with a numeric keyboard. The functionality will be similar you can achieve with the TimeSmith app, however, you can directly connect Atom and SB Watch and you will need no phone. This mini-app is called Atom Time!

Technically you only need Atom and an SB Watch, but this app supports PeekSmith 3 as well to display the time entered and to give you visual feedback about the time you set. If you have no PeekSmith connected, then it will be ignored.

function main() {
  ps.connect('*');
  ps.print('AtomTime');
  sbwatch.connect('SBWatch-155309');
  // sbwatch.setTime('12:00');
}

let time = ''; // the time entered
let timeout = 0; // will contain a timeout ID
let mapping = [ // button layour
  '1', '2', '3',
  '4', '5', '6',
  '7', '8', '9',
  's', '0', 'x'
];
function send() {
  clearTimeout(timeout); // don't trigger send
  if (time === '') return; // nothing to set
  if (time === '9999') {
    sbwatch.setCurrentTime();
  } else {
    sbwatch.setTime(time);
  }
  time = ''; // reset to empty string
}

function onButtonClick(buttonId) {
  let key = mapping[buttonId];
  
  // send immediately
  if (key === 's') {
    send();
    return;
  }
  
  // delete the last number or add a new
  if (key === 'x') {
    if (strLen(time) > 0) time = strSub(time, 0, strLen(time) - 1);
  } else {
    time = time + key;
  }

  // delete the previous timer, and start a new with 4s
  clearTimeout(timeout);
  timeout = setTimeout(send, 4000);

  // display the time entered
  ps.print(`<${time}>`);
}

// displays the processed time, and hand movement indicator
function printFormattedTime(time, moving) {
  if (moving) time = `*${time}*`;
  ps.print(time);
}

// there are events coming from the SB Watch and the buttons of Atom
function onEvent(e) {
  console.log(e.value, e.type, e.source);
  if (e.source === 'sbwatch:hands' && e.type === 'started') {
    atom.vibrate('\'');
    printFormattedTime(e.value, true);
  }
  if (e.source === 'sbwatch:hands' && e.type === 'finished') {
    atom.vibrate('\'');
    printFormattedTime(e.value, false);
  }
  if (e.type === 'click' && e.source === 'atom:button') {
    onButtonClick(parseInt(e.value));
  }
}
cross