Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added left click based on microphone data #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion TRACKBOWLmk2/TRACKBOWLmk2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
#include <bluefruit.h>
#include <Adafruit_LittleFS.h>
#include <InternalFileSystem.h>

#include <PDM.h>
int32_t mic;

extern PDMClass PDM;
short sampleBuffer[256]; // buffer to read samples into, each sample is 16-bits
volatile int samplesRead; // number of samples read

#define ROTATE_Z_PIN 10
#define Z_ROTATION_FILENAME "/trackbowl_z"

Expand Down Expand Up @@ -37,6 +43,17 @@ float battery = 0;
uint32_t lastBatteryReport;

void setup() {
//********************
Serial.begin(115200);
while ( !Serial ) delay(10); // for nrf52840 with native usb
Serial.println("Trackbowl Mouse is active");

PDM.onReceive(onPDMdata);
PDM.begin(1, 16000);


//*********************

pinMode(ROTATE_Z_PIN, INPUT_PULLUP);
// for battery level readings
analogReference(AR_INTERNAL_3_0);
Expand Down Expand Up @@ -150,13 +167,66 @@ void quatmul(float* result, float* r, float* q) {
result[2] = r[0] * q[2] + r[1] * q[3] + r[2] * q[0] - r[3] * q[1];
result[3] = r[0] * q[3] - r[1] * q[2] + r[2] * q[1] + r[3] * q[0];
}
//****************************


int32_t getPDMwave(int32_t samples) {
short minwave = 30000;
short maxwave = -30000;

while (samples > 0) {
if (!samplesRead) {
yield();
continue;
}
for (int i = 0; i < samplesRead; i++) {
minwave = min(sampleBuffer[i], minwave);
maxwave = max(sampleBuffer[i], maxwave);
samples--;
}
// clear the read count
samplesRead = 0;
}
return maxwave - minwave;
}

void onPDMdata() {
// query the number of bytes available
int bytesAvailable = PDM.available();

// read into the sample buffer
PDM.read(sampleBuffer, bytesAvailable);

// 16-bit, 2 bytes per sample
samplesRead = bytesAvailable / 2;
}

//*********************************

void loop() {
// we target 66 Hz
uint32_t current_millis = millis();
if ((current_millis - prev_millis) < 15) {
return;
}



//************************

samplesRead = 0;
mic = getPDMwave(400);
//Serial.println(mic);
if(mic > 1000){
blehid.mouseButtonPress(MOUSE_BUTTON_LEFT);
blehid.mouseButtonRelease();
Serial.println("Mouse Clicked");
}


//Serial.print("Mic: ");

//***********************
prev_millis = current_millis;

if (needToPersistRotationZ && (lastRotationZAdjustment + 3000 < current_millis)) {
Expand Down