mbed to brain machine firmware. See http://klautesblog.blogspot.com for further information.

Committer:
klaute
Date:
Wed Apr 13 15:44:28 2011 +0000
Revision:
1:90922bda2b17
Parent:
0:cf8c942ef95b
v0.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
klaute 0:cf8c942ef95b 1 /**
klaute 0:cf8c942ef95b 2 * firmware for the mbed to brain machine project
klaute 0:cf8c942ef95b 3 * see http://klautesblog.blogspot.com for further information.
klaute 0:cf8c942ef95b 4 *
klaute 0:cf8c942ef95b 5 * Licensed under GPLv3 - http://www.gnu.org/licenses/gpl.html
klaute 0:cf8c942ef95b 6 *
klaute 0:cf8c942ef95b 7 * Written by Kai Lauterbach (klaute at gmail dot com) 04/2011
klaute 0:cf8c942ef95b 8 */
klaute 0:cf8c942ef95b 9
klaute 0:cf8c942ef95b 10 #include "main.h"
klaute 0:cf8c942ef95b 11
klaute 0:cf8c942ef95b 12 void initBrainwaves() {
klaute 0:cf8c942ef95b 13
klaute 1:90922bda2b17 14 // A example brain wave sequence.
klaute 1:90922bda2b17 15 // The sequence starts from a state of being awake.
klaute 1:90922bda2b17 16 // Then is goes into deep meditation.
klaute 1:90922bda2b17 17 // Hangs out there awhile, and then is will be going
klaute 1:90922bda2b17 18 // into awake again.
klaute 0:cf8c942ef95b 19 brainwaves[1-1].type = 1;
klaute 0:cf8c942ef95b 20 brainwaves[1-1].duration = 60;
klaute 0:cf8c942ef95b 21 brainwaves[2-1].type = 0;
klaute 0:cf8c942ef95b 22 brainwaves[2-1].duration = 10;
klaute 0:cf8c942ef95b 23 brainwaves[3-1].type = 1;
klaute 0:cf8c942ef95b 24 brainwaves[3-1].duration = 20;
klaute 0:cf8c942ef95b 25 brainwaves[4-1].type = 0;
klaute 0:cf8c942ef95b 26 brainwaves[4-1].duration = 15;
klaute 0:cf8c942ef95b 27 brainwaves[5-1].type = 1;
klaute 0:cf8c942ef95b 28 brainwaves[5-1].duration = 15;
klaute 0:cf8c942ef95b 29 brainwaves[6-1].type = 0;
klaute 0:cf8c942ef95b 30 brainwaves[6-1].duration = 20;
klaute 0:cf8c942ef95b 31 brainwaves[7-1].type = 1;
klaute 0:cf8c942ef95b 32 brainwaves[7-1].duration = 10;
klaute 0:cf8c942ef95b 33 brainwaves[8-1].type = 0;
klaute 0:cf8c942ef95b 34 brainwaves[8-1].duration = 30;
klaute 0:cf8c942ef95b 35 brainwaves[9-1].type = 1;
klaute 0:cf8c942ef95b 36 brainwaves[9-1].duration = 5;
klaute 0:cf8c942ef95b 37 brainwaves[10-1].type = 0;
klaute 0:cf8c942ef95b 38 brainwaves[10-1].duration = 60;
klaute 0:cf8c942ef95b 39 brainwaves[11-1].type = 3;
klaute 0:cf8c942ef95b 40 brainwaves[11-1].duration = 10;
klaute 0:cf8c942ef95b 41 brainwaves[12-1].type = 0;
klaute 0:cf8c942ef95b 42 brainwaves[12-1].duration = 30;
klaute 0:cf8c942ef95b 43 brainwaves[13-1].type = 3;
klaute 0:cf8c942ef95b 44 brainwaves[13-1].duration = 20;
klaute 0:cf8c942ef95b 45 brainwaves[14-1].type = 0;
klaute 0:cf8c942ef95b 46 brainwaves[14-1].duration = 30;
klaute 0:cf8c942ef95b 47 brainwaves[15-1].type = 3;
klaute 0:cf8c942ef95b 48 brainwaves[15-1].duration = 30;
klaute 0:cf8c942ef95b 49 brainwaves[16-1].type = 0;
klaute 0:cf8c942ef95b 50 brainwaves[16-1].duration = 15;
klaute 0:cf8c942ef95b 51 brainwaves[17-1].type = 3;
klaute 0:cf8c942ef95b 52 brainwaves[17-1].duration = 60;
klaute 0:cf8c942ef95b 53 brainwaves[18-1].type = 0;
klaute 0:cf8c942ef95b 54 brainwaves[18-1].duration = 15;
klaute 0:cf8c942ef95b 55 brainwaves[19-1].type = 1;
klaute 0:cf8c942ef95b 56 brainwaves[19-1].duration = 1;
klaute 0:cf8c942ef95b 57 brainwaves[20-1].type = 0;
klaute 0:cf8c942ef95b 58 brainwaves[20-1].duration = 15;
klaute 0:cf8c942ef95b 59 brainwaves[21-1].type = 3;
klaute 0:cf8c942ef95b 60 brainwaves[21-1].duration = 60;
klaute 0:cf8c942ef95b 61 brainwaves[22-1].type = 2;
klaute 0:cf8c942ef95b 62 brainwaves[22-1].duration = 1;
klaute 0:cf8c942ef95b 63 brainwaves[23-1].type = 3;
klaute 0:cf8c942ef95b 64 brainwaves[23-1].duration = 10;
klaute 0:cf8c942ef95b 65 brainwaves[24-1].type = 2;
klaute 0:cf8c942ef95b 66 brainwaves[24-1].duration = 1;
klaute 0:cf8c942ef95b 67 brainwaves[25-1].type = 3;
klaute 0:cf8c942ef95b 68 brainwaves[25-1].duration = 10;
klaute 0:cf8c942ef95b 69 brainwaves[26-1].type = 2;
klaute 0:cf8c942ef95b 70 brainwaves[26-1].duration = 1;
klaute 0:cf8c942ef95b 71 brainwaves[27-1].type = 3;
klaute 0:cf8c942ef95b 72 brainwaves[27-1].duration = 30;
klaute 0:cf8c942ef95b 73 brainwaves[28-1].type = 0;
klaute 0:cf8c942ef95b 74 brainwaves[28-1].duration = 15;
klaute 0:cf8c942ef95b 75 brainwaves[29-1].type = 1;
klaute 0:cf8c942ef95b 76 brainwaves[29-1].duration = 1;
klaute 0:cf8c942ef95b 77 brainwaves[30-1].type = 0;
klaute 0:cf8c942ef95b 78 brainwaves[30-1].duration = 15;
klaute 0:cf8c942ef95b 79 brainwaves[31-1].type = 3;
klaute 0:cf8c942ef95b 80 brainwaves[31-1].duration = 30;
klaute 0:cf8c942ef95b 81 brainwaves[32-1].type = 0;
klaute 0:cf8c942ef95b 82 brainwaves[32-1].duration = 15;
klaute 0:cf8c942ef95b 83 brainwaves[33-1].type = 1;
klaute 0:cf8c942ef95b 84 brainwaves[33-1].duration = 1;
klaute 0:cf8c942ef95b 85 brainwaves[34-1].type = 0;
klaute 0:cf8c942ef95b 86 brainwaves[34-1].duration = 20;
klaute 0:cf8c942ef95b 87 brainwaves[35-1].type = 1;
klaute 0:cf8c942ef95b 88 brainwaves[35-1].duration = 5;
klaute 0:cf8c942ef95b 89 brainwaves[36-1].type = 0;
klaute 0:cf8c942ef95b 90 brainwaves[36-1].duration = 20;
klaute 0:cf8c942ef95b 91 brainwaves[37-1].type = 1;
klaute 0:cf8c942ef95b 92 brainwaves[37-1].duration = 15;
klaute 0:cf8c942ef95b 93 brainwaves[38-1].type = 0;
klaute 0:cf8c942ef95b 94 brainwaves[38-1].duration = 15;
klaute 0:cf8c942ef95b 95 brainwaves[39-1].type = 1;
klaute 0:cf8c942ef95b 96 brainwaves[39-1].duration = 20;
klaute 0:cf8c942ef95b 97 brainwaves[40-1].type = 0;
klaute 0:cf8c942ef95b 98 brainwaves[40-1].duration = 10;
klaute 0:cf8c942ef95b 99 brainwaves[41-1].type = 1;
klaute 0:cf8c942ef95b 100 brainwaves[41-1].duration = 25;
klaute 0:cf8c942ef95b 101 brainwaves[42-1].type = 0;
klaute 0:cf8c942ef95b 102 brainwaves[42-1].duration = 5;
klaute 0:cf8c942ef95b 103 brainwaves[43-1].type = 1;
klaute 0:cf8c942ef95b 104 brainwaves[43-1].duration = 60;
klaute 0:cf8c942ef95b 105 brainwaves[44-1].isEnd = true;
klaute 0:cf8c942ef95b 106
klaute 0:cf8c942ef95b 107 }
klaute 0:cf8c942ef95b 108
klaute 0:cf8c942ef95b 109 /*************************************************************************************************/
klaute 0:cf8c942ef95b 110
klaute 0:cf8c942ef95b 111 void Audio_L_Control() {
klaute 0:cf8c942ef95b 112 // Toggle left audio channel
klaute 0:cf8c942ef95b 113 audioLeft = ( audioLeft == 0 ) ? 1 : 0;
klaute 0:cf8c942ef95b 114 }
klaute 0:cf8c942ef95b 115
klaute 0:cf8c942ef95b 116 void Audio_R_Control() {
klaute 0:cf8c942ef95b 117 // toggle right audio channel
klaute 0:cf8c942ef95b 118 audioRight = ( audioRight == 0 ) ? 1 : 0;
klaute 0:cf8c942ef95b 119 }
klaute 0:cf8c942ef95b 120
klaute 0:cf8c942ef95b 121 void Brainwave_Control() {
klaute 0:cf8c942ef95b 122
klaute 0:cf8c942ef95b 123 // check for end of brainwave sequence
klaute 0:cf8c942ef95b 124 if ( brainwaves[bw_pos].isEnd && bw_duration <= 0) {
klaute 0:cf8c942ef95b 125
klaute 0:cf8c942ef95b 126 LEDStatus2 = ( LEDStatus2 == 0 ) ? 1 : 0;
klaute 0:cf8c942ef95b 127
klaute 0:cf8c942ef95b 128 ledRight = 0;
klaute 0:cf8c942ef95b 129 ledLeft = 0;
klaute 0:cf8c942ef95b 130 ledStatusRight = 0;
klaute 0:cf8c942ef95b 131 ledStatusLeft = 0;
klaute 0:cf8c942ef95b 132
klaute 0:cf8c942ef95b 133 timer0.detach();
klaute 0:cf8c942ef95b 134 timer1.detach();
klaute 0:cf8c942ef95b 135
klaute 0:cf8c942ef95b 136 return;
klaute 0:cf8c942ef95b 137 }
klaute 0:cf8c942ef95b 138
klaute 0:cf8c942ef95b 139 // check for end of duration
klaute 0:cf8c942ef95b 140 if ( bw_duration > 0 ) {
klaute 0:cf8c942ef95b 141 bw_duration--;
klaute 0:cf8c942ef95b 142 return;
klaute 0:cf8c942ef95b 143 }
klaute 0:cf8c942ef95b 144
klaute 0:cf8c942ef95b 145 // toggle brainwave status
klaute 0:cf8c942ef95b 146 LEDStatus2 = ( LEDStatus2 == 0 ) ? 1 : 0;
klaute 0:cf8c942ef95b 147
klaute 0:cf8c942ef95b 148 bw_duration = brainwaves[bw_pos].duration;
klaute 0:cf8c942ef95b 149
klaute 1:90922bda2b17 150 int type = brainwaves[bw_pos].type;
klaute 0:cf8c942ef95b 151 float pwmPeriod = 0.0;
klaute 0:cf8c942ef95b 152 float ledPulseWidth = 0.0;
klaute 0:cf8c942ef95b 153 float audioFreq = 0.0;
klaute 1:90922bda2b17 154 // Set the audioTimer and PWM signals to the new brain
klaute 1:90922bda2b17 155 // wave frequency.
klaute 0:cf8c942ef95b 156 switch (type) {
klaute 0:cf8c942ef95b 157 case ALFA :
klaute 0:cf8c942ef95b 158 audioFreq = HZ_411_1;
klaute 0:cf8c942ef95b 159 pwmPeriod = HZ_11_1;
klaute 0:cf8c942ef95b 160 ledPulseWidth = HZ_11_1_HALF;
klaute 0:cf8c942ef95b 161 break;
klaute 0:cf8c942ef95b 162 case BETA :
klaute 0:cf8c942ef95b 163 audioFreq = HZ_414_4;
klaute 0:cf8c942ef95b 164 pwmPeriod = HZ_14_4;
klaute 0:cf8c942ef95b 165 ledPulseWidth = HZ_14_4_HALF;
klaute 0:cf8c942ef95b 166 break;
klaute 0:cf8c942ef95b 167 case DELTA :
klaute 0:cf8c942ef95b 168 audioFreq = HZ_402_2;
klaute 0:cf8c942ef95b 169 pwmPeriod = HZ_2_2;
klaute 0:cf8c942ef95b 170 ledPulseWidth = HZ_2_2_HALF;
klaute 0:cf8c942ef95b 171 break;
klaute 0:cf8c942ef95b 172 case THETA :
klaute 0:cf8c942ef95b 173 audioFreq = HZ_406;
klaute 0:cf8c942ef95b 174 pwmPeriod = HZ_6;
klaute 0:cf8c942ef95b 175 ledPulseWidth = HZ_6_HALF;
klaute 0:cf8c942ef95b 176 break;
klaute 0:cf8c942ef95b 177 }
klaute 0:cf8c942ef95b 178
klaute 0:cf8c942ef95b 179 // reconfigure the audio timer
klaute 0:cf8c942ef95b 180 timer0.attach(&Audio_L_Control, HZ_400);
klaute 0:cf8c942ef95b 181 timer1.attach(&Audio_R_Control, audioFreq);
klaute 0:cf8c942ef95b 182
klaute 0:cf8c942ef95b 183 // set the new period to all channels
klaute 0:cf8c942ef95b 184 ledRight.period(pwmPeriod);
klaute 0:cf8c942ef95b 185
klaute 0:cf8c942ef95b 186 // set the new pulse width to all pwm channels
klaute 0:cf8c942ef95b 187 ledLeft.pulsewidth(ledPulseWidth);
klaute 0:cf8c942ef95b 188 ledRight.pulsewidth(ledPulseWidth);
klaute 0:cf8c942ef95b 189 ledStatusLeft.pulsewidth(ledPulseWidth);
klaute 0:cf8c942ef95b 190 ledStatusRight.pulsewidth(ledPulseWidth);
klaute 0:cf8c942ef95b 191
klaute 0:cf8c942ef95b 192 bw_pos++;
klaute 0:cf8c942ef95b 193
klaute 0:cf8c942ef95b 194 }
klaute 0:cf8c942ef95b 195
klaute 0:cf8c942ef95b 196 /*************************************************************************************************/
klaute 0:cf8c942ef95b 197
klaute 0:cf8c942ef95b 198 int main() {
klaute 0:cf8c942ef95b 199
klaute 0:cf8c942ef95b 200 audioLeft = 0;
klaute 0:cf8c942ef95b 201 audioRight = 0;
klaute 0:cf8c942ef95b 202
klaute 0:cf8c942ef95b 203 initBrainwaves();
klaute 0:cf8c942ef95b 204
klaute 0:cf8c942ef95b 205 timer2.attach(&Brainwave_Control, BRAINWAVE_CTRL_DELAY);
klaute 0:cf8c942ef95b 206
klaute 0:cf8c942ef95b 207 while (1) {
klaute 0:cf8c942ef95b 208 LEDStatus1 = 1;
klaute 0:cf8c942ef95b 209 wait(0.02);
klaute 0:cf8c942ef95b 210 LEDStatus1 = 0;
klaute 0:cf8c942ef95b 211 wait(0.75);
klaute 0:cf8c942ef95b 212 }
klaute 0:cf8c942ef95b 213 }
klaute 0:cf8c942ef95b 214
klaute 0:cf8c942ef95b 215 /*************************************************************************************************/