Skip to content

Commit 0651eac

Browse files
committed
Add Theremin, Vibration detection and Object Hunting examples
1 parent 0203a1a commit 0651eac

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+3762
-0
lines changed

examples/object-hunting/app.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: Object Hunting
2+
icon: 🔍
3+
description: Detect a list of object to win the game
4+
5+
bricks:
6+
- arduino:video_object_detection
7+
- arduino:web_ui
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
// SPDX-FileCopyrightText: Copyright (C) 2025 ARDUINO SA <http://www.arduino.cc>
2+
//
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
const socket = io(`http://${window.location.host}`);
6+
const errorContainer = document.getElementById('error-container');
7+
8+
// Game state
9+
const targetObjects = ['book', 'bottle', 'chair', 'cup', 'cell phone'];
10+
let foundObjects = [];
11+
let gameStarted = false;
12+
13+
// UI Elements
14+
const gameIntro = document.getElementById('game-intro');
15+
const gameContent = document.getElementById('game-content');
16+
const startGameBtn = document.getElementById('start-game-btn');
17+
const objectsToFindList = document.getElementById('objects-to-find-list');
18+
const videoFeedContainer = document.getElementById('videoFeedContainer');
19+
const winScreen = document.getElementById('win-screen');
20+
const playAgainBtn = document.getElementById('play-again-btn');
21+
22+
document.addEventListener('DOMContentLoaded', () => {
23+
initSocketIO();
24+
initializeConfidenceSlider();
25+
renderObjectsToFind();
26+
27+
startGameBtn.addEventListener('click', startGame);
28+
playAgainBtn.addEventListener('click', resetGame);
29+
});
30+
31+
function initSocketIO() {
32+
socket.on('connect', () => {
33+
if (errorContainer) {
34+
errorContainer.style.display = 'none';
35+
errorContainer.textContent = '';
36+
}
37+
});
38+
39+
socket.on('disconnect', () => {
40+
if (errorContainer) {
41+
errorContainer.textContent = 'Connection to the board lost. Please check the connection.';
42+
errorContainer.style.display = 'block';
43+
}
44+
});
45+
46+
socket.on('detection', async (message) => {
47+
if (gameStarted) {
48+
handleDetection(message);
49+
}
50+
});
51+
}
52+
53+
function startGame() {
54+
gameStarted = true;
55+
gameIntro.classList.add('hidden');
56+
gameContent.classList.remove('hidden');
57+
updateFoundCounter();
58+
}
59+
60+
function resetGame() {
61+
gameStarted = false;
62+
foundObjects = [];
63+
winScreen.classList.add('hidden');
64+
videoFeedContainer.classList.remove('hidden');
65+
gameIntro.classList.remove('hidden');
66+
gameContent.classList.add('hidden');
67+
renderObjectsToFind();
68+
updateFoundCounter();
69+
}
70+
71+
function updateFoundCounter() {
72+
const chip = document.getElementById('found-counter-chip');
73+
if (chip) {
74+
chip.textContent = `${foundObjects.length}/5 found`;
75+
}
76+
}
77+
78+
function renderObjectsToFind() {
79+
objectsToFindList.innerHTML = '';
80+
targetObjects.forEach(obj => {
81+
const item = document.createElement('div');
82+
item.id = `obj-${obj}`;
83+
item.className = 'object-item';
84+
85+
const icon = document.createElement('img');
86+
icon.src = `./img/${obj}.svg`;
87+
icon.alt = `${obj} icon`;
88+
item.appendChild(icon);
89+
90+
const text = document.createElement('span');
91+
text.textContent = obj;
92+
item.appendChild(text);
93+
94+
objectsToFindList.appendChild(item);
95+
});
96+
}
97+
98+
function handleDetection(detection) {
99+
const detectedObject = detection.content.toLowerCase();
100+
if (targetObjects.includes(detectedObject) && !foundObjects.includes(detectedObject)) {
101+
foundObjects.push(detectedObject);
102+
const foundItem = document.getElementById(`obj-${detectedObject}`);
103+
foundItem.classList.add('found');
104+
105+
const foundIcon = document.createElement('img');
106+
foundIcon.src = './img/found-icon.svg';
107+
foundIcon.alt = 'Found';
108+
foundIcon.className = 'found-icon';
109+
foundItem.appendChild(foundIcon);
110+
111+
updateFoundCounter();
112+
checkWinCondition();
113+
}
114+
}
115+
116+
function checkWinCondition() {
117+
if (foundObjects.length === targetObjects.length) {
118+
gameStarted = false;
119+
videoFeedContainer.classList.add('hidden');
120+
winScreen.classList.remove('hidden');
121+
}
122+
}
123+
124+
function initializeConfidenceSlider() {
125+
const confidenceSlider = document.getElementById('confidenceSlider');
126+
const confidenceInput = document.getElementById('confidenceInput');
127+
const confidenceResetButton = document.getElementById('confidenceResetButton');
128+
129+
if (!confidenceSlider) return;
130+
131+
confidenceSlider.addEventListener('input', updateConfidenceDisplay);
132+
confidenceInput.addEventListener('input', handleConfidenceInputChange);
133+
confidenceInput.addEventListener('blur', validateConfidenceInput);
134+
updateConfidenceDisplay();
135+
136+
confidenceResetButton.addEventListener('click', (e) => {
137+
if (e.target.classList.contains('reset-icon') || e.target.closest('.reset-icon')) {
138+
resetConfidence();
139+
}
140+
});
141+
}
142+
143+
function handleConfidenceInputChange() {
144+
const confidenceInput = document.getElementById('confidenceInput');
145+
const confidenceSlider = document.getElementById('confidenceSlider');
146+
147+
let value = parseFloat(confidenceInput.value);
148+
149+
if (isNaN(value)) value = 0.5;
150+
if (value < 0) value = 0;
151+
if (value > 1) value = 1;
152+
153+
confidenceSlider.value = value;
154+
updateConfidenceDisplay();
155+
}
156+
157+
function validateConfidenceInput() {
158+
const confidenceInput = document.getElementById('confidenceInput');
159+
let value = parseFloat(confidenceInput.value);
160+
161+
if (isNaN(value)) value = 0.5;
162+
if (value < 0) value = 0;
163+
if (value > 1) value = 1;
164+
165+
confidenceInput.value = value.toFixed(2);
166+
167+
handleConfidenceInputChange();
168+
}
169+
170+
function updateConfidenceDisplay() {
171+
const confidenceSlider = document.getElementById('confidenceSlider');
172+
const confidenceInput = document.getElementById('confidenceInput');
173+
const confidenceValueDisplay = document.getElementById('confidenceValueDisplay');
174+
const sliderProgress = document.getElementById('sliderProgress');
175+
176+
if (!confidenceSlider) return;
177+
178+
const value = parseFloat(confidenceSlider.value);
179+
socket.emit('override_th', value); // Send confidence to backend
180+
const percentage = (value - confidenceSlider.min) / (confidenceSlider.max - confidenceSlider.min) * 100;
181+
182+
const displayValue = value.toFixed(2);
183+
confidenceValueDisplay.textContent = displayValue;
184+
185+
if (document.activeElement !== confidenceInput) {
186+
confidenceInput.value = displayValue;
187+
}
188+
189+
sliderProgress.style.width = percentage + '%';
190+
confidenceValueDisplay.style.left = percentage + '%';
191+
}
192+
193+
function resetConfidence() {
194+
195+
const confidenceSlider = document.getElementById('confidenceSlider');
196+
197+
const confidenceInput = document.getElementById('confidenceInput');
198+
199+
200+
201+
if (!confidenceSlider) return;
202+
203+
204+
205+
confidenceSlider.value = '0.5';
206+
207+
confidenceInput.value = '0.50';
208+
209+
updateConfidenceDisplay();
210+
211+
}
212+
213+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
Copyright 2020 The Open Sans Project Authors (https://github.com/googlefonts/opensans)
2+
3+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
4+
This license is copied below, and is also available with a FAQ at:
5+
https://openfontlicense.org
6+
7+
8+
-----------------------------------------------------------
9+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10+
-----------------------------------------------------------
11+
12+
PREAMBLE
13+
The goals of the Open Font License (OFL) are to stimulate worldwide
14+
development of collaborative font projects, to support the font creation
15+
efforts of academic and linguistic communities, and to provide a free and
16+
open framework in which fonts may be shared and improved in partnership
17+
with others.
18+
19+
The OFL allows the licensed fonts to be used, studied, modified and
20+
redistributed freely as long as they are not sold by themselves. The
21+
fonts, including any derivative works, can be bundled, embedded,
22+
redistributed and/or sold with any software provided that any reserved
23+
names are not used by derivative works. The fonts and derivatives,
24+
however, cannot be released under any other type of license. The
25+
requirement for fonts to remain under this license does not apply
26+
to any document created using the fonts or their derivatives.
27+
28+
DEFINITIONS
29+
"Font Software" refers to the set of files released by the Copyright
30+
Holder(s) under this license and clearly marked as such. This may
31+
include source files, build scripts and documentation.
32+
33+
"Reserved Font Name" refers to any names specified as such after the
34+
copyright statement(s).
35+
36+
"Original Version" refers to the collection of Font Software components as
37+
distributed by the Copyright Holder(s).
38+
39+
"Modified Version" refers to any derivative made by adding to, deleting,
40+
or substituting -- in part or in whole -- any of the components of the
41+
Original Version, by changing formats or by porting the Font Software to a
42+
new environment.
43+
44+
"Author" refers to any designer, engineer, programmer, technical
45+
writer or other person who contributed to the Font Software.
46+
47+
PERMISSION & CONDITIONS
48+
Permission is hereby granted, free of charge, to any person obtaining
49+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
50+
redistribute, and sell modified and unmodified copies of the Font
51+
Software, subject to the following conditions:
52+
53+
1) Neither the Font Software nor any of its individual components,
54+
in Original or Modified Versions, may be sold by itself.
55+
56+
2) Original or Modified Versions of the Font Software may be bundled,
57+
redistributed and/or sold with any software, provided that each copy
58+
contains the above copyright notice and this license. These can be
59+
included either as stand-alone text files, human-readable headers or
60+
in the appropriate machine-readable metadata fields within text or
61+
binary files as long as those fields can be easily viewed by the user.
62+
63+
3) No Modified Version of the Font Software may use the Reserved Font
64+
Name(s) unless explicit written permission is granted by the corresponding
65+
Copyright Holder. This restriction only applies to the primary font name as
66+
presented to the users.
67+
68+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69+
Software shall not be used to promote, endorse or advertise any
70+
Modified Version, except to acknowledge the contribution(s) of the
71+
Copyright Holder(s) and the Author(s) or with their explicit written
72+
permission.
73+
74+
5) The Font Software, modified or unmodified, in part or in whole,
75+
must be distributed entirely under this license, and must not be
76+
distributed under any other license. The requirement for fonts to
77+
remain under this license does not apply to any document created
78+
using the Font Software.
79+
80+
TERMINATION
81+
This license becomes null and void if any of the above conditions are
82+
not met.
83+
84+
DISCLAIMER
85+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93+
OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file not shown.

0 commit comments

Comments
 (0)