<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/rss-style.css?cache=2026-11-13T15:19:59+00:00" type="text/css"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	     xmlns:dc="http://purl.org/dc/elements/1.1/"
	   xmlns:atom="http://www.w3.org/2005/Atom"
	     xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	  xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<html:meta xmlns:html="http://www.w3.org/1999/xhtml" name="viewport" content="width=device-width" />
<channel>
	<title>c++ &#8211; Terence Eden’s Blog</title>
	<atom:link href="https://shkspr.mobi/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>https://shkspr.mobi/blog</link>
	<description>Regular nonsense about tech and its effects 🙃</description>
	<lastBuildDate>Sat, 17 Oct 2026 10:10:32 +0000</lastBuildDate>
	<language>en-GB</language>
	<copyright>© Terence Eden. 🄯 CC BY-SA. See https://shkspr.mobi/blog/copyright-and-copyleft/</copyright>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.5</generator>

<image>
	<url>https://shkspr.mobi/blog/wp-content/uploads/2023/07/cropped-avatar-32x32.jpeg</url>
	<title>c++ &#8211; Terence Eden’s Blog</title>
	<link>https://shkspr.mobi/blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title><![CDATA[Una Watch TOTP - MFA on your wrist!]]></title>
		<link>https://shkspr.mobi/blog/2026/10/una-watch-totp-mfa-on-your-wrist/</link>
					<comments>https://shkspr.mobi/blog/2026/10/una-watch-totp-mfa-on-your-wrist/#comments</comments>
				<dc:creator><![CDATA[Terence Eden]]></dc:creator>
		<pubDate>Sun, 18 Oct 2026 11:34:08 +0000</pubDate>
		<category><![CDATA[c++]]></category><category><![CDATA[HowTo]]></category><category><![CDATA[UnaWatch]]></category><category><![CDATA[watch]]></category>		<guid isPermaLink="false">https://shkspr.mobi/blog/?p=76104</guid>

		
		<description>Let's get this out of the way first - this is a terrible idea!  I want my two-factor authentication codes to be easily accessible to me, so I've built a watch app which will display them. That's really convenient for me - but makes for a terrible security posture. Anyone who steals my watch will be able to see my one-time code. If they're clever, they can decompile the app and find the secret…</description>
			
		<content:encoded>&lt;p&gt;Let's get this out of the way first - this is a &lt;em&gt;terrible&lt;/em&gt; idea!  I want my two-factor authentication codes to be easily accessible to me, so I've built a watch app which will display them. That's really convenient for me - but makes for a terrible security posture. Anyone who steals my watch will be able to see my one-time code. If they're clever, they can decompile the app and find the secret which powers the code generation.&lt;/p&gt;

&lt;p&gt;You should absolutely not do this and, if you ignore my warnings, you should only do this for codes which aren't critical to your security.&lt;/p&gt;

&lt;!-- 
👋👋👋👋👋👋👋👋👋
Welcome to Comment Club! If this is your first time - hello! This content is only available to sophisticated nerds who read my HTML comments.

You know what? I *do* use this. I have a low-ish security account which constantly needs my MFA number. Sometimes I can't be bothered unlocking my phone to get it. Of course, on my wrist I save it under a different name. Perhaps I'm an idiot for doing so, but sometimes convenience wins.

Anyway, the three rules of Comment Club are…

1. You must not tell anyone about Comment Club - let them find out about it themselves.
2. You *must* add some Comment Club Content to at least one of your blog posts. Doesn't have to be much, just a little note will do.
3. You should drop an email to anyone whose comments you've read. Start by sending me a friendly message at comment.club@shkspr.mobi

Keep your eyes peeled for more comments in my other blog posts 😃

TTFN.
--&gt;

&lt;p&gt;In my defence, an attacker would still need my username and password to make any use of these codes - but please don't trust your security to my deranged witterings.&lt;/p&gt;

&lt;h2 id="what-it-looks-like"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-totp-mfa-on-your-wrist/#what-it-looks-like"&gt;What It Looks Like&lt;/a&gt;&lt;/h2&gt;

&lt;img src="https://shkspr.mobi/blog/wp-content/uploads/2026/09/MFA.webp" alt="My wristwatch showing &amp;quot;HSBC MFA Code 751646&amp;quot;. Don't worry, this isn't my real code. OR IS IT?!?!" width="1024" height="576" class="aligncenter"&gt;

&lt;h2 id="prior-art"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-totp-mfa-on-your-wrist/#prior-art"&gt;Prior Art&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;I'm sure many other people have done this. Indeed, three years ago I built &lt;a href="https://shkspr.mobi/blog/2023/07/an-eink-wrist-mounted-totp-generator/"&gt;An eInk, Wrist-Mounted, TOTP Generator&lt;/a&gt;.&lt;/p&gt;

&lt;img src="https://shkspr.mobi/blog/wp-content/uploads/2023/07/Wrist-TOTP.jpg" alt="A chunky wristwatch showing the time and a selection of 6 digit codes and their corresponding entities." width="1024" height="1024" class="aligncenter"&gt;

&lt;h2 id="the-service"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-totp-mfa-on-your-wrist/#the-service"&gt;The Service&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;This is in my &lt;code&gt;Service.cpp&lt;/code&gt; - as you can see, it's pretty simple. This won't let you add in hundreds of codes - you get one and that's it. There's no countdown when a code is about to expire, so I recommend waiting until a new one pops up before you start typing it in.&lt;/p&gt;

&lt;p&gt;I'll be candid, I haven't used C++ properly since university - and that was in the last century. I've mostly just cut things out to see what breaks. Feel free to educate me on the correct idiomatic code.&lt;/p&gt;

&lt;p&gt;You will need to set your own secret in here.&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-cpp"&gt;//  Standard includes.
#include "Service.hpp"
#include "SDK/Messages/CommandMessages.hpp"
#include "SDK/Messages/MessageGuard.hpp"

