forked from Lebliss/VHP-Vibro-Glove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVHP-Vibro-Glove.ino
119 lines (98 loc) · 3.98 KB
/
VHP-Vibro-Glove.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
// Copyright 2021-2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This Arduino example sketch shows how to use PWM on the slim board
// Plays a sin wave from a lookup on each channel.
#include "pwm_sleeve.h"
#include "board_defs.h"
using namespace audio_tactile;
static uint16_t sin_wave[8];
static uint16_t silence[8] = {0}; // all zeros for no vibration output.
// Output sequence for board Apollo84 hardware
//int order_pairs[8] = {0, 3, 4, 5, 11, 9, 8, 6};
// Output sequence for Godef Hardware
int order_pairs[8] = {4, 5, 6, 7, 8, 9, 10, 11};
void OnPwmSequenceEnd();
void setup() {
nrf_gpio_cfg_output(kLedPinBlue);
nrf_gpio_cfg_output(kLedPinGreen);
// Create a sin wav pattern to play, which is repeated every 4 ms.
// Can be changed to any pattern here, with resolution of 0 to 512, where 512
// is the loudest. Also, scale the output, so its not too loud.
const int scale = 2;
sin_wave[0] = 256 / scale;
sin_wave[1] = 437 / scale;
sin_wave[2] = 512 / scale;
sin_wave[3] = 437 / scale;
sin_wave[4] = 256 / scale;
sin_wave[5] = 75 / scale;
sin_wave[6] = 0 / scale;
sin_wave[7] = 75 / scale;
SleeveTactors.OnSequenceEnd(OnPwmSequenceEnd);
SleeveTactors.Initialize();
// The default Samplingrate [1] is not set correctly [2] and thus becomes defacto
// sampling rate of 9. Use SetUpsamplingFactor [3] to set the correct samplingrate.
// [1] - https://github.com/google/audio-to-tactile/blob/3be62006ecf7a95962d7520e23e69258e64e49bd/src/pwm_sleeve.h#L75
// [2] - https://github.com/google/audio-to-tactile/blob/3be62006ecf7a95962d7520e23e69258e64e49bd/src/pwm_sleeve.cpp#L87
// [3] - https://github.com/google/audio-to-tactile/blob/3be62006ecf7a95962d7520e23e69258e64e49bd/src/pwm_sleeve.cpp#L108
SleeveTactors.SetUpsamplingFactor(8);
// Warning: issue only in Arduino. When using StartPlayback() it crashes.
// Looks like NRF_PWM0 module is automatically triggered, and triggering it
// again here crashes ISR. Temporary fix is to only use nrf_pwm_task_trigger
// for NRF_PWM1 and NRF_PWM2. To fix might need a nRF52 driver update.
nrf_pwm_task_trigger(NRF_PWM1, NRF_PWM_TASK_SEQSTART0);
nrf_pwm_task_trigger(NRF_PWM2, NRF_PWM_TASK_SEQSTART0);
}
void loop() {
nrf_gpio_pin_set(kLedPinBlue);
// Loop through all channels.
for (int i = 0; i < 3; i++) {
shuffleOrder(order_pairs, 4);
for (int c = 0; c < 4; c++) {
Serial.print("Current activation: ");
Serial.print(order_pairs[c]+1);
Serial.print(" and ");
Serial.println(order_pairs[c+4]+1);
nrf_gpio_pin_set(kLedPinGreen);
SleeveTactors.UpdateChannel(order_pairs[c], sin_wave);
SleeveTactors.UpdateChannel(order_pairs[c+4], sin_wave);
delay(100);
nrf_gpio_pin_clear(kLedPinGreen);
SleeveTactors.UpdateChannel(order_pairs[c], silence);
SleeveTactors.UpdateChannel(order_pairs[c+4], silence);
delay(67);
}
}
nrf_gpio_pin_clear(kLedPinBlue);
delay(1333);
}
// This function is triggered after sequence is finished playing.
void OnPwmSequenceEnd() {}
// Shuffle the ordering array (two sets in one array)
void shuffleOrder(int *order, int n)
{
// Seed the random function with noise from analog pin 0
randomSeed(analogRead(0));
// Shuffle each element with a random later element
for (int i = 0; i < n; i++)
{
int j = random(n-i);
int temp = order[i];
order[i] = order[i+j];
order[i+j] = temp;
temp = order[i+n];
order[i+n] = order[i+j+n];
order[i+j+n] = temp;
}
}