Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
209 commits
Select commit Hold shift + click to select a range
28f82fc
Make recording duration configuratable and add experimental target
r59q Aug 13, 2025
c2b90af
Fix CSV formatting, add WIP button for printing to pdf
r59q Aug 18, 2025
3311f66
Add print iframe in new utility file
r59q Aug 19, 2025
6bdf496
Scale each printed recording to have matching y-axes
r59q Aug 19, 2025
ea7f548
prettier
r59q Aug 19, 2025
c4f1ebc
Add recording label to printed gestures
r59q Aug 19, 2025
8ef6c03
Add zezo-line, spdx identifier
r59q Aug 19, 2025
8d6b78e
Prettier
r59q Aug 19, 2025
c88a5c7
Move ChartDataset to lib
r59q Aug 19, 2025
edf0231
Add printable recordings feature
r59q Aug 19, 2025
3172ec1
Make the tick labels of printed gestures larger
r59q Aug 19, 2025
254180f
Fix fingerprint enabled check
r59q Aug 20, 2025
cb7a051
Remove unused code
r59q Aug 20, 2025
10048a3
Merge branch 'fingerprint-fix' into development
r59q Aug 20, 2025
fde6bb7
Merge pull request #652 from microbit-foundation/fingerprint-fix
Karlo-Emilo Aug 21, 2025
2167261
Update package.json to simple version
Karlo-Emilo Aug 21, 2025
3d39449
Merge branch 'production-math' into main
Karlo-Emilo Aug 21, 2025
6ca2255
Merge pull request #654 from microbit-foundation/Karlo-Emilo-patch-1
Karlo-Emilo Aug 21, 2025
0e0d37c
Add feature to show recording in dialog
r59q Aug 23, 2025
c8bac14
Make y-axis ticks on recording graph configurable
r59q Aug 23, 2025
e18cf2d
Merge pull request #651 from microbit-foundation/development
Karlo-Emilo Aug 25, 2025
313519e
Use highlighted axis for print display
r59q Aug 30, 2025
44be7bb
Add IconButton component
r59q Aug 30, 2025
611f678
Reorder license lotifier
r59q Aug 30, 2025
b579973
Use icon buttons
r59q Sep 1, 2025
a480296
Use more IconButton
r59q Sep 1, 2025
0cac1f3
Add types to stores
r59q Sep 18, 2025
800396e
Move vector to core
r59q Sep 18, 2025
ab89425
Move LiveDataVector to core
r59q Sep 18, 2025
d2aab4d
Add base vector to core
r59q Sep 18, 2025
647ebcb
Remove playground
r59q Sep 18, 2025
9813404
Move axis to core
r59q Sep 18, 2025
5d1d7e9
rename model to entities in core
r59q Sep 18, 2025
aed28d9
Move filters to core
r59q Sep 18, 2025
603e158
Move confidence to core
r59q Sep 23, 2025
1ea56f8
Rename Gesture to GestureState
r59q Sep 23, 2025
f43bfed
Remove duplicate confidence type
r59q Sep 23, 2025
37d3790
Move baselivedatavector
r59q Sep 23, 2025
1e8f12d
Move livedata buffer
r59q Sep 23, 2025
8291a45
Move ModelTrainer and ModelRegistry to core
r59q Sep 23, 2025
c6fbd99
Move repository interface into core
r59q Sep 24, 2025
68cf786
Move models to core
r59q Sep 24, 2025
25e16a3
Prettier
r59q Sep 24, 2025
a516ebb
Add classifier interface
r59q Sep 24, 2025
bcbcc6a
First batch of core shared logic
r59q Nov 24, 2025
1e603c3
Second batch of core shared logic
r59q Nov 24, 2025
91a3ab5
Fix compile issues for legacy components
r59q Nov 25, 2025
b4b5762
Add AppRoot to path
r59q Nov 25, 2025
1092e3f
Prettier lint
r59q Nov 25, 2025
c59aa30
Fix tests
r59q Nov 25, 2025
f666040
Move document title to app instead of app root
r59q Nov 26, 2025
c7a6340
Move error reconnect to controller
r59q Nov 26, 2025
f8f6f09
Prettier
r59q Nov 26, 2025
e3240f5
Add the makecode embed package
r59q Dec 7, 2025
d3bd8c7
Add makecode path
r59q Dec 9, 2025
994be93
Add makecode iframe
r59q Dec 9, 2025
11400e0
Add back button to embedded view
r59q Dec 15, 2025
b9fdc86
Remove unused imports
r59q Dec 15, 2025
79bc66b
Add 'To MakeCode' button
r59q Dec 16, 2025
1704409
Implement Feature controller
r59q Jan 5, 2026
e754d14
Make mlmachine set itself up for better dependency management
r59q Jan 5, 2026
3c153b7
Fix identifier tests
r59q Jan 5, 2026
cd08bb0
Prettier
r59q Jan 5, 2026
e68b36e
Create output service
r59q Jan 7, 2026
7dcb8cf
Update license identifier
r59q Jan 7, 2026
c52b770
Add states repository source
r59q Jan 7, 2026
6de86d1
Opening makecode page now sets override state
r59q Jan 7, 2026
c897cbc
Improve tests and localstorage keyval store
r59q Jan 8, 2026
8131550
Split up devices into Devices and Connection
r59q Jan 8, 2026
9eb6d5e
Split up devices and move some of the logic to the controller
r59q Jan 10, 2026
1ff2f5b
Change dev overlay to be event transparent
r59q Jan 10, 2026
d8ec098
Add makecode project saving/loading
r59q Jan 11, 2026
455e93c
Only show makecode link when no changes to makecode project is made
r59q Jan 11, 2026
2fe12bb
Add MakeCode blocks view
r59q Jan 12, 2026
c7cf68a
Make blocks renderer
r59q Jan 12, 2026
cf5041a
prettier
r59q Jan 12, 2026
954fbd6
Fix the makecode link when no project changes have been made
r59q Jan 12, 2026
d76edd6
Add license notifier to missing file
r59q Jan 12, 2026
0bc467b
Add translations
r59q Jan 13, 2026
60df6ee
Support hex file download from makecode embedding
r59q Jan 13, 2026
f901af0
Add download handler
r59q Jan 14, 2026
0d0ae63
Extract logic to ts
r59q Jan 14, 2026
498eda7
Remove commented-out code
r59q Jan 14, 2026
9d35872
Move file utility to utils, try another strategy for hex creation
r59q Jan 14, 2026
8c799c9
Add padding logic
r59q Jan 14, 2026
5b081ca
Separate universal hexes
r59q Jan 14, 2026
dd39034
Update comment
r59q Jan 14, 2026
ccca37c
Remove the padding logic again
r59q Jan 14, 2026
6f83c43
Add overlay tied to the new MicrobitFlashing object
r59q Jan 17, 2026
0dc69b3
cleanup: Move logic to controller
r59q Jan 17, 2026
bbf1b52
Make flashing progress bar smoother
r59q Jan 17, 2026
e2c590d
Remove output controller as argument in microbit handlers
r59q Jan 17, 2026
86f8ab1
Correctly set output target
r59q Jan 18, 2026
9cc1696
Bump microbyte, remove logic for finding versioned hex file
r59q Jan 18, 2026
4dbb40d
prettier
r59q Jan 18, 2026
e07115f
Fix bug where correct combined hex could be found
r59q Jan 18, 2026
dc6eced
prettier
r59q Jan 18, 2026
f13a31f
Rename err to con
r59q Jan 18, 2026
3ccb82e
Bump microbyte, fix bug where micro:bit was wrongly disconnected
r59q Jan 19, 2026
6c28ebc
Fix the incorrect disconnection logic
r59q Jan 28, 2026
4533da8
Add warning to flashing hex in makecode when bluetooth isn't enabled
r59q Jan 30, 2026
06873e8
Add protection for faulty hexes
r59q Jan 30, 2026
9c8ea90
Remove snackbar from the old stores implementation
r59q Feb 3, 2026
bb038aa
Remove fingerprint enabled from old stores implementation
r59q Feb 3, 2026
d33d468
Add available and selected axes to states
r59q Feb 3, 2026
771a651
Add validation service
r59q Feb 4, 2026
4c22d5e
Add redirect after flashing hex file
r59q Feb 11, 2026
6e1d146
tweak behaviour of model page to respect feature flag
r59q Feb 18, 2026
da48abc
Remove unused parameter from output handler
r59q Feb 18, 2026
0fa9709
Add makecode warning for connect dialog
r59q Feb 18, 2026
47a427a
Add Makecode feature introduction
r59q Feb 25, 2026
ebcad53
Merge branch 'production-math' into development
Karlo-Emilo Feb 27, 2026
531a2ca
Fix error 153
r59q Feb 27, 2026
ffeab5f
Merge branch 'development' of github.com:microbit-foundation/cctd-ml-…
r59q Feb 27, 2026
a1b7aa9
Add a todo
r59q Mar 4, 2026
3662d5c
Update the makecode/knn intro video
r59q Mar 5, 2026
7197b97
Filter repository
r59q Feb 9, 2026
b51060d
Validation service implementations
r59q Feb 10, 2026
74d77ba
Cleanup: prettier, remove unused imports
r59q Feb 10, 2026
0865910
Move datset logic into it's own factory
r59q Feb 11, 2026
4ce3eca
Implement Recording for new gestures
r59q Feb 11, 2026
9a97283
Add gesture serialization
r59q Feb 16, 2026
9dca4e3
prettier
r59q Feb 16, 2026
d2457cf
Refactor gesture component
r59q Feb 17, 2026
a072fff
Fix recordings
r59q Feb 18, 2026
6d5b8a9
Move state management into bootstrapping from classifier service
r59q Mar 23, 2026
03d451f
Change to dev config for default
r59q Mar 27, 2026
c766811
Remove stores stores
r59q Mar 30, 2026
d7469c7
Fix Gesture->NewGesture
r59q Mar 31, 2026
93a35ca
Remove validation sets from stores
r59q Mar 31, 2026
a92f409
NeuralNetwork settings alignment
r59q Apr 14, 2026
1a56b08
Implement the neuralnetwork training logic requirements
r59q Apr 14, 2026
dcb6675
Implement the neuralnetwork training logic requirements
r59q Apr 14, 2026
e54bc7d
Replace old usage of knn and nn training with service-based training
r59q Apr 15, 2026
61d2764
Prepare for predictor in application layer
r59q Apr 15, 2026
3335307
Move polling engine to backend
r59q Apr 16, 2026
03a3886
Move usages of store gestures to controller gestures
r59q Apr 16, 2026
e5584bf
Added recording service
r59q Apr 20, 2026
c465f93
Remove recording from stores, move to backend
r59q Apr 21, 2026
f3f891d
Prettier
r59q Apr 21, 2026
6861ee6
Remove unused elements from stores
r59q Apr 21, 2026
c08c15f
Remove usages of devices
r59q Apr 21, 2026
c559639
Add training error and pending settings history
r59q Apr 22, 2026
ecf1caf
Add data extraction logic
r59q Apr 27, 2026
0f9082d
Move usages of filters/classifier
r59q Apr 28, 2026
028758f
Remove stores
r59q Apr 29, 2026
b0a8934
prettier
r59q Apr 29, 2026
f304830
Cleanup imports
r59q Apr 29, 2026
6049002
Part1 refactor livegraph
r59q May 5, 2026
baa820b
Prettier
r59q May 5, 2026
d50ff77
Fix live-graph
r59q May 6, 2026
f2eafde
Split up liveGraph
r59q May 6, 2026
1a7f97b
Simplify livegraph
r59q May 6, 2026
b531928
Add comments, update live graph recording lines logic
r59q May 6, 2026
25ef759
Clean up dimension labels
r59q May 6, 2026
b7a2115
Add selected gesture
r59q May 6, 2026
5b0882f
prettier
r59q May 6, 2026
7cada92
Fix: Importing gestures from example now shows up immediately
r59q May 11, 2026
097d457
Fix axis repo logger
r59q May 11, 2026
595d058
Make changes to gestures result in changes in NN architecture
r59q May 11, 2026
60c211a
Improve debugging and application initialization. Clean up old files
r59q May 11, 2026
e8104e7
Removed changelog
r59q May 12, 2026
c5350c0
Add agent files to gitignore
r59q May 12, 2026
4e464f6
Listener logic to handle gesture change events in backend
r59q May 12, 2026
1464967
Add gesture list listener
r59q May 12, 2026
4c6a089
Fix dataset factory
r59q May 12, 2026
131e340
Remove console.log
r59q May 12, 2026
b8baf26
Fix the model training state
r59q May 12, 2026
184414e
Fix validation page
r59q May 26, 2026
9288f64
Fix evaluation label exception
r59q May 28, 2026
dbe0287
Fix the gesture dots on the validation recordings
r59q Jun 1, 2026
3762224
Fix matrix displayed
r59q Jun 3, 2026
4b53af0
Change log to info to fit more idiomatic logger formats
r59q Jun 3, 2026
beb638d
Fix the confusion matrix
r59q Jun 3, 2026
bc43a24
More debugging utility
r59q Jun 10, 2026
e1fef3e
fix row major
r59q Jun 10, 2026
3226e41
Fixes on knn
r59q Jun 15, 2026
e0591ba
Make pending changes more reliable
r59q Jun 16, 2026
a9828ec
Fix the training data points in knn graph
r59q Jun 17, 2026
c17d237
Remove knn points from states to avoid confusion
r59q Jun 17, 2026
50e3c68
Clean up observer, not saving knn points anymore
r59q Jun 17, 2026
cb585ea
Fix KNN model graph
r59q Jun 29, 2026
cd3bde5
Remove legacy model implementations
r59q Jun 29, 2026
300147a
Handle undecided gesture validation
r59q Jun 29, 2026
1bdb871
Prettier + fix bug from loading example with no output
r59q Jul 1, 2026
016bfe9
Add licenses
r59q Jul 2, 2026
e51f2e1
Prep test suite for fixes
r59q Jul 2, 2026
1f1a39f
Prep test suite for fixes
r59q Jul 2, 2026
7fba4cb
Cleanup old files
r59q Jul 2, 2026
85c8857
Move livedata store
r59q Jul 2, 2026
48f96c9
Move notification service to application
r59q Jul 2, 2026
04f32d2
Move KeyValueStore to interface adapter
r59q Jul 2, 2026
85c1e05
fix checks
r59q Jul 2, 2026
5bd721f
prettier
r59q Jul 2, 2026
02f1007
Add tests from old test branch
r59q Jul 2, 2026
4fbc798
Move the auto-update on validation page to controller
r59q Jul 2, 2026
fda9a4a
Fix autoupdate on validation page
r59q Jul 2, 2026
5626710
Refactor model options
r59q Jul 7, 2026
d871120
Rename getName to getOption
r59q Jul 7, 2026
0886b02
Fix devoverlay
r59q Jul 7, 2026
46e174c
Fix KNN auto-training
r59q Jul 8, 2026
2723c95
Refactor Knn training page
r59q Jul 8, 2026
bab2d45
Extract axis picker
r59q Jul 8, 2026
d7d930d
Prettier
r59q Jul 8, 2026
3a45cab
Fix missing identifier
r59q Jul 8, 2026
50d6448
Ran eslint fix
r59q Jul 8, 2026
8764aad
Install unused imports eslint plugin and run --fix
r59q Jul 8, 2026
252d8cb
Move components to frontend
r59q Jul 8, 2026
9654a1c
Merge pull request #667 from microbit-foundation/development2
Karlo-Emilo Sep 24, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
16 changes: 15 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*
* SPDX-License-Identifier: MIT
*/
const path = require('path');

