Skip to content

Commit 6431936

Browse files
committed
Moving to Library Standards
Moving out the pinMode and Debounce into a library. Should probably make it a true object and wrap up all of the debounce and global varaibles there. But/and this works for now and should fullfill the library requirements.
1 parent e205139 commit 6431936

File tree

4 files changed

+74
-37
lines changed

4 files changed

+74
-37
lines changed

LICENSE.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Released under MIT License
2+
3+
Copyright (c) 2021 TinyMLx.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

examples/test_microphone/test_microphone.ino

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,20 @@
55
*/
66

77
#include <PDM.h>
8-
9-
#define BUTTON_PIN 13
8+
#include <TinyMLShield.h>
109

1110
// PDM buffer
1211
short sampleBuffer[256];
1312
volatile int samplesRead;
1413

15-
// Variables for button debouncing
16-
unsigned long lastDebounceTime = 0;
17-
unsigned long debounceDelay = 50;
18-
bool lastButtonState = HIGH;
19-
bool buttonState;
20-
2114
bool record = false;
2215

2316
void setup() {
2417
Serial.begin(9600);
2518
while (!Serial);
2619

27-
pinMode(BUTTON_PIN, OUTPUT);
28-
digitalWrite(BUTTON_PIN, HIGH);
29-
nrf_gpio_cfg_out_with_input(digitalPinToPinName(BUTTON_PIN));
20+
// Initialize the TinyML Shield
21+
initializeShield();
3022

3123
PDM.onReceive(onPDMdata);
3224
// Initialize PDM microphone in mono mode with 16 kHz sample rate
@@ -41,23 +33,11 @@ void setup() {
4133
}
4234

4335
void loop() {
44-
bool buttonRead = nrf_gpio_pin_read(digitalPinToPinName(BUTTON_PIN));
45-
46-
if (buttonRead != lastButtonState) {
47-
lastDebounceTime = millis();
36+
// see if the button is pressed and turn off or on recording accordingly
37+
bool clicked = readShieldButton();
38+
if (clicked){
39+
record = !record;
4840
}
49-
50-
if (millis() - lastDebounceTime >= debounceDelay) {
51-
if (buttonRead != buttonState) {
52-
buttonState = buttonRead;
53-
54-
if (!buttonState) {
55-
record = !record;
56-
}
57-
}
58-
}
59-
60-
lastButtonState = buttonRead;
6141

6242
if (samplesRead) {
6343

@@ -82,13 +62,3 @@ void onPDMdata() {
8262

8363
samplesRead = bytesAvailable / 2;
8464
}
85-
86-
void nrf_gpio_cfg_out_with_input(uint32_t pin_number) {
87-
nrf_gpio_cfg(
88-
pin_number,
89-
NRF_GPIO_PIN_DIR_OUTPUT,
90-
NRF_GPIO_PIN_INPUT_CONNECT,
91-
NRF_GPIO_PIN_PULLUP,
92-
NRF_GPIO_PIN_S0S1,
93-
NRF_GPIO_PIN_NOSENSE);
94-
}

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Harvard_TinyMLx
2+
version=0.0.1-Alpha
3+
author=TinyMLx Authors
4+
maintainer=Brian Plancher <brian.plancher@gmail.com>
5+
sentence=Supports the TinyML edX Course and TinyML Shield.
6+
paragraph=This library supports the TinyML Shield and provides examples that suppor the TinyML edX course. The examples work best with the Arduino Nano 33 BLE Sense board and the Tiny Machine Learning Kit from Arduino.
7+
category=Sensors
8+
url=http://www.tinymlx.org
9+
includes=TinyMLShield.h
10+
ldflags=-lm

src/TinyMLShield.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#define BUTTON_PIN 13
2+
3+
// Custom shield button pin setting
4+
void nrf_gpio_cfg_out_with_input(uint32_t pin_number) {
5+
nrf_gpio_cfg(
6+
pin_number,
7+
NRF_GPIO_PIN_DIR_OUTPUT,
8+
NRF_GPIO_PIN_INPUT_CONNECT,
9+
NRF_GPIO_PIN_PULLUP,
10+
NRF_GPIO_PIN_S0S1,
11+
NRF_GPIO_PIN_NOSENSE);
12+
}
13+
14+
// Variables for button debouncing
15+
unsigned long lastDebounceTime = 0;
16+
unsigned long debounceDelay = 50;
17+
bool lastButtonState = HIGH;
18+
bool buttonState;
19+
20+
// set the pin modes for the button
21+
void initializeShield(){
22+
pinMode(BUTTON_PIN, OUTPUT);
23+
digitalWrite(BUTTON_PIN, HIGH);
24+
nrf_gpio_cfg_out_with_input(digitalPinToPinName(BUTTON_PIN));
25+
}
26+
27+
// debounce the button and return true on new click
28+
bool readShieldButton(){
29+
bool buttonRead = nrf_gpio_pin_read(digitalPinToPinName(BUTTON_PIN));
30+
31+
if (buttonRead != lastButtonState) {
32+
lastDebounceTime = millis();
33+
}
34+
35+
if (millis() - lastDebounceTime >= debounceDelay) {
36+
if (buttonRead != buttonState) {
37+
buttonState = buttonRead;
38+
39+
if (!buttonState) {
40+
lastButtonState = buttonRead;
41+
return true;
42+
}
43+
}
44+
}
45+
46+
lastButtonState = buttonRead;
47+
return false;
48+
}

0 commit comments

Comments
 (0)