//  Needed for PureTOTP.
#include &amp;lt;iostream&amp;gt;
#include "TOTP.h"

#include &amp;lt;string&amp;gt;
#include &amp;lt;sstream&amp;gt;

//  What this Glance can do.
Service::Service(SDK::Kernel &amp;amp;kernel)
    : mKernel(kernel)
    , mGlanceUI()
    , mGlanceTitle()
    , mGlanceValue()
{}

void Service::createGuiControls()
{
    //  Set the main title in the Glance.
    mGlanceTitle = mGlanceUI.createText();
    mGlanceTitle.pos({ kTitleX, kTitleY }, { kTitleW, kTitleH })
        .font(GlanceFont_t::GLANCE_FONT_POPPINS_SEMIBOLD_20)
        .color(GlanceColor_t::GLANCE_COLOR_TEAL)
        .setText("HSBC MFA Code")
        .alignment(GlanceAlignH_t::GLANCE_ALIGN_H_CENTER);

    //  Set the default text for the value.
    mGlanceValue = mGlanceUI.createText();
    mGlanceValue.pos({ 20, kValueY }, { 200, kValueH })
        .font(GlanceFont_t::GLANCE_FONT_POPPINS_SEMIBOLD_20)
        .color(GlanceColor_t::GLANCE_COLOR_YELLOW_DARK)
        .setText( "Waiting for update" )
        .alignment(GlanceAlignH_t::GLANCE_ALIGN_H_CENTER);
}

void Service::glanceUpdate()
{
    //  Set the Base32 encoded secret (all uppercase).
    std::string secret = "QWERTYUIOP";
    //  Generate the code based on the Unix timestamp right now.
    const std::time_t t = std::time(0);
    std::string code = TOTP::GenerateTOTP( 6, 30, TOTP::HMAC_SHA1, secret, t );

    std::ostringstream ss;
    ss &amp;lt;&amp;lt; t;

    //  Convert the string to char* and display it as the value.
    mGlanceValue.print( code.c_str() ); 

    //  Show the Unix time to check the timezone is correct.
    // mGlanceTitle.print( ss.str().c_str() );
}

//  No service.
Service::~Service(){}

//  When this glance is opened.
void Service::run()
{
    while (true) {
        SDK::MessageBase *msg;
        if(!mKernel.comm.getMessage(msg)) {
            continue;
        }

        switch (msg-&amp;gt;getType()) {
            //  The Glance has started.
            case SDK::MessageType::EVENT_GLANCE_START:
                if (configGui()) {
                    createGuiControls();
                } else {
                    mKernel.comm.releaseMessage(msg);
                    mKernel.sys.exit(0);
                }
                break;
            //  The Glance has been stopped.
            case SDK::MessageType::COMMAND_APP_STOP:
            case SDK::MessageType::EVENT_GLANCE_STOP:
                mKernel.comm.releaseMessage(msg);
                return;
                break;

            //  Every second, perform an action.
            case SDK::MessageType::EVENT_GLANCE_TICK:
                onGlanceTick();
                break;

            default:
                break;
        }

        mKernel.comm.releaseMessage(msg);
    }
}

void Service::onGlanceTick()
{
    if (mGlanceUI.isInvalid()) {
        if (auto upd = SDK::make_msg&amp;lt;SDK::Message::RequestGlanceUpdate&amp;gt;(mKernel)) {
            upd-&amp;gt;name           = APP_NAME;
            upd-&amp;gt;controls       = mGlanceUI.data();
            upd-&amp;gt;controlsNumber = static_cast&amp;lt;uint32_t&amp;gt;(mGlanceUI.size());
            upd.send(100);
        }

        mGlanceUI.setValid();
    }
    glanceUpdate();
}