module.exports = {
root: true,
parser: '@typescript-eslint/parser',
Expand Down Expand Up @@ -33,11 +35,23 @@ module.exports = {
},
},
],
plugins: ['@typescript-eslint'],
plugins: ['@typescript-eslint', 'unused-imports'],
ignorePatterns: [
'node_modules',
'svelte.config.js',
'.eslintrc.cjs',
'babel.config.cjs',
],
rules: {
'unused-imports/no-unused-imports': 'error',
'unused-imports/no-unused-vars': [
'warn',
{
vars: 'all',
varsIgnorePattern: '^_',
args: 'after-used',
argsIgnorePattern: '^_',
},
],
},
};
10 changes: 9 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@

.env

/src/.DS_Store
/src/.DS_Store

# Agent files
TASKS.md
ARCHITECTURE.md
DECISIONS.md
.github/copilot-instructions.md
*.AGENT.md
*.agent.md
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@
"**/.DS_Store": true,
"**/Thumbs.db": true
},
"hide-files.files": []
"hide-files.files": [],
"chat.tools.terminal.autoApprove": {
"/^npx tsc --noEmit --skipLibCheck$/": {
"approve": true,
"matchCommandLine": true
}
}
}
191 changes: 191 additions & 0 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# ML-Machine Architecture

