Friday, December 18, 2015

Smart Phone, Dumb Watch: Implementing a Pebble Pedometer

My Galaxy Note 5 has a built in step counter. However I find that every time I want to check it, I have to bring up the S Health App and dig around until I can find the screen I'm looking for. Further more, I'd love to be able to track steps for a given activity. Think electronic Ranger Beads. S Health will give me a pretty graph of my steps, but not the one number I want. I've tried looking for a lighter weight pedometer app, but every one I've found has been an ad infused disaster.

By now you know where I'm going with this: this is the perfect job for my Pebble. The Pebble classic lacks the step counter that the phone has, but provides the streamlined UI that I'm after. Here's what my Steps 'App' looks like on the Pebble Classic:

There's really not much to it. I select the Steps option on the home menu, and from there, I can see my Steps Since value, as well as the daily total and the previous 4 days. Steps Since is the arbitrary activity counter I mentioned. I can clear it from the watch anytime I want.

Behind the scenes the phone is tracking both the daily and steps since activity in text files. If I ever want to use this data for other purposes, I can.

The above is all written in Tasker and leverages AutoPebble. Most of this little app is pretty obvious, but I did have a few hurdles to get over.

First, it wasn't obvious to me how to gain access to the step counter. One would assume there's a magic Tasker variable, %STEPS you can query. That's not the case. Instead, you need to setup a profile which will be notified when N steps have been taken. Within this profile, I set that global variable I'm after. See:

Profile: Steps Taken Tracker (109)
  Event: Steps Taken [ Number:10 ]
  Enter: Anon (110)
    A1: Variable Add [ Name:%STEPS_DAILY Value:10 Wrap Around:0 ]
    A2: Variable Add [ Name:%STEPS_SINCE Value:10 Wrap Around:0 ] 

I setup this profile to be notified every 10 steps, and as you can see, it updates both the daily and since global variables. I'm not sure what the impact of being notified of every step would be, but it just seems excessive.

To track steps on a daily basis I needed to save and clear out the %STEPS_DAILY value at midnight. That was also an easy profile to write:

 Profile: Steps Daily Reset (116)
  Time: 12:00AM
  Enter: Anon (117)
    A1: Write File [ File:Tasker/steps.txt Text:%DATE: %STEPS_DAILY Append:On Add Newline:On ]
    A2: Variable Set [ Name:%STEPS_DAILY To:0 Do Maths:Off Append:Off ]

Clearing the %STEPS_SINCE is done on demand. In this case I save the start, end dates as well as the step count for the period being cleared. I also send a vibration request to the watch so I get a signal that value has been cleared:

Pebble Steps Clear Since (119)
  A1: Write File [ File:Tasker/steps.since.txt Text:%STEPS_SINCE_STARTED %TIMES %STEPS_SINCE Append:On Add Newline:On ]
  A2: Variable Set [ Name:%STEPS_SINCE_STARTED To:%TIMES Do Maths:Off Append:Off ]
  A3: Variable Set [ Name:%STEPS_SINCE To:0 Do Maths:Off Append:Off ]
  A4: AutoPebble App [ Configuration:Full Screen: false
    Control App: Go Back
    Check Pebble Connected: false
    Other Pebble App: unknown
    No Prefix if Command: false
    Do Not Disturb: false
    Vibration Pattern: 300
    Clear History: false
    Open Phone App: false
    Save Scren: false
    Don't Send Screen: false
    Go Back: false
    Go Back Multi: false Package:com.joaomgcd.autopebble Name:AutoPebble App Timeout (Seconds):20 ]

One final challenge was that I wanted to show the last 4 days worth of step activity on the watch, however, steps.txt contains all activity. To get around this, I wrote an action: Tail File which will grab the last N lines from the file and return them. As you can tell, it's written in JavaScript, which means that the coding is straightforward. However, it's note especially efficient. I find no other choice but to read in the entire file as a string, which will be painful once steps.txt gets huge. But I'll deal with it then. (One solution: setup a Profile to rotate the file every month, and then the file won't grow past 30 lines or so.)

Tail File (132)
  A1: JavaScriptlet [ Code:var file = local("par1");
    var max = local("par2");

    var lines = readFile(file).split("\n");

    if(lines[lines.length-1] == "") {
      lines.pop();
    }

    max = Math.min(max, lines.length);
    while(max != lines.length) {
      lines.shift();
    }

    setLocal("lines", lines.join("\n")); Libraries: Auto Exit:On Timeout (Seconds):45 ]
  A2: Return [ Value:%lines Stop:On ] 

For completeness, here's the actions to show the steps menu and clear the steps since value. That should just about do it for this exercise:

Pebble Steps Menu (114)
  A1: Variable Set [ Name:%newline To:
  Do Maths:Off Append:Off ]
  A2: Perform Task [ Name:Tail File Priority:%priority Parameter 1 (%par1):Tasker/steps.txt Parameter 2 (%par2):5 Return Value Variable:%labels Stop:Off ]
  A3: Variable Split [ Name:%labels Splitter:%newline Delete Base:Off ]
  A4: Array Process [ Variable:%labels Type:Reverse ]
  A5: Array Push [ Name:%labels Position:1 Value:Today: %STEPS_DAILY Fill Spaces:Off ]
  A6: Array Push [ Name:%labels Position:1 Value:Since: %STEPS_SINCE Fill Spaces:Off ]
  A7: AutoPebble List [ Configuration:Full Screen: false
    Header: Steps
    Labels: %labels(:)
    Long Click Actions: steps_clear_since
    Last Line Height Default: false
    Remember Position: false
    No Prefix if Command: false
    Do Not Disturb: false
    Clear History: false
    Open Phone App: false
    Save Scren: false
    Don't Send Screen: false
    Go Back: false
    Go Back Multi: false Package:com.joaomgcd.autopebble Name:AutoPebble List Timeout (Seconds):120 ]


Profile: AutoPebble, Steps Clear Since (118)
  Event: AutoPebble [ Configuration:Command Filter: steps_clear_since
  Case Insensitive: false
  Exact: false
  Case Insensitive: false ]
  Enter: Pebble Steps Clear Since (119)
  A1: Write File [ File:Tasker/steps.since.txt Text:%STEPS_SINCE_STARTED %TIMES %STEPS_SINCE Append:On Add Newline:On ]
  A2: Variable Set [ Name:%STEPS_SINCE_STARTED To:%TIMES Do Maths:Off Append:Off ]
  A3: Variable Set [ Name:%STEPS_SINCE To:0 Do Maths:Off Append:Off ]
  A4: AutoPebble App [ Configuration:Full Screen: false
    Control App: Go Back
    Check Pebble Connected: false
    Other Pebble App: unknown
    No Prefix if Command: false
    Do Not Disturb: false
    Vibration Pattern: 300
    Clear History: false
    Open Phone App: false
    Save Scren: false
    Don't Send Screen: false
    Go Back: false
    Go Back Multi: false Package:com.joaomgcd.autopebble Name:AutoPebble App Timeout (Seconds):20 ]

No comments:

Post a Comment