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

Committer:
klaute
Date:
Sat Apr 09 12:35:35 2011 +0000
Revision:
0:cf8c942ef95b
Child:
1:90922bda2b17
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 0:cf8c942ef95b 14 brainwaves[1-1].type = 1;
klaute 0:cf8c942ef95b 15 brainwaves[1-1].duration = 60;
klaute 0:cf8c942ef95b 16 brainwaves[2-1].type = 0;
klaute 0:cf8c942ef95b 17 brainwaves[2-1].duration = 10;
klaute 0:cf8c942ef95b 18 brainwaves[3-1].type = 1;
klaute 0:cf8c942ef95b 19 brainwaves[3-1].duration = 20;
klaute 0:cf8c942ef95b 20 brainwaves[4-1].type = 0;
klaute 0:cf8c942ef95b 21 brainwaves[4-1].duration = 15;
klaute 0:cf8c942ef95b 22 brainwaves[5-1].type = 1;
klaute 0:cf8c942ef95b 23 brainwaves[5-1].duration = 15;
klaute 0:cf8c942ef95b 24 brainwaves[6-1].type = 0;
klaute 0:cf8c942ef95b 25 brainwaves[6-1].duration = 20;
klaute 0:cf8c942ef95b 26 brainwaves[7-1].type = 1;
klaute 0:cf8c942ef95b 27 brainwaves[7-1].duration = 10;
klaute 0:cf8c942ef95b 28 brainwaves[8-1].type = 0;
klaute 0:cf8c942ef95b 29 brainwaves[8-1].duration = 30;
klaute 0:cf8c942ef95b 30 brainwaves[9-1].type = 1;
klaute 0:cf8c942ef95b 31 brainwaves[9-1].duration = 5;
klaute 0:cf8c942ef95b 32 brainwaves[10-1].type = 0;
klaute 0:cf8c942ef95b 33 brainwaves[10-1].duration = 60;
klaute 0:cf8c942ef95b 34 brainwaves[11-1].type = 3;
klaute 0:cf8c942ef95b 35 brainwaves[11-1].duration = 10;
klaute 0:cf8c942ef95b 36 brainwaves[12-1].type = 0;
klaute 0:cf8c942ef95b 37 brainwaves[12-1].duration = 30;
klaute 0:cf8c942ef95b 38 brainwaves[13-1].type = 3;
klaute 0:cf8c942ef95b 39 brainwaves[13-1].duration = 20;
klaute 0:cf8c942ef95b 40 brainwaves[14-1].type = 0;
klaute 0:cf8c942ef95b 41 brainwaves[14-1].duration = 30;
klaute 0:cf8c942ef95b 42 brainwaves[15-1].type = 3;
klaute 0:cf8c942ef95b 43 brainwaves[15-1].duration = 30;
klaute 0:cf8c942ef95b 44 brainwaves[16-1].type = 0;
klaute 0:cf8c942ef95b 45 brainwaves[16-1].duration = 15;
klaute 0:cf8c942ef95b 46 brainwaves[17-1].type = 3;
klaute 0:cf8c942ef95b 47 brainwaves[17-1].duration = 60;
klaute 0:cf8c942ef95b 48 brainwaves[18-1].type = 0;
klaute 0:cf8c942ef95b 49 brainwaves[18-1].duration = 15;
klaute 0:cf8c942ef95b 50 brainwaves[19-1].type = 1;
klaute 0:cf8c942ef95b 51 brainwaves[19-1].duration = 1;
klaute 0:cf8c942ef95b 52 brainwaves[20-1].type = 0;
klaute 0:cf8c942ef95b 53 brainwaves[20-1].duration = 15;
klaute 0:cf8c942ef95b 54 brainwaves[21-1].type = 3;
klaute 0:cf8c942ef95b 55 brainwaves[21-1].duration = 60;
klaute 0:cf8c942ef95b 56 brainwaves[22-1].type = 2;
klaute 0:cf8c942ef95b 57 brainwaves[22-1].duration = 1;
klaute 0:cf8c942ef95b 58 brainwaves[23-1].type = 3;
klaute 0:cf8c942ef95b 59 brainwaves[23-1].duration = 10;
klaute 0:cf8c942ef95b 60 brainwaves[24-1].type = 2;
klaute 0:cf8c942ef95b 61 brainwaves[24-1].duration = 1;
klaute 0:cf8c942ef95b 62 brainwaves[25-1].type = 3;
klaute 0:cf8c942ef95b 63 brainwaves[25-1].duration = 10;
klaute 0:cf8c942ef95b 64 brainwaves[26-1].type = 2;
klaute 0:cf8c942ef95b 65 brainwaves[26-1].duration = 1;
klaute 0:cf8c942ef95b 66 brainwaves[27-1].type = 3;
klaute 0:cf8c942ef95b 67 brainwaves[27-1].duration = 30;
klaute 0:cf8c942ef95b 68 brainwaves[28-1].type = 0;
klaute 0:cf8c942ef95b 69 brainwaves[28-1].duration = 15;
klaute 0:cf8c942ef95b 70 brainwaves[29-1].type = 1;
klaute 0:cf8c942ef95b 71 brainwaves[29-1].duration = 1;
klaute 0:cf8c942ef95b 72 brainwaves[30-1].type = 0;
klaute 0:cf8c942ef95b 73 brainwaves[30-1].duration = 15;
klaute 0:cf8c942ef95b 74 brainwaves[31-1].type = 3;
klaute 0:cf8c942ef95b 75 brainwaves[31-1].duration = 30;
klaute 0:cf8c942ef95b 76 brainwaves[32-1].type = 0;
klaute 0:cf8c942ef95b 77 brainwaves[32-1].duration = 15;
klaute 0:cf8c942ef95b 78 brainwaves[33-1].type = 1;
klaute 0:cf8c942ef95b 79 brainwaves[33-1].duration = 1;
klaute 0:cf8c942ef95b 80 brainwaves[34-1].type = 0;
klaute 0:cf8c942ef95b 81 brainwaves[34-1].duration = 20;
klaute 0:cf8c942ef95b 82 brainwaves[35-1].type = 1;
klaute 0:cf8c942ef95b 83 brainwaves[35-1].duration = 5;
klaute 0:cf8c942ef95b 84 brainwaves[36-1].type = 0;
klaute 0:cf8c942ef95b 85 brainwaves[36-1].duration = 20;
klaute 0:cf8c942ef95b 86 brainwaves[37-1].type = 1;
klaute 0:cf8c942ef95b 87 brainwaves[37-1].duration = 15;
klaute 0:cf8c942ef95b 88 brainwaves[38-1].type = 0;
klaute 0:cf8c942ef95b 89 brainwaves[38-1].duration = 15;
klaute 0:cf8c942ef95b 90 brainwaves[39-1].type = 1;
klaute 0:cf8c942ef95b 91 brainwaves[39-1].duration = 20;
klaute 0:cf8c942ef95b 92 brainwaves[40-1].type = 0;
klaute 0:cf8c942ef95b 93 brainwaves[40-1].duration = 10;
klaute 0:cf8c942ef95b 94 brainwaves[41-1].type = 1;
klaute 0:cf8c942ef95b 95 brainwaves[41-1].duration = 25;
klaute 0:cf8c942ef95b 96 brainwaves[42-1].type = 0;
klaute 0:cf8c942ef95b 97 brainwaves[42-1].duration = 5;
klaute 0:cf8c942ef95b 98 brainwaves[43-1].type = 1;
klaute 0:cf8c942ef95b 99 brainwaves[43-1].duration = 60;
klaute 0:cf8c942ef95b 100 brainwaves[44-1].isEnd = true;
klaute 0:cf8c942ef95b 101
klaute 0:cf8c942ef95b 102 }
klaute 0:cf8c942ef95b 103
klaute 0:cf8c942ef95b 104 /*************************************************************************************************/
klaute 0:cf8c942ef95b 105
klaute 0:cf8c942ef95b 106 void Audio_L_Control() {
klaute 0:cf8c942ef95b 107 // Toggle left audio channel
klaute 0:cf8c942ef95b 108 audioLeft = ( audioLeft == 0 ) ? 1 : 0;
klaute 0:cf8c942ef95b 109 }
klaute 0:cf8c942ef95b 110
klaute 0:cf8c942ef95b 111 void Audio_R_Control() {
klaute 0:cf8c942ef95b 112 // toggle right audio channel
klaute 0:cf8c942ef95b 113 audioRight = ( audioRight == 0 ) ? 1 : 0;
klaute 0:cf8c942ef95b 114 }
klaute 0:cf8c942ef95b 115
klaute 0:cf8c942ef95b 116 void Brainwave_Control() {
klaute 0:cf8c942ef95b 117
klaute 0:cf8c942ef95b 118 // check for end of brainwave sequence
klaute 0:cf8c942ef95b 119 if ( brainwaves[bw_pos].isEnd && bw_duration <= 0) {
klaute 0:cf8c942ef95b 120
klaute 0:cf8c942ef95b 121 LEDStatus2 = ( LEDStatus2 == 0 ) ? 1 : 0;
klaute 0:cf8c942ef95b 122
klaute 0:cf8c942ef95b 123 ledRight = 0;
klaute 0:cf8c942ef95b 124 ledLeft = 0;
klaute 0:cf8c942ef95b 125 ledStatusRight = 0;
klaute 0:cf8c942ef95b 126 ledStatusLeft = 0;
klaute 0:cf8c942ef95b 127
klaute 0:cf8c942ef95b 128 timer0.detach();
klaute 0:cf8c942ef95b 129 timer1.detach();
klaute 0:cf8c942ef95b 130
klaute 0:cf8c942ef95b 131 return;
klaute 0:cf8c942ef95b 132 }
klaute 0:cf8c942ef95b 133
klaute 0:cf8c942ef95b 134 // check for end of duration
klaute 0:cf8c942ef95b 135 if ( bw_duration > 0 ) {
klaute 0:cf8c942ef95b 136 bw_duration--;
klaute 0:cf8c942ef95b 137 return;
klaute 0:cf8c942ef95b 138 }
klaute 0:cf8c942ef95b 139
klaute 0:cf8c942ef95b 140 // toggle brainwave status
klaute 0:cf8c942ef95b 141 LEDStatus2 = ( LEDStatus2 == 0 ) ? 1 : 0;
klaute 0:cf8c942ef95b 142
klaute 0:cf8c942ef95b 143 bw_duration = brainwaves[bw_pos].duration;
klaute 0:cf8c942ef95b 144
klaute 0:cf8c942ef95b 145 int type = brainwaves[bw_pos].type;
klaute 0:cf8c942ef95b 146 float pwmPeriod = 0.0;
klaute 0:cf8c942ef95b 147 float ledPulseWidth = 0.0;
klaute 0:cf8c942ef95b 148 float audioFreq = 0.0;
klaute 0:cf8c942ef95b 149 switch (type) {
klaute 0:cf8c942ef95b 150 case ALFA :
klaute 0:cf8c942ef95b 151 audioFreq = HZ_411_1;
klaute 0:cf8c942ef95b 152 pwmPeriod = HZ_11_1;
klaute 0:cf8c942ef95b 153 ledPulseWidth = HZ_11_1_HALF;
klaute 0:cf8c942ef95b 154 break;
klaute 0:cf8c942ef95b 155 case BETA :
klaute 0:cf8c942ef95b 156 audioFreq = HZ_414_4;
klaute 0:cf8c942ef95b 157 pwmPeriod = HZ_14_4;
klaute 0:cf8c942ef95b 158 ledPulseWidth = HZ_14_4_HALF;
klaute 0:cf8c942ef95b 159 break;
klaute 0:cf8c942ef95b 160 case DELTA :
klaute 0:cf8c942ef95b 161 audioFreq = HZ_402_2;
klaute 0:cf8c942ef95b 162 pwmPeriod = HZ_2_2;
klaute 0:cf8c942ef95b 163 ledPulseWidth = HZ_2_2_HALF;
klaute 0:cf8c942ef95b 164 break;
klaute 0:cf8c942ef95b 165 case THETA :
klaute 0:cf8c942ef95b 166 audioFreq = HZ_406;
klaute 0:cf8c942ef95b 167 pwmPeriod = HZ_6;
klaute 0:cf8c942ef95b 168 ledPulseWidth = HZ_6_HALF;
klaute 0:cf8c942ef95b 169 break;
klaute 0:cf8c942ef95b 170 }
klaute 0:cf8c942ef95b 171
klaute 0:cf8c942ef95b 172 // reconfigure the audio timer
klaute 0:cf8c942ef95b 173 timer0.attach(&Audio_L_Control, HZ_400);
klaute 0:cf8c942ef95b 174 timer1.attach(&Audio_R_Control, audioFreq);
klaute 0:cf8c942ef95b 175
klaute 0:cf8c942ef95b 176 // set the new period to all channels
klaute 0:cf8c942ef95b 177 ledRight.period(pwmPeriod);
klaute 0:cf8c942ef95b 178
klaute 0:cf8c942ef95b 179 // set the new pulse width to all pwm channels
klaute 0:cf8c942ef95b 180 ledLeft.pulsewidth(ledPulseWidth);
klaute 0:cf8c942ef95b 181 ledRight.pulsewidth(ledPulseWidth);
klaute 0:cf8c942ef95b 182 ledStatusLeft.pulsewidth(ledPulseWidth);
klaute 0:cf8c942ef95b 183 ledStatusRight.pulsewidth(ledPulseWidth);
klaute 0:cf8c942ef95b 184
klaute 0:cf8c942ef95b 185 bw_pos++;
klaute 0:cf8c942ef95b 186
klaute 0:cf8c942ef95b 187 }
klaute 0:cf8c942ef95b 188
klaute 0:cf8c942ef95b 189 /*************************************************************************************************/
klaute 0:cf8c942ef95b 190
klaute 0:cf8c942ef95b 191 int main() {
klaute 0:cf8c942ef95b 192
klaute 0:cf8c942ef95b 193 audioLeft = 0;
klaute 0:cf8c942ef95b 194 audioRight = 0;
klaute 0:cf8c942ef95b 195
klaute 0:cf8c942ef95b 196 initBrainwaves();
klaute 0:cf8c942ef95b 197
klaute 0:cf8c942ef95b 198 timer2.attach(&Brainwave_Control, BRAINWAVE_CTRL_DELAY);
klaute 0:cf8c942ef95b 199
klaute 0:cf8c942ef95b 200 while (1) {
klaute 0:cf8c942ef95b 201 LEDStatus1 = 1;
klaute 0:cf8c942ef95b 202 wait(0.02);
klaute 0:cf8c942ef95b 203 LEDStatus1 = 0;
klaute 0:cf8c942ef95b 204 wait(0.75);
klaute 0:cf8c942ef95b 205 }
klaute 0:cf8c942ef95b 206 }
klaute 0:cf8c942ef95b 207
klaute 0:cf8c942ef95b 208 /*************************************************************************************************/