## Purpose

ML-Machine is a browser-based, interactive machine-learning application for gesture recognition with micro:bit devices.
Users collect accelerometer samples, label recordings as gestures, train a model (Neural Network or KNN), and run live predictions that can trigger visual/audio/IO outputs.

This document describes the current architecture as implemented in the repository.

## Technology Stack

- Frontend: Svelte + TypeScript + Vite
- Styling: Windi CSS
- ML runtime: TensorFlow.js plus custom model abstractions
- Device communication: `microbyte` and browser hardware APIs (Web Bluetooth/USB)
- Persistence: browser localStorage wrappers
- Testing: Vitest + Testing Library

## High-Level Architecture

The project is organized around a layered backend inside the frontend app:

- `src/frontend`, `src/pages`, `src/components`, `src/router`: UI and navigation
- `src/backend/interface-controller`: controllers consumed by Svelte views
- `src/backend/application`: application services/engines
- `src/backend/domain`: service contracts and implementations
- `src/backend/infrastructure`: repository adapters (state and localStorage)
- `src/backend/statemanagement`: state abstractions and Svelte-backed state model
- `src/core`: domain entities, filters, vectors, model/classifier abstractions and trainers
- `src/lib`: browser/device integration utilities, stores, adapters

Dependency direction is mostly inward: UI -> controllers -> services -> repositories/core.

