rpi4-osdev/part8-breakout-ble
2021-02-15 20:25:16 +00:00
..
controller part8 working - but gfx is way too slow with unoptimised clang 2021-02-08 16:06:09 +00:00
BCM4345C0.hcd A rough BLE controlled Breakout game in part8 2020-08-11 22:08:40 +01:00
boot.S A rough BLE controlled Breakout game in part8 2020-08-11 22:08:40 +01:00
bt.c Fixed typos 2021-02-15 17:26:21 +00:00
bt.h part8 working - but gfx is way too slow with unoptimised clang 2021-02-08 16:06:09 +00:00
fb.c part8 no works with DEBUG=0 in fb.c, but Eddystone beacon in part7 does not 2021-02-14 20:32:02 +00:00
fb.h part8 working - but gfx is way too slow with unoptimised clang 2021-02-08 16:06:09 +00:00
io.c A rough BLE controlled Breakout game in part8 2020-08-11 22:08:40 +01:00
io.h part8 working - but gfx is way too slow with unoptimised clang 2021-02-08 16:06:09 +00:00
kernel.c part8 no works with DEBUG=0 in fb.c, but Eddystone beacon in part7 does not 2021-02-14 20:32:02 +00:00
link.ld A rough BLE controlled Breakout game in part8 2020-08-11 22:08:40 +01:00
Makefile Updated part2 to explain Makefiles on other platforms 2021-02-12 20:21:45 +00:00
Makefile.gcc part8 working - but gfx is way too slow with unoptimised clang 2021-02-08 16:06:09 +00:00
Makefile.gcc.windows part8 working with optimisation, but not with DEBUG=0 in fb.c 2021-02-11 22:14:31 +00:00
mb.c A rough BLE controlled Breakout game in part8 2020-08-11 22:08:40 +01:00
mb.h A rough BLE controlled Breakout game in part8 2020-08-11 22:08:40 +01:00
README.md Added documentation on building the Breakout controller 2021-02-15 20:25:16 +00:00
terminal.h part8 no works with DEBUG=0 in fb.c, but Eddystone beacon in part7 does not 2021-02-14 20:32:02 +00:00

Writing a "bare metal" operating system for Raspberry Pi 4 (Part 8)

Receiving Bluetooth data

So we've mastered advertising, and we're broadcasting data out into the World. But that's only half the story! In this part we'll be exploring how to receive data from an external source. This is much more exciting as we can begin to use other devices as controllers.

In fact, my idea was to control the Breakout game in part6 by receiving data from trackpad on my MacBook Pro over Bluetooth! Neat, eh?

Building a Bluetooth Breakout controller

Let's first build the controller code for broadcasting from the laptop. We essentially need to create a BLE peripheral in code.

We don't need to build everything from scratch since we're not on bare metal any more! I used the Bleno library, which is extremely popular for this kind of work. It requires some Node.js knowledge, but I didn't have any before I started and so I'm sure you'll be fine too.

Once you've got Node.js installed, use npm to install Bleno with npm install bleno. Because I'm on a Mac and running a recent version of Mac OS X (> Catalina), I needed to do this using these three steps instead:

  • npm install github:notjosh/bleno#inject-bindings
  • npm install github:notjosh/bleno-mac
  • npm install github:sandeepmistry/node-xpc-connection#pull/26/head

I made some simple modifications to the echo example in the Bleno repository. This example implements a Bluetooth peripheral, exposing a service which:

  • lets a connected device read a locally stored byte value
  • lets a connected device update the locally stored byte value (it can be changed locally too...!)
  • lets a connected device subscribe to receive updates when the locally stored byte value changes
  • lets a connected device unsubscribe from receiving updates

You won't be surprised to know that my design is for our Raspberry Pi to subscribe to receive updates from this service. That locally stored byte value will be updated locally to reflect the current mouse cursor position as it changes. Our Raspberry Pi will then be notified every time I move the mouse on my MacBook Pro as if by magic!

You can see the changes I made to the Bleno echo example to implement this in the controller subdirectory of this part8-breakout-ble. They boil down to making use of iohook, which I installed using npm install iohook. Here's the interesting bit (the rest is just plumbing):

var ioHook = require('iohook');

var buf = Buffer.allocUnsafe(1);
var obuf = Buffer.allocUnsafe(1);
const scrwidth = 1440;
const divisor = scrwidth / 100;

ioHook.on( 'mousemove', event => {
   buf.writeUInt8(Math.round(event.x / divisor), 0);

   if (Buffer.compare(buf, obuf)) {
      e._value = buf;
      if (e._updateValueCallback) e._updateValueCallback(e._value);
      buf.copy(obuf);
   }
});

ioHook.start();

Here I'm capturing the x coordinate of the mouse cursor and translating it into a number between 0 (far left of the screen) and 100 (far right of the screen). If it changes from the previous value, we update the callback value (our Raspberry Pi only needs to know when the position has changed). As the callback value is updated, so any subscribed devices will be notified.

And we have ourselves a working, albeit a bit hacky, Bluetooth game controller!

Todo

  • Write up scanning implementation (receiving advertising reports)
  • Write up device detection (service UUID & name matching)
  • Write up ACL notification of characteristic change (using bleno echo example)