bool Service::configGui()
{
    bool status = false;
    if (auto gc = SDK::make_msg&amp;lt;SDK::Message::RequestGlanceConfig&amp;gt;(mKernel)) {
        if (gc.send(100) &amp;amp;&amp;amp; gc.ok()) {
            if (gc-&amp;gt;maxControls &amp;gt;= 3) {
                mGlanceUI.setWidth(gc-&amp;gt;width);
                mGlanceUI.setHeight(gc-&amp;gt;height);
                status = true;
            }
        }
    }
    return status;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id="the-library"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-totp-mfa-on-your-wrist/#the-library"&gt;The Library&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;There are dozens of C++ libraries for RFC 6238 generation. I ended up using &lt;a href="https://github.com/Einztein/PureTOTP"&gt;PureTOTP&lt;/a&gt; as it didn't require any external dependencies and was only two files.  As I only needed SHA1 TOTP codes, I stripped out a bunch of functionality which was unnecessary for me. Similarly, I only wanted to generate 6 digit codes with a 30 second period - so took out anything which did more than that.&lt;/p&gt;

&lt;p&gt;I've tidied up the code and comments where I can. The original was MIT Licenced - so this is as well.&lt;/p&gt;

&lt;p&gt;There are two parts to this. First, the header&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-_"&gt;// TOTP.h
//
#pragma once
#include &amp;lt;string&amp;gt;
class TOTP
{
public:
    const static int HMAC_SHA1 = 20;    //  Length of HMAC.

    static std::string GenerateTOTP(const std::string&amp;amp; secret);
    static std::string GenerateTOTP(int digit, int refreshSeconds, int algorithm, const std::string&amp;amp; secret, int specTime);

    TOTP(int digit, int refreshSeconds, int algorithm, const std::string&amp;amp; secret): digit(digit), refreshSeconds(refreshSeconds), algorithm(algorithm), secret(secret) {}
    TOTP(const std::string&amp;amp; secret): digit(6), refreshSeconds(30), algorithm(HMAC_SHA1), secret(secret) {}

    std::string currentTOTP();

private:
    int digit, refreshSeconds, algorithm;

    std::string secret;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next is the standalone library which does the TOTP generation, &lt;code&gt;TOTP.cpp&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-cpp"&gt;// TOTP.cpp
//
#include "TOTP.h"
#include &amp;lt;vector&amp;gt;
#include &amp;lt;stack&amp;gt;
#include &amp;lt;queue&amp;gt;
#include &amp;lt;sstream&amp;gt;
#include &amp;lt;iomanip&amp;gt;
#include &amp;lt;cstdint&amp;gt;
#include &amp;lt;cstring&amp;gt;
#include &amp;lt;array&amp;gt;
#include &amp;lt;stdexcept&amp;gt;
#include &amp;lt;algorithm&amp;gt;
using namespace std;

struct JEVP_MD
{
    enum Kind {
        SHA1
    };

    Kind kind;
    int  md_size;
    int  block_size;
};

static inline uint32_t rotl32(uint32_t x, uint32_t n) { return (x &amp;lt;&amp;lt; n) | (x &amp;gt;&amp;gt; (32 - n)); }
static inline uint32_t rotr32(uint32_t x, uint32_t n) { return (x &amp;gt;&amp;gt; n) | (x &amp;lt;&amp;lt; (32 - n)); }
static inline uint64_t rotr64(uint64_t x, uint64_t n) { return (x &amp;gt;&amp;gt; n) | (x &amp;lt;&amp;lt; (64 - n)); }

static uint32_t load_be32(const unsigned char* p)
{
    return (uint32_t(p[0]) &amp;lt;&amp;lt; 24) |
           (uint32_t(p[1]) &amp;lt;&amp;lt; 16) |
           (uint32_t(p[2]) &amp;lt;&amp;lt;  8) |
            uint32_t(p[3]);
}

static void store_be32(uint32_t v, unsigned char* p)
{
    p[0] = static_cast&amp;lt;unsigned char&amp;gt;((v &amp;gt;&amp;gt; 24) &amp;amp; 0xFF);
    p[1] = static_cast&amp;lt;unsigned char&amp;gt;((v &amp;gt;&amp;gt; 16) &amp;amp; 0xFF);
    p[2] = static_cast&amp;lt;unsigned char&amp;gt;((v &amp;gt;&amp;gt;  8) &amp;amp; 0xFF);
    p[3] = static_cast&amp;lt;unsigned char&amp;gt;( v        &amp;amp; 0xFF);
}

static void store_be64(uint64_t v, unsigned char* p)
{
    p[0] = static_cast&amp;lt;unsigned char&amp;gt;((v &amp;gt;&amp;gt; 56) &amp;amp; 0xFF);
    p[1] = static_cast&amp;lt;unsigned char&amp;gt;((v &amp;gt;&amp;gt; 48) &amp;amp; 0xFF);
    p[2] = static_cast&amp;lt;unsigned char&amp;gt;((v &amp;gt;&amp;gt; 40) &amp;amp; 0xFF);
    p[3] = static_cast&amp;lt;unsigned char&amp;gt;((v &amp;gt;&amp;gt; 32) &amp;amp; 0xFF);
    p[4] = static_cast&amp;lt;unsigned char&amp;gt;((v &amp;gt;&amp;gt; 24) &amp;amp; 0xFF);
    p[5] = static_cast&amp;lt;unsigned char&amp;gt;((v &amp;gt;&amp;gt; 16) &amp;amp; 0xFF);
    p[6] = static_cast&amp;lt;unsigned char&amp;gt;((v &amp;gt;&amp;gt;  8) &amp;amp; 0xFF);
    p[7] = static_cast&amp;lt;unsigned char&amp;gt;( v        &amp;amp; 0xFF);
}

static vector&amp;lt;unsigned char&amp;gt; sha1_hash(const unsigned char* data, size_t len)
{
    vector&amp;lt;unsigned char&amp;gt; msg(data, data + len);
    uint64_t              bit_len = static_cast&amp;lt;uint64_t&amp;gt;(len) * 8;

    msg.push_back(0x80);
    while ((msg.size() % 64) != 56) msg.push_back(0x00);

    unsigned char lenbuf[8];
    store_be64(bit_len, lenbuf);
    msg.insert(msg.end(), lenbuf, lenbuf + 8);

    uint32_t h0 = 0x67452301;
    uint32_t h1 = 0xEFCDAB89;
    uint32_t h2 = 0x98BADCFE;
    uint32_t h3 = 0x10325476;
    uint32_t h4 = 0xC3D2E1F0;

    for (size_t chunk = 0; chunk &amp;lt; msg.size(); chunk += 64)
    {
        uint32_t w[80];
        for (int i = 0; i &amp;lt; 16; ++i)
            w[i] = load_be32(&amp;amp;msg[chunk + i * 4]);
        for (int i = 16; i &amp;lt; 80; ++i)
            w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);

        uint32_t a = h0, b = h1, c = h2, d = h3, e = h4;

        for (int i = 0; i &amp;lt; 80; ++i)
        {
            uint32_t f, k;
            if (i &amp;lt; 20)
            {
                f = (b &amp;amp; c) | ((~b) &amp;amp; d);
                k = 0x5A827999;
            }
            else if (i &amp;lt; 40)
            {
                f = b ^ c ^ d;
                k = 0x6ED9EBA1;
            }
            else if (i &amp;lt; 60)
            {
                f = (b &amp;amp; c) | (b &amp;amp; d) | (c &amp;amp; d);
                k = 0x8F1BBCDC;
            }
            else
            {
                f = b ^ c ^ d;
                k = 0xCA62C1D6;
            }

            uint32_t temp = rotl32(a, 5) + f + e + k + w[i];

            e = d;
            d = c;
            c = rotl32(b, 30);
            b = a;
            a = temp;
        }

        h0 += a;
        h1 += b;
        h2 += c;
        h3 += d;
        h4 += e;
    }

    vector&amp;lt;unsigned char&amp;gt; out(20);
    store_be32(h0, &amp;amp;out[ 0]);
    store_be32(h1, &amp;amp;out[ 4]);
    store_be32(h2, &amp;amp;out[ 8]);
    store_be32(h3, &amp;amp;out[12]);
    store_be32(h4, &amp;amp;out[16]);
    return out;
}

static const JEVP_MD g_sha1   = {JEVP_MD::SHA1, 20, 64};

const JEVP_MD* JEVP_sha1() { return &amp;amp;g_sha1; }

unsigned char* JHMAC(const JEVP_MD* JEVP_MD, const void* key, int key_len, const unsigned char* data, size_t data_len, unsigned char* md, unsigned int* md_len)
{
    if (!JEVP_MD || !key || key_len &amp;lt; 0 || !data)
        return nullptr;

    static thread_local unsigned char static_md[64];

    if (!md)
        md = static_md;

    vector&amp;lt;unsigned char&amp;gt; key_block(JEVP_MD-&amp;gt;block_size, 0);
    const unsigned char*  key_bytes = static_cast&amp;lt;const unsigned char*&amp;gt;(key);

    vector&amp;lt;unsigned char&amp;gt; real_key;
    if (key_len &amp;gt; JEVP_MD-&amp;gt;block_size)
    {
        real_key = sha1_hash(key_bytes, static_cast&amp;lt;size_t&amp;gt;(key_len));
        std::copy(real_key.begin(), real_key.end(), key_block.begin());
    }
    else
    {
        std::copy(key_bytes, key_bytes + key_len, key_block.begin());
    }

    vector&amp;lt;unsigned char&amp;gt; ipad(JEVP_MD-&amp;gt;block_size);
    vector&amp;lt;unsigned char&amp;gt; opad(JEVP_MD-&amp;gt;block_size);
    for (int i = 0; i &amp;lt; JEVP_MD-&amp;gt;block_size; ++i)
    {
        ipad[i] = static_cast&amp;lt;unsigned char&amp;gt;(key_block[i] ^ 0x36);
        opad[i] = static_cast&amp;lt;unsigned char&amp;gt;(key_block[i] ^ 0x5C);
    }

    vector&amp;lt;unsigned char&amp;gt; inner;
    inner.reserve(JEVP_MD-&amp;gt;block_size + data_len);
    inner.insert(inner.end(), ipad.begin(), ipad.end());
    inner.insert(inner.end(), data, data + data_len);

    vector&amp;lt;unsigned char&amp;gt; inner_hash = sha1_hash(inner.data(), inner.size());

    vector&amp;lt;unsigned char&amp;gt; outer;
    outer.reserve(JEVP_MD-&amp;gt;block_size + inner_hash.size());
    outer.insert(outer.end(), opad.begin(), opad.end());
    outer.insert(outer.end(), inner_hash.begin(), inner_hash.end());

    vector&amp;lt;unsigned char&amp;gt; final_hash = sha1_hash(outer.data(), outer.size());

    std::memcpy(md, final_hash.data(), final_hash.size());
    if (md_len) *md_len = static_cast&amp;lt;unsigned int&amp;gt;(final_hash.size());

    return md;
}

//  Base32 decoding table.
/* clang-format off */
const int base32_reverse_map[128] =
{
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 15
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 31
    255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, // 47
//      (49)   2    3    4    5    6    7  (56)
    255, 255,  26,  27,  28,  29,  30,  31, 255, 255, 255, 255, 255, 255, 255, 255, // 63
// (64)   A    B    C    D    E    F    G    H    I    J    K    L    M    N    O
    255,   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14, // 79
//   P    Q    R    S    T    U    V    W    X    Y    Z  (91)
    15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255, 255, // 95
// (96)   A    B    C    D    E    F    G    H    I    J    K    L    M    N    O  // lower case instead
    255,   0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,  14, // 111
//   P    Q    R    S    T    U    V    W    X    Y    Z (123)                     // lower case instead
    15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255, 255  // 127
};
/* clang-format on */

//  Base32 decoding function.
vector&amp;lt;unsigned char&amp;gt; base32_decode(const string&amp;amp; base32)
{
    queue&amp;lt;int&amp;gt; q_rev;
    for (const char&amp;amp; c : base32)
    {
        if (c &amp;lt;= 49 || (c &amp;gt;= 56 &amp;amp;&amp;amp; c &amp;lt;= 64) || (c &amp;gt;= 91 &amp;amp;&amp;amp; c &amp;lt;= 96) || c &amp;gt;= 123)
        { //    Illegal lower case will be treat as upper case thanks to base32_reverse_map.
            if (c == '=') break;
        }
        else
        {
            q_rev.push(base32_reverse_map[(int)c]); //  Get integers of base32.
        }
    }
    queue&amp;lt;bool&amp;gt; q_bin;
    while (!q_rev.empty()) //   Transform the integers into bits and queue them up.
    {
        int now = q_rev.front(); // Get a integer from queue.
        q_rev.pop();
        stack&amp;lt;bool&amp;gt; sb;
        for (int i = 0; i &amp;lt; 5; i++) //  Push the bits of this integer into stack
        {                           //  5 bits into 1 group
            sb.push(now &amp;amp; 1);       //  Push the last bit of this integer into stack.
            now &amp;gt;&amp;gt;= 1;              //  Flush the last bit.
        }
        while (!sb.empty()) //  Flush the bits of this integer into binary queue.
        {
            q_bin.push(sb.top()); //    Pop bits from stack into binary queue.
            sb.pop();
        }
    }
    vector&amp;lt;unsigned char&amp;gt; uc;
    while (!q_bin.empty()) //   Transform the bits into bytes.
    {
        int thehex = 0;
        for (int i = 0; i &amp;lt; 8; i++) //  Read a byte from the bits queue.
        {
            if (q_bin.empty())
            {                     //    If the bits queue is empty now,
                thehex &amp;lt;&amp;lt;= 8 - i; //    set 0 on the last bits of a byte.
                break;
            }
            thehex &amp;lt;&amp;lt;= 1;                 //    Make the space for the new bit.
            thehex |= (int)q_bin.front(); //    Push this bit into byte.
            q_bin.pop();
        }
        uc.push_back((unsigned char)thehex);
    }

    if (uc.back() == 0) uc.pop_back();     //   TOTP may abandon last zero.
    if (uc.size() % 2 != 0) uc.pop_back(); //   TOTP may abandon odd res.
    return uc;
}

//  Get Unix Time.
time_t getUnixTime()
{
    return time(0);
}

//  Calculate the TOTP time signature.
time_t getTimeSign(int refreshPeriod, time_t specUnixTime)
{
    time_t outTime = specUnixTime / refreshPeriod;
    return outTime;
}

//  Transform time_t aka &amp;lt;long long&amp;gt; to bytes.
vector&amp;lt;unsigned char&amp;gt; llong2bytes(time_t tt)
{
    uint64_t value = static_cast&amp;lt;uint64_t&amp;gt;(tt);

    vector&amp;lt;unsigned char&amp;gt; bytes(8);

    for (int i = 7; i &amp;gt;= 0; --i)
    {
        bytes[i] = static_cast&amp;lt;unsigned char&amp;gt;(value &amp;amp; 0xFF);
        value &amp;gt;&amp;gt;= 8;
    }

    return bytes;
}

//  Fast Exponentiation algorithm.
unsigned fastpow(unsigned base, unsigned index)
{
    if (index == 0) return 1;
    unsigned t = 1;
    while (index != 0)
    {
        if (index &amp;amp; 1) t *= base;
        index &amp;gt;&amp;gt;= 1;
        base *= base;
    }
    return t;
}

//  Calculate TOTP according to RFC standard.
std::string hash_to_TOTP(const unsigned char* hmac, unsigned int length, int digit)
{
    int offset = hmac[length - 1] &amp;amp; 0x0F;

    uint32_t bin_code = ((hmac[offset]     &amp;amp; 0x7F) &amp;lt;&amp;lt; 24) |
                        ((hmac[offset + 1] &amp;amp; 0xFF) &amp;lt;&amp;lt; 16) |
                        ((hmac[offset + 2] &amp;amp; 0xFF) &amp;lt;&amp;lt; 8)  |
                        ( hmac[offset + 3] &amp;amp; 0xFF);

    uint32_t mod = 1;
    for (int i = 0; i &amp;lt; digit; ++i) mod *= 10;

    uint32_t otp = bin_code % mod;

    std::ostringstream oss;
    oss &amp;lt;&amp;lt; std::setw(digit) &amp;lt;&amp;lt; std::setfill('0') &amp;lt;&amp;lt; otp;
    return oss.str();
}

std::string TOTP::GenerateTOTP(const std::string&amp;amp; secret)
{
    return GenerateTOTP(6, 30, HMAC_SHA1, secret, getUnixTime());
}

std::string TOTP::GenerateTOTP(int digit, int refreshSeconds, int algorithm, const std::string&amp;amp; secret, int specTime)
{
    vector&amp;lt;unsigned char&amp;gt; h_msg = llong2bytes(getTimeSign(refreshSeconds, specTime)); //    Get current time signature and transform time_t aka &amp;lt;long long&amp;gt; to bytes.
    vector&amp;lt;unsigned char&amp;gt; h_key = base32_decode(secret); // Transform base32 encoded secret key to decoded bytes.

    unsigned char* out_hmac = new unsigned char[algorithm]; //  Perform openssl function to calculate HMAC-SHA1 hash.
    unsigned int   out_hmac_length;
    const JEVP_MD* emd;
    emd = JEVP_sha1();
    JHMAC(emd, h_key.data(), (int)h_key.size(), h_msg.data(), (int)h_msg.size(), out_hmac, &amp;amp;out_hmac_length);

    string fine = hash_to_TOTP(out_hmac, out_hmac_length, digit);
    delete[] out_hmac;

    return fine; // Calculate TOTP according to RFC standard.
}

std::string TOTP::currentTOTP()
{
    return GenerateTOTP(digit, refreshSeconds, algorithm, secret, getUnixTime());
}
&lt;/code&gt;&lt;/pre&gt;
&lt;img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=76104&amp;amp;HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager"&gt;</content:encoded>
	

					<wfw:commentRss>https://shkspr.mobi/blog/2026/10/una-watch-totp-mfa-on-your-wrist/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title><![CDATA[Una Watch - SDK and Writing Your First App]]></title>
		<link>https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/</link>
					<comments>https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/#respond</comments>
				<dc:creator><![CDATA[Terence Eden]]></dc:creator>
		<pubDate>Tue, 06 Oct 2026 11:34:25 +0000</pubDate>
		<category><![CDATA[c++]]></category><category><![CDATA[HowTo]]></category><category><![CDATA[programming]]></category><category><![CDATA[UnaWatch]]></category>		<guid isPermaLink="false">https://shkspr.mobi/blog/?p=76095</guid>

		
		<description>I don't know why hardware manufacturers find it so hard to write decent documentation. As I've said before you need to actually test your readme.  The Una Watch development instructions are scattered across a bunch of different files.  So here's how to go from zero to your first "Hello, World!" Glance app with the minimum of fuss.  This all assumes a fairly modern Debian / Ubuntu / Pop OS Linux.  …</description>
			
		<content:encoded>&lt;p&gt;I don't know why hardware manufacturers find it so hard to write decent documentation. As I've said before &lt;a href="https://shkspr.mobi/blog/2025/10/how-to-actually-test-your-readme/"&gt;you need to &lt;em&gt;actually&lt;/em&gt; test your readme&lt;/a&gt;.  The Una Watch development instructions are scattered across a bunch of different files.&lt;/p&gt;

&lt;p&gt;So here's how to go from zero to your first "Hello, World!" Glance app with the minimum of fuss.&lt;/p&gt;

&lt;p&gt;This all assumes a fairly modern Debian / Ubuntu / Pop OS Linux.&lt;/p&gt;

&lt;h2 id="prerequisites"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/#prerequisites"&gt;Prerequisites&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;This installs most of the tools you will need:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-bash"&gt;sudo apt update
sudo apt install -y git python3 python3-pip cmake build-essential
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id="stm32cubeclt"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/#stm32cubeclt"&gt;STM32CubeCLT&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Visit &lt;a href="https://www.st.com/en/development-tools/stm32cubeclt.html"&gt;https://www.st.com/en/development-tools/stm32cubeclt.html&lt;/a&gt; and download the Linux version of the tool. Inexplicably, you need to give your email address. I recommend giving a disposable one from &lt;a href="https://10minutemail.com/"&gt;https://10minutemail.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After downloading, unzip the file.&lt;/p&gt;

&lt;p&gt;On the command line, &lt;code&gt;cd&lt;/code&gt; into the directory containing the unzipped file.&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-bash"&gt;chmod +x stm32cubeclt_*.sh
sudo ./stm32cubeclt_*.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You will need to agree to their licence. Just hit &lt;code&gt;y&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That installs everything in &lt;code&gt;/opt/st/stm32cubeclt_1.22.0/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You will need to make that available to your PATH.&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-bash"&gt;export PATH="/opt/st/stm32cubeclt_1.22.0/GNU-tools-for-STM32/bin:$PATH"
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id="clone-the-repo"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/#clone-the-repo"&gt;Clone the repo&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;You will need to copy &lt;a href="https://github.com/UNAWatch/una-sdk/"&gt;the Una SDK&lt;/a&gt;. I keep my git repos in &lt;code&gt;~/git/&lt;/code&gt; - you will need the location of the directory later.&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-bash"&gt;cd ~/git/
git clone git@github.com:UNAWatch/una-sdk.git
cd una-sdk/
git submodule update --init ThirdParty/lvgl
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then update all the Python dependencies.&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-bash"&gt;export UNA_SDK="$PWD"
python3 -m pip install --upgrade pip
python3 -m pip install -r "$UNA_SDK/Utilities/Scripts/app_packer/requirements.txt"
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id="check-it-has-all-worked"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/#check-it-has-all-worked"&gt;Check it has all worked&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Paste this into your terminal. Check for any errors.&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-bash"&gt;which arm-none-eabi-gcc || true
which cmake || true
which make || true
which python3 || true
python3 -m pip --version
arm-none-eabi-gcc --version || true
cmake --version || true
make --version || true
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id="build-the-demo-app"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/#build-the-demo-app"&gt;Build the demo app&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;If all of the above has worked, you should be able to build the demo app with:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-bash"&gt;cd /tmp
mkdir -p MyAlarm
cp -r "$UNA_SDK/Examples/Apps/Alarm/"* MyAlarm
mkdir -p MyAlarm/build
cmake -G "Unix Makefiles" -S MyAlarm/Software/Apps/Alarm-CMake -B MyAlarm/build
cmake --build MyAlarm/build
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id="make-it-permanent"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/#make-it-permanent"&gt;Make it permanent&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;Store the paths permanently. If you put cloned the Una SDK to somewhere other than &lt;code&gt;~/git/&lt;/code&gt; you'll need to edit this next bit of code:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-bash"&gt;echo 'export UNA_SDK="$HOME/git/una-sdk"' &amp;gt;&amp;gt; ~/.bashrc
echo 'export PATH="/opt/st/stm32cubeclt_1.22.0/GNU-tools-for-STM32/bin:$PATH"' &amp;gt;&amp;gt; ~/.bashrc
source ~/.bashrc
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id="build-a-glance-app"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/#build-a-glance-app"&gt;Build a Glance app&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;At the moment, the only way to build a GUI app is using the Windows only (Booooo!) TouchGFX&lt;sup id="fnref:windoze"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/#fn:windoze" class="footnote-ref" title="Just after I wrote this, the SDK was updated to allow LVGL which should work on Linux. There's no documentation for it yet that I could find." role="doc-noteref"&gt;0&lt;/a&gt;&lt;/sup&gt;. So, instead, we'll make a "Hello World" glance. All it will do is draw some text.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;GlanceFloors&lt;/code&gt; demo app is the simplest one to copy.&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-bash"&gt;cd $UNA_SDK/Examples/Apps/
cp -r  GlanceFloors/ GlanceHello
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Use an IDE or a text editor to open up the new GlanceHello directory.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rename the directory &lt;code&gt;GlanceHello/Software/App/GlanceFloors-CMake/&lt;/code&gt; to &lt;code&gt;GlanceHello/Software/App/GlanceHello-CMake/&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Edit the file &lt;code&gt;GlanceHello/Software/App/GlanceFloors-CMake/CMakeLists.txt&lt;/code&gt; - specifically the &lt;code&gt;# App configuration&lt;/code&gt; section.&lt;/li&gt;
&lt;li&gt;Change the &lt;code&gt;APP_NAME&lt;/code&gt; to &lt;code&gt;GlanceHello&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Change the &lt;code&gt;APP_USER_NAME&lt;/code&gt; to &lt;code&gt;Hello&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a demo, you can leave the &lt;code&gt;DEV_ID&lt;/code&gt; and &lt;code&gt;APP_ID&lt;/code&gt; as their default values.&lt;/p&gt;

&lt;h2 id="minimum-viable-app"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/#minimum-viable-app"&gt;Minimum viable app&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;You will need to edit two more files.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;GlanceHello/Software/Libs/Header/Service.hpp&lt;/code&gt; remove the following lines:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-CPP"&gt;SDK::Sensor::Connection mSensorFloors;
uint32_t                mFloorsValue;
bool                    mDataReceived;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then save the file.&lt;/p&gt;

&lt;p&gt;Edit &lt;code&gt;GlanceHello/Software/Libs/Source/Service.cpp&lt;/code&gt; and replace its entire contents with this:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-CPP"&gt;#include "Service.hpp"
#include "SDK/Messages/CommandMessages.hpp"
#include "SDK/Messages/MessageGuard.hpp"

Service::Service(SDK::Kernel &amp;amp;kernel)
    : mKernel(kernel)
    , mGlanceUI()
    , mGlanceTitle()
    , mGlanceValue()
{}

Service::~Service(){}

void Service::run()
{
    while (true) {
        SDK::MessageBase *msg;
        if(!mKernel.comm.getMessage(msg)) {
            continue;
        }

        switch (msg-&amp;gt;getType()) {

            case SDK::MessageType::EVENT_GLANCE_START:
                if (configGui()) {
                    createGuiControls();
                } else {
                    mKernel.comm.releaseMessage(msg);
                    mKernel.sys.exit(0);
                }
                break;

            case SDK::MessageType::COMMAND_APP_STOP:
            case SDK::MessageType::EVENT_GLANCE_STOP:
                mKernel.comm.releaseMessage(msg);
                return;
                break;

            case SDK::MessageType::EVENT_GLANCE_TICK:
                onGlanceTick();
                break;

            default:
                break;
        }

        mKernel.comm.releaseMessage(msg);
    }
}

void Service::onGlanceTick()
{
    if (mGlanceUI.isInvalid()) {
        if (auto upd = SDK::make_msg&amp;lt;SDK::Message::RequestGlanceUpdate&amp;gt;(mKernel)) {
            upd-&amp;gt;name           = APP_NAME;
            upd-&amp;gt;controls       = mGlanceUI.data();
            upd-&amp;gt;controlsNumber = static_cast&amp;lt;uint32_t&amp;gt;(mGlanceUI.size());
            upd.send(100);
        }

        mGlanceUI.setValid();
}
}

bool Service::configGui()
{
    bool status = false;
    if (auto gc = SDK::make_msg&amp;lt;SDK::Message::RequestGlanceConfig&amp;gt;(mKernel)) {
        if (gc.send(100) &amp;amp;&amp;amp; gc.ok()) {
            if (gc-&amp;gt;maxControls &amp;gt;= 3) {
                mGlanceUI.setWidth(gc-&amp;gt;width);
                mGlanceUI.setHeight(gc-&amp;gt;height);
                status = true;
            }
        }
    }
    return status;
}

void Service::createGuiControls()
{
    mGlanceTitle = mGlanceUI.createText();
    mGlanceTitle.pos({ kTitleX, kTitleY }, { kTitleW, kTitleH })
        .font(GlanceFont_t::GLANCE_FONT_POPPINS_SEMIBOLD_20)
        .color(GlanceColor_t::GLANCE_COLOR_TEAL)
        .setText("Hello, World!")
        .alignment(GlanceAlignH_t::GLANCE_ALIGN_H_CENTER);
    mGlanceValue = mGlanceUI.createText();
    mGlanceValue.pos({ 0, kValueY }, { 200, kValueH })
        .font(GlanceFont_t::GLANCE_FONT_POPPINS_SEMIBOLD_20)
        .color(GlanceColor_t::GLANCE_COLOR_WHITE)
        .setText("I know C++")
        .alignment(GlanceAlignH_t::GLANCE_ALIGN_H_CENTER);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Save the file.&lt;/p&gt;

&lt;h2 id="compile-it"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/#compile-it"&gt;Compile it&lt;/a&gt;&lt;/h2&gt;

&lt;p&gt;On the command line&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-bash"&gt;cd $UNA_SDK/Examples/Apps/GlanceHello/Software/App/GlanceHello-CMake
cmake -G "Unix Makefiles" -S . -B build
cmake --build build
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It should finish with a success message of &lt;code&gt;[100%] Built target GlanceHelloApp&lt;/code&gt;&lt;/p&gt;

&lt;h2 id="copying-it-to-your-watch"&gt;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/#copying-it-to-your-watch"&gt;Copying it to your watch&lt;/a&gt;&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Plug your watch into your computer with a USB-C cable.&lt;/li&gt;
&lt;li&gt;On your computer, open the watch's filesystem.&lt;/li&gt;
&lt;li&gt;Open the &lt;code&gt;Apps&lt;/code&gt; directory.&lt;/li&gt;
&lt;li&gt;Create a new directory.&lt;/li&gt;
&lt;li&gt;Name the new directory with the string of characters from the &lt;code&gt;APP_ID&lt;/code&gt; which is set in &lt;code&gt;CMakeLists.txt&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The app is stored in &lt;code&gt;GlanceHello/Output/&lt;/code&gt; and will be called &lt;code&gt;Hello_1.5.0-rc5.uapp&lt;/code&gt; or similar.&lt;/li&gt;
&lt;li&gt;Copy the &lt;code&gt;.uapp&lt;/code&gt; file from your computer to the directory you just created on the watch.&lt;/li&gt;
&lt;li&gt;Eject the watch.&lt;/li&gt;
&lt;li&gt;Unplug the cable.&lt;/li&gt;
&lt;li&gt;Restart the watch by pressing the "Up" button until you get to "Power Off", then press "Select", then press "Select" again.&lt;/li&gt;
&lt;li&gt;Turn the watch on by holding down "Select".&lt;/li&gt;
&lt;li&gt;Once the watch has fully restarted, press the "Down" button until you see the "Hello" glance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If it has worked, you should see something like this:&lt;/p&gt;

&lt;img src="https://shkspr.mobi/blog/wp-content/uploads/2026/09/cpp.webp" alt="A watch displaying &amp;quot;Hello World&amp;quot; and &amp;quot;I know C++&amp;quot;. The latter if off centre because I don't know C++." width="1926" height="2048" class="aligncenter"&gt;

&lt;p&gt;Hurrah! Treat yourself to a beer 🍻&lt;/p&gt;

&lt;div id="footnotes" role="doc-endnotes"&gt;
&lt;hr aria-label="Footnotes"&gt;
&lt;ol start="0"&gt;

&lt;li id="fn:windoze"&gt;
&lt;p&gt;Just after I wrote this, the SDK was updated to allow &lt;a href="https://lvgl.io/"&gt;LVGL&lt;/a&gt; which should work on Linux. There's no documentation for it yet that I could find.&amp;nbsp;&lt;a href="https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/#fnref:windoze" class="footnote-backref" role="doc-backlink"&gt;↩︎&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;
&lt;/div&gt;
&lt;img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=76095&amp;amp;HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager"&gt;</content:encoded>
	

					<wfw:commentRss>https://shkspr.mobi/blog/2026/10/una-watch-sdk-and-writing-your-first-app/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title><![CDATA[How to make the Watchy vibrate]]></title>
		<link>https://shkspr.mobi/blog/2023/07/how-to-make-the-watchy-vibrate/</link>
					<comments>https://shkspr.mobi/blog/2023/07/how-to-make-the-watchy-vibrate/#comments</comments>
				<dc:creator><![CDATA[Terence Eden]]></dc:creator>
		<pubDate>Sun, 09 Jul 2023 11:34:42 +0000</pubDate>
		<category><![CDATA[arduino]]></category><category><![CDATA[c++]]></category><category><![CDATA[HowTo]]></category><category><![CDATA[tutorial]]></category><category><![CDATA[watchy]]></category>		<guid isPermaLink="false">https://shkspr.mobi/blog/?p=46247</guid>

		
		<description>I am enjoying playing with the eInk Watchy. It is a cute package and is everything I want in a Smart-Watch; geeky, long battery life, and not obnoxious.  But - fuck me! - the documentation is atrocious! Well, that's a lie. There is no documentation. It has the "Chat to us on Discord" anti-pattern that infects so many otherwise great projects.  So I'm left to figure out how to make the Watchy's…</description>
			
		<content:encoded>&lt;p&gt;I am enjoying playing with the eInk Watchy. It is a cute package and is everything I want in a Smart-Watch; geeky, long battery life, and not obnoxious.&lt;/p&gt;

&lt;p&gt;But - fuck me! - the documentation is atrocious! Well, that's a lie. There is no documentation. It has the "Chat to us on Discord" anti-pattern that infects so many otherwise great projects.&lt;/p&gt;

&lt;p&gt;So I'm left to figure out how to make the Watchy's haptics work.&lt;/p&gt;

&lt;p&gt;The example watchfaces have a file called &lt;code&gt;settings.h&lt;/code&gt; which contains &lt;a href="https://github.com/sqfmi/Watchy/blob/342eb48a497eb3dcb1ec03c5c156937fdec07ba9/examples/WatchFaces/7_SEG/settings.h#L24"&gt;&lt;code&gt;.vibrateOClock = true&lt;/code&gt;&lt;/a&gt;. Set that and you'll get a little buzz on the hour, &lt;em&gt;every&lt;/em&gt; hour.&lt;/p&gt;

&lt;p&gt;But how do you make it buzz when you've hit your step-count goal? Or when the time is 13:37? Or whenever &lt;em&gt;you&lt;/em&gt; want?&lt;/p&gt;

&lt;p&gt;There's no documentation. And searching the code for "vibrate" or "vibe" or "vibration" yielded nothing.  But I accidentally typo'd my search and entered "vib" - and that did the trick.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;config.h&lt;/code&gt; there's this line: &lt;a href="https://github.com/sqfmi/Watchy/blob/342eb48a497eb3dcb1ec03c5c156937fdec07ba9/src/config.h#L25"&gt;&lt;code&gt;#define VIB_MOTOR_PIN 13&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So, pin 13 is wired in to the vibration motor. We can add that line to our own config file. But how do we turn the buzzer on and off?&lt;/p&gt;

&lt;p&gt;With a bit of help from Discord, it became clear:&lt;/p&gt;

&lt;pre&gt;&lt;code class="language-c"&gt;pinMode(VIB_MOTOR_PIN, OUTPUT);     // Set vibration motor pin as an output.
digitalWrite(VIB_MOTOR_PIN, true);  // Enable vibration motor
delay(150);                         // Vibe for 150 ms.
digitalWrite(VIB_MOTOR_PIN, false); // Disable vibration motor.
delay(500);                         // Wait half a second
digitalWrite(VIB_MOTOR_PIN, true);  // Enable vibration motor
delay(250);                         // Vibe for quarter of a second
digitalWrite(VIB_MOTOR_PIN, false); // Stop vibrating
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That's the same as the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/vibrate"&gt;HTML5 Vibrate API&lt;/a&gt; - you can't control the intensity, but you can &lt;a href="https://shkspr.mobi/blog/2014/01/malicious-use-of-the-html5-vibrate-api/"&gt;program your own vibration patterns&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I just don't understand why hardware vendors are &lt;em&gt;so&lt;/em&gt; crap at writing documentation. Don't they like people using their products?&lt;/p&gt;
&lt;img src="https://shkspr.mobi/blog/wp-content/themes/edent-wordpress-theme/info/okgo.php?ID=46247&amp;amp;HTTP_REFERER=RSS" alt="" width="1" height="1" loading="eager"&gt;</content:encoded>
	

					<wfw:commentRss>https://shkspr.mobi/blog/2023/07/how-to-make-the-watchy-vibrate/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