## Composition Root and Bootstrapping

The composition root is `MLMachine` in `src/backend/interface-adapter/MLMachine.ts`.
It wires repositories, services, listeners, states, controllers, and starts the live prediction engine.

Startup flow:

1. `src/main.ts` mounts `src/App.svelte`.
2. `src/App.svelte` gets controllers from `MLMachine.getInstance()`.
3. `MLMachine` constructor builds the object graph and starts `PollingPredictorEngine`.
4. `src/frontend/view/AppRoot.svelte` loads shell UI and route content.

This is effectively a singleton service locator plus manual dependency injection.

## Runtime Views and Routing

- Router: `src/router/Router.svelte` + `src/router/Router.ts`
- Route components are dynamically imported (page-level code splitting).
- Pages typically pull controller instances via `getControllers()`.

Example pattern:

- `src/pages/DataPage.svelte` reads gesture state and invokes controller actions.
- `src/pages/training/TrainingPage.svelte` gates training UI on data sufficiency via `DataController`.

## Core Runtime Flows

### 1) Data Collection and Recording

1. Micro:bit handler receives accelerometer events (`InputMicrobitHandler`).
2. Events are converted to vector data and pushed to `DataController.addLiveData(...)`.
3. `DataServiceImpl` writes samples through `LiveDataRepository` (`StatesLiveDataRepository`) into the state-backed live buffer.
4. Recording (`RecordingServiceImpl`) waits for configured duration, snapshots buffered data, and stores a `Recording` on a gesture.

