Socket Demo
This section describes the architecture and extension points of the socket-demo.
Sensor data protocol
Each sample is a single line, ASCII hex, 18 hex chars total, then a newline:
id: 2-byte device ID (last two bytes of MAC), big-endian, printed as 4 hex charsaX aY aZ: accelerometer bytes (encoding 0..255; 128 ≈ 0 g)rssi*: beacon RSSI bytes (0..255). If unknown, devices may send FF.
Example: 0D4A80A27FFF00010203\n
Command protocol quick reference
L\nlist connected device IDs (one per line)I\nrequest IDs from all devicesC<id><RR><GG><BB>\nset LED color on<id>M<id><SS><TT>\nvibrate motor on<id>with strengthSSforTTticksR<id>\none-shot sensor sample from<id>-
R<id><FF>\nstream samples atFFHz from<id>;00stops -
Scenes:
Scenebase class and concrete scenes undersocket-demo/scenes/ - Manager:
SceneManagerorchestrates which scene is active - Devices:
HitloopDeviceandHitloopDeviceManager(served via CDN) handle device data over WebSocket - How-tos: read device state, add a new Scene
Architecture
- WebSocket messages are handled by
HitloopDeviceManager, which maintains a map ofHitloopDeviceobjects keyed by device id. - The p5 draw loop delegates to a
SceneManagerthat callsdraw()on the activeScene. - Scenes are modular files under
socket-demo/scenes/and extend theScenebase class. - Navigation between scenes is provided by
SceneManager.nextScene()andSceneManager.previousScene(); the demo maps these to the right/left arrow keys.
File layout
socket-demo/index.html: includes p5 libraries, shared device classes from the CDN, localScene.js,SceneManager.js, your scene files, andsketch.js.socket-demo/sketch.js: wires up theHitloopDeviceManager(WebSocket URL), registers scenes withSceneManager, connects, and starts the default scene.socket-demo/Scene.js: minimal base class providingsetup()anddraw()plus access to thedeviceManager.socket-demo/SceneManager.js: manages scene registration, switching, and ordered traversal.socket-demo/scenes/*.js: concrete scenes such asDeviceListScene,FirstDeviceDetailsScene,GridHeatmapScene.
Shared files via CDN
The demo relies on shared libraries delivered by a CDN:
p5.min.js,p5.sound.min.jsHitloopDevice.js,HitloopDeviceManager.js
They are referenced in index.html like:
<script src="http://cdn.hitloop.feib.nl/js/p5.min.js"></script>
<script src="http://cdn.hitloop.feib.nl/js/p5.sound.min.js"></script>
<script src="http://cdn.hitloop.feib.nl/js/HitloopDevice.js"></script>
<script src="http://cdn.hitloop.feib.nl/js/HitloopDeviceManager.js"></script>
These CDN files are served by the cdn-server service in this repository and kept consistent across demos.