~

Dependencies:   4DGL-uLCD-SE SDFileSystem

Committer:
fkhan39
Date:
Mon Sep 26 19:15:11 2016 +0000
Revision:
11:eca6295235e6
Parent:
10:98f119e9582a
Child:
12:a2908fdf3b66
serial port;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fkhan39 0:570683b2d0c9 1 #include "mbed.h"
fkhan39 6:708e878d4374 2 #include "uLCD_4DGL.h"
fkhan39 6:708e878d4374 3 #include "LSM9DS1.h"
fkhan39 7:1a21d8290cf1 4 #include "math.h"
fkhan39 7:1a21d8290cf1 5 //#include "SongPlayer.h"
fkhan39 7:1a21d8290cf1 6 #include <fstream>
fkhan39 7:1a21d8290cf1 7 #include <string>
fkhan39 7:1a21d8290cf1 8 #include "SDFileSystem.h"
fkhan39 0:570683b2d0c9 9
fkhan39 7:1a21d8290cf1 10
fkhan39 9:63c3c734a620 11 /* serial
kswanson31 5:151b0fa1fb44 12 Serial pc(USBTX, USBRX);
fkhan39 7:1a21d8290cf1 13 */
fkhan39 7:1a21d8290cf1 14
fkhan39 9:63c3c734a620 15 /* PART 1
kswanson31 5:151b0fa1fb44 16 AnalogOut aout(p18);
kswanson31 5:151b0fa1fb44 17 AnalogIn ain(p15);
kswanson31 5:151b0fa1fb44 18 DigitalOut led1(LED1);
kswanson31 5:151b0fa1fb44 19 DigitalOut led2(LED2);
kswanson31 5:151b0fa1fb44 20 DigitalOut led3(LED3);
kswanson31 5:151b0fa1fb44 21 DigitalOut led4(LED4);
kswanson31 5:151b0fa1fb44 22 */
kswanson31 5:151b0fa1fb44 23
fkhan39 11:eca6295235e6 24 // PART 3
fkhan39 11:eca6295235e6 25
fkhan39 11:eca6295235e6 26 Serial pc(p13, p14); // TX, RX
fkhan39 9:63c3c734a620 27
fkhan39 9:63c3c734a620 28
fkhan39 9:63c3c734a620 29 /* PART 8
fkhan39 4:f5bc5d9a790b 30 BusOut myleds(LED1,LED2,LED3,LED4);
kswanson31 5:151b0fa1fb44 31
fkhan39 4:f5bc5d9a790b 32 class microphone
fkhan39 4:f5bc5d9a790b 33 {
fkhan39 4:f5bc5d9a790b 34 public :
fkhan39 4:f5bc5d9a790b 35 microphone(PinName pin);
fkhan39 4:f5bc5d9a790b 36 float read();
fkhan39 4:f5bc5d9a790b 37 operator float ();
fkhan39 4:f5bc5d9a790b 38 private :
fkhan39 4:f5bc5d9a790b 39 AnalogIn _pin;
fkhan39 4:f5bc5d9a790b 40 };
fkhan39 4:f5bc5d9a790b 41 microphone::microphone (PinName pin):
fkhan39 4:f5bc5d9a790b 42 _pin(pin)
fkhan39 4:f5bc5d9a790b 43 {
fkhan39 4:f5bc5d9a790b 44 }
fkhan39 4:f5bc5d9a790b 45 float microphone::read()
fkhan39 4:f5bc5d9a790b 46 {
fkhan39 4:f5bc5d9a790b 47 return _pin.read();
fkhan39 4:f5bc5d9a790b 48 }
fkhan39 4:f5bc5d9a790b 49 inline microphone::operator float ()
fkhan39 4:f5bc5d9a790b 50 {
fkhan39 4:f5bc5d9a790b 51 return _pin.read();
fkhan39 4:f5bc5d9a790b 52 }
fkhan39 4:f5bc5d9a790b 53
fkhan39 4:f5bc5d9a790b 54 microphone mymicrophone(p16);
kswanson31 5:151b0fa1fb44 55 */
fkhan39 0:570683b2d0c9 56
fkhan39 9:63c3c734a620 57 /* PART 4
fkhan39 9:63c3c734a620 58 uLCD_4DGL lcd(p13,p14,p12);
fkhan39 9:63c3c734a620 59 int ax, ay;
fkhan39 9:63c3c734a620 60 */
fkhan39 11:eca6295235e6 61
fkhan39 11:eca6295235e6 62 /*PART 9, SD CARD
fkhan39 9:63c3c734a620 63 SDFileSystem sd(p5, p6, p7, p8, "sd");
fkhan39 9:63c3c734a620 64 */
fkhan39 11:eca6295235e6 65
fkhan39 9:63c3c734a620 66 /* PART 9B, SOUND
fkhan39 9:63c3c734a620 67 float note[16]= {329.628,329.628,329.628,349.228,391.995,391.995,391.995,349.228,
fkhan39 9:63c3c734a620 68 329.628,349.228,391.995,349.228,329.628,293.665,329.628,349.228
fkhan39 9:63c3c734a620 69 };
fkhan39 9:63c3c734a620 70 float duration[16]= {0.48,0.12,0.48,0.12,0.48,0.12,0.12,0.12,
fkhan39 9:63c3c734a620 71 0.12,0.12,0.12,0.12,0.48,0.12,1.68,0.12
fkhan39 9:63c3c734a620 72 };
fkhan39 9:63c3c734a620 73 */
fkhan39 9:63c3c734a620 74
fkhan39 9:63c3c734a620 75
fkhan39 9:63c3c734a620 76 int main() {
fkhan39 9:63c3c734a620 77
fkhan39 9:63c3c734a620 78 /* PART 1
fkhan39 1:4ed94566512a 79 const double pi = 3.141592653589793238462;
fkhan39 1:4ed94566512a 80 const double amplitude = 1.0f;
fkhan39 1:4ed94566512a 81 const double offset = 65535/2; // 0xFFFF/2
fkhan39 1:4ed94566512a 82 double rads = 0.0;
fkhan39 1:4ed94566512a 83 uint16_t sample = 0;
kswanson31 5:151b0fa1fb44 84 */
fkhan39 9:63c3c734a620 85
fkhan39 11:eca6295235e6 86 // PART 4, BUBBLE, PART 3
fkhan39 9:63c3c734a620 87 LSM9DS1 imu(p9, p10, 0xD6, 0x3C);
fkhan39 9:63c3c734a620 88 imu.begin();
fkhan39 9:63c3c734a620 89 if (!imu.begin()) {
fkhan39 11:eca6295235e6 90 pc.printf("Failed to communicate with imu9DS1.\n");
fkhan39 9:63c3c734a620 91 }
fkhan39 9:63c3c734a620 92 imu.calibrate();
fkhan39 11:eca6295235e6 93
fkhan39 9:63c3c734a620 94
fkhan39 9:63c3c734a620 95 /* PART 9, SD CARD
fkhan39 9:63c3c734a620 96 mkdir("/sd/mydir", 0777);
fkhan39 9:63c3c734a620 97 FILE *fp = fopen("/sd/mydir/sdtest.txt", "w");
fkhan39 9:63c3c734a620 98 if(fp == NULL) {
fkhan39 9:63c3c734a620 99 error("Could not open file for write\n");
fkhan39 9:63c3c734a620 100 }
fkhan39 9:63c3c734a620 101 fprintf(fp, "Hello SD file World!");
fkhan39 9:63c3c734a620 102 fclose(fp);
fkhan39 9:63c3c734a620 103
fkhan39 9:63c3c734a620 104 std::ifstream file("/sd/mydir/sdtest.txt");
fkhan39 9:63c3c734a620 105 string str;
fkhan39 9:63c3c734a620 106
fkhan39 9:63c3c734a620 107 while (std::getline(file, str))
fkhan39 9:63c3c734a620 108 {
fkhan39 9:63c3c734a620 109 const char * c = str.c_str();
fkhan39 9:63c3c734a620 110 pc.printf(c);
fkhan39 9:63c3c734a620 111 }
fkhan39 9:63c3c734a620 112 */
fkhan39 9:63c3c734a620 113
fkhan39 9:63c3c734a620 114 /* PART 9, SPEAKER
fkhan39 9:63c3c734a620 115 // setup instance of new SongPlayer class, mySpeaker using pin 26
fkhan39 9:63c3c734a620 116 // the pin must be a PWM output pin
fkhan39 9:63c3c734a620 117 SongPlayer mySpeaker(p26);
fkhan39 9:63c3c734a620 118 // Start song and return once playing starts
fkhan39 9:63c3c734a620 119 mySpeaker.PlaySong(note,duration);
fkhan39 9:63c3c734a620 120 double theta;
fkhan39 9:63c3c734a620 121 */
fkhan39 9:63c3c734a620 122
fkhan39 9:63c3c734a620 123 while (1) {
fkhan39 9:63c3c734a620 124
fkhan39 9:63c3c734a620 125 /* PART 1
fkhan39 9:63c3c734a620 126
fkhan39 9:63c3c734a620 127 // analog out
fkhan39 9:63c3c734a620 128 for (int i = 0; i < 720; i++) {
fkhan39 9:63c3c734a620 129 rads = (pi * i) / 180.0f;
fkhan39 9:63c3c734a620 130 sample = (uint16_t)(amplitude * (offset * (cos(rads + pi))) + offset);
fkhan39 9:63c3c734a620 131 aout.write_u16(sample);
fkhan39 9:63c3c734a620 132 }
fkhan39 9:63c3c734a620 133
fkhan39 9:63c3c734a620 134 // analog in
fkhan39 9:63c3c734a620 135 led1 = (ain > 0.2f) ? 1 : 0;
fkhan39 9:63c3c734a620 136 led2 = (ain > 0.4f) ? 1 : 0;
fkhan39 9:63c3c734a620 137 led3 = (ain > 0.6f) ? 1 : 0;
fkhan39 9:63c3c734a620 138 led4 = (ain > 0.8f) ? 1 : 0;
fkhan39 9:63c3c734a620 139 wait(.01);
fkhan39 9:63c3c734a620 140 */
fkhan39 9:63c3c734a620 141
fkhan39 9:63c3c734a620 142 /* PART 8
fkhan39 9:63c3c734a620 143 //read in, subtract 0.67 DC bias, take absolute value, and scale up .1Vpp to 15 for builtin LED display
fkhan39 9:63c3c734a620 144 myleds = int(abs((mymicrophone - (0.67/3.3)))*500.0);
fkhan39 9:63c3c734a620 145 //Use an 8kHz audio sample rate (phone quality audio);
fkhan39 9:63c3c734a620 146 wait(1.0/8000.0);
fkhan39 9:63c3c734a620 147 */
fkhan39 9:63c3c734a620 148
fkhan39 11:eca6295235e6 149 // PART 3
fkhan39 11:eca6295235e6 150
fkhan39 11:eca6295235e6 151
fkhan39 11:eca6295235e6 152 imu.readAccel();
fkhan39 11:eca6295235e6 153 imu.readMag();
fkhan39 11:eca6295235e6 154 imu.readGyro();
fkhan39 11:eca6295235e6 155
fkhan39 11:eca6295235e6 156
fkhan39 11:eca6295235e6 157 pc.printf("gyro: %d %d %d\n\r", imu.gx, imu.gy, imu.gz);
fkhan39 11:eca6295235e6 158 pc.printf("accel: %d %d %d\n\r", imu.ax, imu.ay, imu.az);
fkhan39 11:eca6295235e6 159 pc.printf("mag: %d %d %d\n\n\r", imu.mx, imu.my, imu.mz);
fkhan39 11:eca6295235e6 160
fkhan39 9:63c3c734a620 161 /* PART 4, BUBBLE/COMPASS
fkhan39 9:63c3c734a620 162
fkhan39 9:63c3c734a620 163 imu.readAccel();
fkhan39 9:63c3c734a620 164 imu.readMag();
fkhan39 9:63c3c734a620 165 imu.readGyro();
kswanson31 3:99acbebbed6c 166
fkhan39 9:63c3c734a620 167 lcd.filled_circle(64 + ax / 250, 64 + ay/ 250 , 8, BLACK);
fkhan39 9:63c3c734a620 168 ax = imu.ax, ay = imu.ay;
fkhan39 9:63c3c734a620 169 lcd.filled_circle(64 + ax / 250, 64 + ay/ 250 , 8, WHITE);
fkhan39 9:63c3c734a620 170 lcd.circle(64, 64, 10, WHITE);
fkhan39 9:63c3c734a620 171
fkhan39 9:63c3c734a620 172 // compass, PART 4 extra credit
fkhan39 9:63c3c734a620 173
fkhan39 9:63c3c734a620 174 lcd.line(64,64,64+60*cos(theta), 64+60*sin(theta), BLACK);
fkhan39 9:63c3c734a620 175 theta = atan2((double)imu.mx, (double)imu.my);
fkhan39 9:63c3c734a620 176 lcd.line(64,64,64+60*cos(theta), 64+60*sin(theta), GREEN);
fkhan39 9:63c3c734a620 177 lcd.locate(0,1);
fkhan39 9:63c3c734a620 178 lcd.printf("%f",180 * theta / 3.14159);
fkhan39 9:63c3c734a620 179
fkhan39 9:63c3c734a620 180 */
fkhan39 9:63c3c734a620 181
fkhan39 9:63c3c734a620 182 }
fkhan39 9:63c3c734a620 183 }
fkhan39 10:98f119e9582a 184
fkhan39 10:98f119e9582a 185
fkhan39 10:98f119e9582a 186
fkhan39 10:98f119e9582a 187 /*
fkhan39 10:98f119e9582a 188 ,
fkhan39 10:98f119e9582a 189 / \
fkhan39 10:98f119e9582a 190 { }
fkhan39 10:98f119e9582a 191 p !
fkhan39 10:98f119e9582a 192 ; : ;
fkhan39 10:98f119e9582a 193 | : |
fkhan39 10:98f119e9582a 194 | : |
fkhan39 10:98f119e9582a 195 l ; l
fkhan39 10:98f119e9582a 196 l ; l
fkhan39 10:98f119e9582a 197 I ; I
fkhan39 10:98f119e9582a 198 I ; I
fkhan39 10:98f119e9582a 199 I ; I
fkhan39 10:98f119e9582a 200 I ; I
fkhan39 10:98f119e9582a 201 d | b
fkhan39 10:98f119e9582a 202 H | H
fkhan39 10:98f119e9582a 203 H | H
fkhan39 10:98f119e9582a 204 H I H
fkhan39 10:98f119e9582a 205 ,;, H I H ,;,
fkhan39 10:98f119e9582a 206 ;H@H; ;_H_;, ;H@H;
fkhan39 10:98f119e9582a 207 `\Y/d_,;|4H@HK|;,_b\Y/'
fkhan39 10:98f119e9582a 208 '\;MMMMM$@@@$MMMMM;/'
fkhan39 10:98f119e9582a 209 "~~~*;!8@8!;*~~~"
fkhan39 10:98f119e9582a 210 ;888;
fkhan39 10:98f119e9582a 211 ;888;
fkhan39 10:98f119e9582a 212 ;888;
fkhan39 10:98f119e9582a 213 ;888;
fkhan39 10:98f119e9582a 214 d8@8b
fkhan39 10:98f119e9582a 215 O8@8O
fkhan39 10:98f119e9582a 216 T808T
fkhan39 10:98f119e9582a 217 `~`
fkhan39 10:98f119e9582a 218 */