### 2) Training

1. UI invokes classifier/controller training actions.
2. `ModelServiceImpl` builds dataset from gestures and selected axes/filters through `DataServiceImpl`.
3. Selected trainer runs:
- KNN: `KNNModelTrainer`
- Neural Network: `NeuralNetworkModelTrainer`
4. `ClassifierController` wraps trained model in `VectorClassifier` and stores it via `ClassifierService`.

### 3) Live Prediction Loop

`PollingPredictorEngine` runs on an interval:

1. Pulls recent live data window (`duration`, `sampleSize`).
2. Skips prediction if no classifier or insufficient samples (< 8 for filter requirements).
3. Applies selected filters/axes to build `VectorPredictionInput`.
4. Runs classifier prediction.
5. Maps output vector to gesture IDs and saves confidence values to state.

Confidence updates then drive UI and output behavior.

### 4) Micro:bit Connection and Output

- Input/output roles are handled by dedicated handlers in `src/lib/microbit-interfacing`.
- `CombinedMicrobitHandler` supports single-device input+output mode.
- Connection states are represented in backend domain objects and persisted via state repositories.
- Output mode can switch between proprietary firmware and MakeCode behavior based on device messages.

## State and Data Model

State is centralized in `SvelteStates` (`src/backend/statemanagement/SvelteStates.ts`) using abstract state wrappers.
It contains slices for:

