-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain01_MouseUSB.ino
166 lines (141 loc) · 4.71 KB
/
main01_MouseUSB.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <Wire.h>
// #include <elapsedMillis.h>
#include <Audio.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
// TO DO MAIN:
// Change the max and min of sensor to what the sensor actually measures
// Looking at different mappings
// To decide whether to stop the current pulse and play the next or just to play the next after finishing the current pulse
enum Mode {
MODE_2, // Continuous Vibration
MODE_3, // Motion-Coupled Continous Vibration
MODE_OFF // OFF Mode
};
Mode currentMode = MODE_OFF; // Initialize current mode to MODE_3
//=========== Sensor Constants ===========
float filtered_sensor_value = 0.f;
float last_triggered_sensor_val = 0.f;
static constexpr uint32_t kSensorMinValue = 0;
static constexpr uint32_t kSensorMaxValue = 5359;
//=========== audio variables ===========
AudioSynthWaveform signal;
AudioOutputPT8211 dac;
AudioConnection patchCord1(signal, 0, dac, 0);
AudioConnection patchCord2(signal, 0, dac, 1);
//=========== control flow variables ===========
elapsedMicros pulse_time_us = 0;
bool is_vibrating = false;
uint16_t mapped_bin_id = 0;
uint16_t last_bin_id = 0;
//=========== signal generator ===========
static float kSignalFrequencyHz = 40.f; // Frequency of continuous vibration
static constexpr float kSignalAmp = 1.f;
//=========== other variables ===========
float xValue;
uint16_t condition;
float freqValue;
uint16_t binValue;
uint32_t signalDuration;
//=========== serial ===========
static constexpr int kBaudRate = 115200;
//=========== helper functions ===========
inline void SetupSerial() __attribute__((always_inline));
void SetupSerial() {
while (!Serial && millis() < 2000);
Serial.begin(kBaudRate);
delay(50);
}
void SetupAudio() {
AudioMemory(20);
delay(50); // time for DAC voltage stable
signal.begin(WAVEFORM_SINE);
signal.frequency(kSignalFrequencyHz);
}
void StartPulseCV(float freq) {
signal.begin(WAVEFORM_SINE);
signal.frequency(freq);
signal.phase(0.0);
signal.amplitude(kSignalAmp);
pulse_time_us = 0;
is_vibrating = true;
}
void StopPulse() {
signal.amplitude(0.f);
is_vibrating = false;
}
void GenerateContinuousVibration() {
signal.begin(WAVEFORM_SINE);
signal.frequency(kSignalFrequencyHz);
signal.amplitude(kSignalAmp);
is_vibrating = true;
}
void GenerateMotionCoupledVibration(float measuredDistance, float freq, uint16_t bin) {
mapped_bin_id = bin;
if (mapped_bin_id != last_bin_id) {
// Uncomment below to stop the current vibration and play the next one.
if (is_vibrating) { // This loop is for the case when we want to stop the ongoing vibration and start the next one.
StopPulse();
delayMicroseconds(50);
}
StartPulseCV(freq);
last_bin_id = mapped_bin_id;
}
signalDuration = 3 * 1000 * 1000* 1 / freq;
if (is_vibrating && pulse_time_us >= signalDuration) {
StopPulse();
}
}
void setup() {
SetupSerial();
SetupAudio();
}
void loop() {
if (Serial.available() > 0) {
String data = Serial.readStringUntil('\n'); // Read the incoming data until newline
data.trim(); // Remove any leading/trailing whitespace
if (data.length() > 0) {
int firstCommaIndex = data.indexOf(',');
int secondCommaIndex = data.indexOf(',', firstCommaIndex + 1);
int thirdCommaIndex = data.indexOf(',', secondCommaIndex + 1);
if (firstCommaIndex > -1 && secondCommaIndex > -1 && thirdCommaIndex > -1) {
String xValueStr = data.substring(0, firstCommaIndex);
String conditionStr = data.substring(firstCommaIndex + 1, secondCommaIndex);
String freqValueStr = data.substring(secondCommaIndex + 1, thirdCommaIndex);
String binValueStr = data.substring(thirdCommaIndex + 1);
xValue = xValueStr.toInt();
condition = conditionStr.toInt();
freqValue = freqValueStr.toInt();
binValue = binValueStr.toInt();
}
}
}
// Map the sensor value to the bin numbers
mapped_bin_id = map(xValue, kSensorMinValue, kSensorMaxValue, 0, binValue);
// Switch between the modes of operation based on the user preference
switch (condition) {
case 1:
currentMode = MODE_OFF; // OFF mode
break;
case 2:
currentMode = MODE_2; //Continuous Vibration
break;
case 3:
currentMode = MODE_3; //Motion Coupled Vibration
break;
}
switch (currentMode) {
case MODE_OFF:
StopPulse();
break;
case MODE_2:
GenerateContinuousVibration();
break;
case MODE_3:
GenerateMotionCoupledVibration(xValue, freqValue, mapped_bin_id);
break;
}
}