- gestures and selected gesture
- live data buffer and prediction
- selected axes and filters
- model settings/training status
- KNN debug points and NN training iterations
- confidences and recording status
- micro:bit connection and output target
- notifications and UI-adjacent operational state

Most infrastructure repositories are thin adapters over this state object (`States*Repository`).

## Persistence Boundaries

- Gesture dataset persistence: `LocalStorageGestureRepository`
- User reconnect session flag: `LocalStorageUserSessionRepository`
- Other runtime state: in-memory Svelte stores (not long-term persisted)

The `ControlledStorage` utility wraps localStorage interactions.

## Feature Flags and Build Variants

Feature values are loaded from `features.json` through a feature provider.

Build variants are prepared by `prepEnv.js`, which copies variant-specific:

- `features.json`
- `windi.config.js`

from `src/__viteBuildVariants__/{variant}` into repository root before running Vite.

Supported targets:

- `branded`
- `unbranded`
- `simple`
- `experimental`

## Firmware Boundary

Firmware source is under `microbit/`:

- `microbit/v1`: micro:bit v1 source
- `microbit/v2`: micro:bit v2 source and build scripts

The web app depends on compatible device firmware and version signaling for some behaviors.

## Testing Layout

Tests live under `src/__tests__/` with grouped suites such as:

- `backend/`
- `ml/`
- `csv/`
- `site/`
- `archtest/`

Tooling is configured through Vitest in `vite.config.ts` and setup in `src/__tests__/setup_tests.ts`.

## Architectural Diagram

```mermaid
flowchart LR
UI[Svelte UI Pages/Components] --> C[Interface Controllers]
C --> A[Application/Domain Services]
A --> R[Repositories]
R --> S[SvelteStates]
A --> Core[Core ML/Entities/Filters]
Device[Micro:bit Handlers in lib] --> C
Device --> A
Persist[(localStorage)] <--> R
```

## Prediction Loop Diagram

```mermaid
flowchart TD
T[Timer tick] --> D[Read live data window]
D --> Q{Classifier exists\nand enough samples?}
Q -- no --> X[Skip tick]
Q -- yes --> F[Apply selected filters/axes]
F --> P[Run classifier.predict]
P --> M[Map output vector to gesture confidences]
M --> U[Update confidence state]
```

## Notable Design Characteristics

- Strong separation between core ML/domain objects and UI framework code.
- Controllers provide a stable interface for Svelte pages.
- State is centralized and highly observable via Svelte store adapters.
- MLMachine.ts is a large bootstrapping class. Everything in the application is initialized there. It's the bunch of wires that ties everything together
141 changes: 0 additions & 141 deletions CHANGELOG.md

This file was deleted.

Loading
Loading