3 fuc:fat sd eth
Dependencies: FATFileSystem SDFileSystem WIZnetInterface mbed
Fork of adc_dac by
main.cpp@4:e3fceccc589e, 2016-11-11 (annotated)
- Committer:
- jh_ndm
- Date:
- Fri Nov 11 06:38:37 2016 +0000
- Revision:
- 4:e3fceccc589e
- Parent:
- 3:4132f3ef7d22
eth&sd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jh_ndm | 4:e3fceccc589e | 1 | /* |
jh_ndm | 4:e3fceccc589e | 2 | #include "mbed.h" |
jh_ndm | 4:e3fceccc589e | 3 | AnalogIn battery(A3); |
jh_ndm | 4:e3fceccc589e | 4 | Serial uart1(PA_13,PA_14); |
jh_ndm | 4:e3fceccc589e | 5 | int main(){ |
jh_ndm | 4:e3fceccc589e | 6 | while(1){ |
jh_ndm | 4:e3fceccc589e | 7 | if(battery > 0.5){ |
jh_ndm | 4:e3fceccc589e | 8 | uart1.printf("%02f\n",battery.read()*3.3); |
jh_ndm | 4:e3fceccc589e | 9 | wait(1); |
jh_ndm | 4:e3fceccc589e | 10 | } |
jh_ndm | 4:e3fceccc589e | 11 | } |
jh_ndm | 4:e3fceccc589e | 12 | } |
jh_ndm | 4:e3fceccc589e | 13 | */ |
jh_ndm | 0:695e35fce077 | 14 | |
jh_ndm | 4:e3fceccc589e | 15 | /********uart1 demo******** |
jh_ndm | 4:e3fceccc589e | 16 | #include "mbed.h" |
jh_ndm | 4:e3fceccc589e | 17 | |
jh_ndm | 4:e3fceccc589e | 18 | Serial uart1(PA_13, PA_14); // tx, rx |
jh_ndm | 0:695e35fce077 | 19 | |
jh_ndm | 4:e3fceccc589e | 20 | int main() { |
jh_ndm | 4:e3fceccc589e | 21 | uart1.printf("Please input something\n"); |
jh_ndm | 4:e3fceccc589e | 22 | |
jh_ndm | 4:e3fceccc589e | 23 | while(1) { |
jh_ndm | 4:e3fceccc589e | 24 | char c = uart1.getc(); |
jh_ndm | 4:e3fceccc589e | 25 | uart1.printf("%s\n",c); |
jh_ndm | 4:e3fceccc589e | 26 | } |
jh_ndm | 4:e3fceccc589e | 27 | } |
jh_ndm | 4:e3fceccc589e | 28 | */ |
jh_ndm | 4:e3fceccc589e | 29 | |
jh_ndm | 4:e3fceccc589e | 30 | /********uart2 demo******** |
jh_ndm | 4:e3fceccc589e | 31 | #include "mbed.h" |
jh_ndm | 4:e3fceccc589e | 32 | |
jh_ndm | 4:e3fceccc589e | 33 | Serial uart2(PC_10, PC_11); // tx, rx |
jh_ndm | 4:e3fceccc589e | 34 | |
jh_ndm | 4:e3fceccc589e | 35 | int main() { |
jh_ndm | 4:e3fceccc589e | 36 | uart2.printf("Please input something\n"); |
jh_ndm | 4:e3fceccc589e | 37 | |
jh_ndm | 4:e3fceccc589e | 38 | while(1) { |
jh_ndm | 4:e3fceccc589e | 39 | char c = uart2.getc(); |
jh_ndm | 4:e3fceccc589e | 40 | uart2.printf("%s\n",c); |
jh_ndm | 4:e3fceccc589e | 41 | } |
jh_ndm | 4:e3fceccc589e | 42 | } |
jh_ndm | 4:e3fceccc589e | 43 | */ |
jh_ndm | 4:e3fceccc589e | 44 | |
jh_ndm | 4:e3fceccc589e | 45 | /**********Digital demo********** |
jh_ndm | 4:e3fceccc589e | 46 | #include "mbed.h" |
jh_ndm | 4:e3fceccc589e | 47 | |
jh_ndm | 4:e3fceccc589e | 48 | DigitalOut num1(D10); |
jh_ndm | 4:e3fceccc589e | 49 | DigitalOut num2(D11); |
jh_ndm | 4:e3fceccc589e | 50 | DigitalOut num3(D12); |
jh_ndm | 4:e3fceccc589e | 51 | DigitalOut num4(D2); |
jh_ndm | 4:e3fceccc589e | 52 | DigitalOut num5(D3); |
jh_ndm | 4:e3fceccc589e | 53 | DigitalOut num6(D5); |
jh_ndm | 4:e3fceccc589e | 54 | DigitalOut num7(D6); |
jh_ndm | 4:e3fceccc589e | 55 | |
jh_ndm | 4:e3fceccc589e | 56 | int main(){ |
jh_ndm | 4:e3fceccc589e | 57 | while(1){ |
jh_ndm | 4:e3fceccc589e | 58 | wait(1); |
jh_ndm | 4:e3fceccc589e | 59 | num1 = 1; |
jh_ndm | 4:e3fceccc589e | 60 | num2 = 1; |
jh_ndm | 4:e3fceccc589e | 61 | num3 = 1; |
jh_ndm | 4:e3fceccc589e | 62 | |
jh_ndm | 4:e3fceccc589e | 63 | wait(1); |
jh_ndm | 4:e3fceccc589e | 64 | num1 = 0; |
jh_ndm | 4:e3fceccc589e | 65 | num2 = 0; |
jh_ndm | 4:e3fceccc589e | 66 | num3 = 0; |
jh_ndm | 4:e3fceccc589e | 67 | |
jh_ndm | 4:e3fceccc589e | 68 | } |
jh_ndm | 4:e3fceccc589e | 69 | } |
jh_ndm | 4:e3fceccc589e | 70 | /* */ |
jh_ndm | 4:e3fceccc589e | 71 | |
jh_ndm | 4:e3fceccc589e | 72 | /**********analog demo*********** |
jh_ndm | 4:e3fceccc589e | 73 | #include "mbed.h" |
jh_ndm | 4:e3fceccc589e | 74 | AnalogIn battery5(A0); |
jh_ndm | 4:e3fceccc589e | 75 | AnalogIn battery4(PC_9); |
jh_ndm | 4:e3fceccc589e | 76 | AnalogIn battery3(D5); |
jh_ndm | 4:e3fceccc589e | 77 | Serial uart1(PA_13,PA_14); |
jh_ndm | 4:e3fceccc589e | 78 | //Serial pc(USBTX,USBRX); |
jh_ndm | 4:e3fceccc589e | 79 | int main(){ |
jh_ndm | 4:e3fceccc589e | 80 | while(1){ |
jh_ndm | 4:e3fceccc589e | 81 | //if(battery3 > 0.5){ |
jh_ndm | 4:e3fceccc589e | 82 | // uart1.printf("%f\n",battery3.read()*3.3); |
jh_ndm | 4:e3fceccc589e | 83 | // wait(0.2); |
jh_ndm | 4:e3fceccc589e | 84 | // } |
jh_ndm | 4:e3fceccc589e | 85 | //if(battery4 > 0.5){ |
jh_ndm | 4:e3fceccc589e | 86 | // uart1.printf("%f\n",battery4.read()); |
jh_ndm | 4:e3fceccc589e | 87 | //wait(0.2); |
jh_ndm | 4:e3fceccc589e | 88 | //} |
jh_ndm | 4:e3fceccc589e | 89 | if(battery5 > 0.5){ |
jh_ndm | 4:e3fceccc589e | 90 | uart1.printf("%f\n",battery5.read()); |
jh_ndm | 4:e3fceccc589e | 91 | wait(0.2); |
jh_ndm | 4:e3fceccc589e | 92 | } |
jh_ndm | 4:e3fceccc589e | 93 | } |
jh_ndm | 4:e3fceccc589e | 94 | } |
jh_ndm | 4:e3fceccc589e | 95 | */ |
jh_ndm | 4:e3fceccc589e | 96 | |
jh_ndm | 4:e3fceccc589e | 97 | /* |
jh_ndm | 4:e3fceccc589e | 98 | #include"mbed.h" |
jh_ndm | 4:e3fceccc589e | 99 | AnalogIn battery(A2); |
jh_ndm | 4:e3fceccc589e | 100 | Serial uart1(PA_13,PA_14); |
jh_ndm | 4:e3fceccc589e | 101 | int main(){ |
jh_ndm | 4:e3fceccc589e | 102 | while(1){ |
jh_ndm | 4:e3fceccc589e | 103 | if(battery > 0.5){ |
jh_ndm | 4:e3fceccc589e | 104 | uart1.printf("%f",battery.read()); |
jh_ndm | 4:e3fceccc589e | 105 | wait(1); |
jh_ndm | 4:e3fceccc589e | 106 | } |
jh_ndm | 4:e3fceccc589e | 107 | } |
jh_ndm | 4:e3fceccc589e | 108 | } |
jh_ndm | 4:e3fceccc589e | 109 | */ |
jh_ndm | 4:e3fceccc589e | 110 | /* |
jh_ndm | 0:695e35fce077 | 111 | #include "mbed.h" |
jh_ndm | 0:695e35fce077 | 112 | |
jh_ndm | 2:b67216fc6104 | 113 | Serial uart1(PA_13, PA_14); // tx, rx |
jh_ndm | 2:b67216fc6104 | 114 | Serial uart2(PC_10, PC_11); // tx, rx |
jh_ndm | 3:4132f3ef7d22 | 115 | DigitalOut myD1(D10); |
jh_ndm | 3:4132f3ef7d22 | 116 | DigitalOut myD2(D11); |
jh_ndm | 3:4132f3ef7d22 | 117 | DigitalOut myD3(D12); |
jh_ndm | 3:4132f3ef7d22 | 118 | DigitalOut myD4(D2); |
jh_ndm | 3:4132f3ef7d22 | 119 | AnalogIn myA1(A1); |
jh_ndm | 3:4132f3ef7d22 | 120 | AnalogIn myA2(A2); |
jh_ndm | 3:4132f3ef7d22 | 121 | AnalogIn myA3(A3); |
jh_ndm | 2:b67216fc6104 | 122 | PwmOut PWM1(D3); |
jh_ndm | 2:b67216fc6104 | 123 | PwmOut PWM2(D5); |
jh_ndm | 2:b67216fc6104 | 124 | PwmOut PWM3(D6); |
jh_ndm | 3:4132f3ef7d22 | 125 | |
jh_ndm | 3:4132f3ef7d22 | 126 | Timeout timeout; |
jh_ndm | 3:4132f3ef7d22 | 127 | |
jh_ndm | 3:4132f3ef7d22 | 128 | void attimeout() |
jh_ndm | 3:4132f3ef7d22 | 129 | { |
jh_ndm | 3:4132f3ef7d22 | 130 | myD1 = 0; |
jh_ndm | 3:4132f3ef7d22 | 131 | myD2 = 0; |
jh_ndm | 3:4132f3ef7d22 | 132 | myD3 = 0; |
jh_ndm | 3:4132f3ef7d22 | 133 | myD4 = 0; |
jh_ndm | 3:4132f3ef7d22 | 134 | } |
jh_ndm | 0:695e35fce077 | 135 | |
jh_ndm | 3:4132f3ef7d22 | 136 | int main() { |
jh_ndm | 3:4132f3ef7d22 | 137 | uart1.baud(115200); |
jh_ndm | 3:4132f3ef7d22 | 138 | uart2.baud(115200); |
jh_ndm | 3:4132f3ef7d22 | 139 | PWM1.period_us(500); |
jh_ndm | 3:4132f3ef7d22 | 140 | PWM2.period_us(500); |
jh_ndm | 3:4132f3ef7d22 | 141 | PWM3.period_us(500); |
jh_ndm | 3:4132f3ef7d22 | 142 | PWM1 = 0.2; |
jh_ndm | 3:4132f3ef7d22 | 143 | PWM2 = 0.5; |
jh_ndm | 3:4132f3ef7d22 | 144 | PWM3 = 0.8; |
jh_ndm | 3:4132f3ef7d22 | 145 | uart2.printf("uart2:for Loopback test,please input something\n"); |
jh_ndm | 3:4132f3ef7d22 | 146 | uart1.printf("start testing uart1 digitalOut and PWM and analogIn\n"); |
jh_ndm | 0:695e35fce077 | 147 | while(1){ |
jh_ndm | 3:4132f3ef7d22 | 148 | while(uart2.readable()){ |
jh_ndm | 3:4132f3ef7d22 | 149 | uart2.printf("%c", uart2.getc()); |
jh_ndm | 2:b67216fc6104 | 150 | } |
jh_ndm | 2:b67216fc6104 | 151 | |
jh_ndm | 3:4132f3ef7d22 | 152 | timeout.attach(&attimeout,2); |
jh_ndm | 3:4132f3ef7d22 | 153 | |
jh_ndm | 3:4132f3ef7d22 | 154 | myD1 = 1; |
jh_ndm | 3:4132f3ef7d22 | 155 | myD2 = 1; |
jh_ndm | 3:4132f3ef7d22 | 156 | myD3 = 1; |
jh_ndm | 3:4132f3ef7d22 | 157 | myD4 = 1; |
jh_ndm | 3:4132f3ef7d22 | 158 | wait(0.25); |
jh_ndm | 3:4132f3ef7d22 | 159 | |
jh_ndm | 3:4132f3ef7d22 | 160 | uart1.printf("A1:%02f\n",myA1.read()*3.3); |
jh_ndm | 3:4132f3ef7d22 | 161 | uart1.printf("A2:%02f\n",myA2.read()*3.3); |
jh_ndm | 3:4132f3ef7d22 | 162 | uart1.printf("A3:%02f\n",myA3.read()*3.3); |
jh_ndm | 2:b67216fc6104 | 163 | } |
jh_ndm | 0:695e35fce077 | 164 | } |
jh_ndm | 4:e3fceccc589e | 165 | */ |
jh_ndm | 4:e3fceccc589e | 166 | |
jh_ndm | 4:e3fceccc589e | 167 | #include "mbed.h" |
jh_ndm | 4:e3fceccc589e | 168 | #include <Serial.h> |
jh_ndm | 4:e3fceccc589e | 169 | #include "EthernetInterface.h" |
jh_ndm | 4:e3fceccc589e | 170 | #include "SDFileSystem.h" |
jh_ndm | 4:e3fceccc589e | 171 | #include <stdio.h> |
jh_ndm | 4:e3fceccc589e | 172 | #include <string.h> |
jh_ndm | 4:e3fceccc589e | 173 | |
jh_ndm | 4:e3fceccc589e | 174 | #define ADXL345_I2C_READ 0xA7 |
jh_ndm | 4:e3fceccc589e | 175 | #define ADXL345_I2C_WRITE 0xA6 |
jh_ndm | 4:e3fceccc589e | 176 | #define ADXL345_I2C_ADDRESS 0x53 |
jh_ndm | 4:e3fceccc589e | 177 | #define ADXL345_DEVID_REG 0x00 |
jh_ndm | 4:e3fceccc589e | 178 | #define ADXL345_DATAX0_REG 0x32 |
jh_ndm | 4:e3fceccc589e | 179 | #define ADXL345_POWER_CTL_REG 0x2D |
jh_ndm | 4:e3fceccc589e | 180 | #define MeasurementMode 0x08 |
jh_ndm | 4:e3fceccc589e | 181 | |
jh_ndm | 4:e3fceccc589e | 182 | #define MAC "\x00\x08\xDC\x11\x34\x78" |
jh_ndm | 4:e3fceccc589e | 183 | #define IP "192.168.1.19" |
jh_ndm | 4:e3fceccc589e | 184 | #define MASK "255.255.255.0" |
jh_ndm | 4:e3fceccc589e | 185 | #define GATEWAY "192.168.1.1" |
jh_ndm | 4:e3fceccc589e | 186 | |
jh_ndm | 4:e3fceccc589e | 187 | #define HTTPD_SERVER_PORT 80 |
jh_ndm | 4:e3fceccc589e | 188 | #define HTTPD_MAX_REQ_LENGTH 1023 |
jh_ndm | 4:e3fceccc589e | 189 | #define HTTPD_MAX_HDR_LENGTH 255 |
jh_ndm | 4:e3fceccc589e | 190 | #define HTTPD_MAX_FNAME_LENGTH 127 |
jh_ndm | 4:e3fceccc589e | 191 | #define HTTPD_MAX_DNAME_LENGTH 127 |
jh_ndm | 4:e3fceccc589e | 192 | |
jh_ndm | 4:e3fceccc589e | 193 | SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // WIZwiki-W7500 |
jh_ndm | 4:e3fceccc589e | 194 | EthernetInterface eth; |
jh_ndm | 4:e3fceccc589e | 195 | TCPSocketServer server; |
jh_ndm | 4:e3fceccc589e | 196 | TCPSocketConnection client; |
jh_ndm | 4:e3fceccc589e | 197 | char buffer[HTTPD_MAX_REQ_LENGTH+1]; |
jh_ndm | 4:e3fceccc589e | 198 | char httpHeader[HTTPD_MAX_HDR_LENGTH+1]; |
jh_ndm | 4:e3fceccc589e | 199 | char fileName[HTTPD_MAX_FNAME_LENGTH+1]; |
jh_ndm | 4:e3fceccc589e | 200 | char dirName[HTTPD_MAX_DNAME_LENGTH+1]; |
jh_ndm | 4:e3fceccc589e | 201 | char *uristr; |
jh_ndm | 4:e3fceccc589e | 202 | char *eou; |
jh_ndm | 4:e3fceccc589e | 203 | char *qrystr; |
jh_ndm | 4:e3fceccc589e | 204 | FILE *fp; |
jh_ndm | 4:e3fceccc589e | 205 | int rdCnt; |
jh_ndm | 4:e3fceccc589e | 206 | DigitalOut led1(D10); //server listning status |
jh_ndm | 4:e3fceccc589e | 207 | DigitalOut led2(D11); //socket connecting status |
jh_ndm | 4:e3fceccc589e | 208 | Ticker ledTick; |
jh_ndm | 4:e3fceccc589e | 209 | |
jh_ndm | 4:e3fceccc589e | 210 | I2C adxl345(PA_10,PA_09); |
jh_ndm | 4:e3fceccc589e | 211 | int16_t xyz[3] = {0, 0, 0}; |
jh_ndm | 4:e3fceccc589e | 212 | |
jh_ndm | 4:e3fceccc589e | 213 | Serial uart0(PA_13, PA_14); // tx, rx |
jh_ndm | 4:e3fceccc589e | 214 | Serial uart2(PC_10, PC_11); // tx, rx |
jh_ndm | 4:e3fceccc589e | 215 | //DigitalOut myD1(D10); |
jh_ndm | 4:e3fceccc589e | 216 | //DigitalOut myD2(D11); |
jh_ndm | 4:e3fceccc589e | 217 | DigitalOut myD3(D12); |
jh_ndm | 4:e3fceccc589e | 218 | DigitalOut myD4(D2); |
jh_ndm | 4:e3fceccc589e | 219 | AnalogIn myA1(A1); |
jh_ndm | 4:e3fceccc589e | 220 | AnalogIn myA2(A2); |
jh_ndm | 4:e3fceccc589e | 221 | AnalogIn myA3(A3); |
jh_ndm | 4:e3fceccc589e | 222 | PwmOut PWM1(D3); |
jh_ndm | 4:e3fceccc589e | 223 | PwmOut PWM2(D5); |
jh_ndm | 4:e3fceccc589e | 224 | PwmOut PWM3(D6); |
jh_ndm | 4:e3fceccc589e | 225 | DigitalIn mybutton(PC_06); |
jh_ndm | 4:e3fceccc589e | 226 | |
jh_ndm | 4:e3fceccc589e | 227 | Timeout digitalTimeout; |
jh_ndm | 4:e3fceccc589e | 228 | Timeout analogTimeout; |
jh_ndm | 0:695e35fce077 | 229 | |
jh_ndm | 0:695e35fce077 | 230 | |
jh_ndm | 4:e3fceccc589e | 231 | void attimeoutD() |
jh_ndm | 4:e3fceccc589e | 232 | { |
jh_ndm | 4:e3fceccc589e | 233 | static int pos = 0; |
jh_ndm | 4:e3fceccc589e | 234 | if(pos == 0){ |
jh_ndm | 4:e3fceccc589e | 235 | //myD1 = 0; |
jh_ndm | 4:e3fceccc589e | 236 | //myD2 = 0; |
jh_ndm | 4:e3fceccc589e | 237 | myD3 = 0; |
jh_ndm | 4:e3fceccc589e | 238 | myD4 = 0; |
jh_ndm | 4:e3fceccc589e | 239 | }else{ |
jh_ndm | 4:e3fceccc589e | 240 | //myD1 = 1; |
jh_ndm | 4:e3fceccc589e | 241 | //myD2 = 1; |
jh_ndm | 4:e3fceccc589e | 242 | myD3 = 1; |
jh_ndm | 4:e3fceccc589e | 243 | myD4 = 1; |
jh_ndm | 4:e3fceccc589e | 244 | } |
jh_ndm | 4:e3fceccc589e | 245 | pos = !pos; |
jh_ndm | 4:e3fceccc589e | 246 | digitalTimeout.attach(&attimeoutD,0.5); |
jh_ndm | 4:e3fceccc589e | 247 | } |
jh_ndm | 4:e3fceccc589e | 248 | void attimeoutA() |
jh_ndm | 4:e3fceccc589e | 249 | { |
jh_ndm | 4:e3fceccc589e | 250 | uart0.printf("A1:%02fV ",myA1.read()*3.3); |
jh_ndm | 4:e3fceccc589e | 251 | uart0.printf("A2:%02fV ",myA2.read()*3.3); |
jh_ndm | 4:e3fceccc589e | 252 | uart0.printf("A3:%02fV \r\n",myA3.read()*3.3); |
jh_ndm | 4:e3fceccc589e | 253 | analogTimeout.attach(&attimeoutA,1); |
jh_ndm | 4:e3fceccc589e | 254 | } |
jh_ndm | 2:b67216fc6104 | 255 | |
jh_ndm | 4:e3fceccc589e | 256 | void ledTickfunc() |
jh_ndm | 4:e3fceccc589e | 257 | { |
jh_ndm | 4:e3fceccc589e | 258 | led1 = !led1; |
jh_ndm | 4:e3fceccc589e | 259 | } |
jh_ndm | 2:b67216fc6104 | 260 | |
jh_ndm | 4:e3fceccc589e | 261 | void getFile(char* uri) |
jh_ndm | 4:e3fceccc589e | 262 | { |
jh_ndm | 4:e3fceccc589e | 263 | //uart0.printf("getFile %s\n", uri); |
jh_ndm | 4:e3fceccc589e | 264 | char *lstchr = strrchr(uri, NULL) -1; |
jh_ndm | 4:e3fceccc589e | 265 | if ('/' == *lstchr) { |
jh_ndm | 4:e3fceccc589e | 266 | //uart0.printf("Open directory /sd%s\r\n", uri); |
jh_ndm | 4:e3fceccc589e | 267 | *lstchr = 0; |
jh_ndm | 4:e3fceccc589e | 268 | sprintf(fileName, "/sd%s", uri); |
jh_ndm | 4:e3fceccc589e | 269 | DIR *d = opendir(fileName); |
jh_ndm | 4:e3fceccc589e | 270 | if (d != NULL) { |
jh_ndm | 4:e3fceccc589e | 271 | sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); |
jh_ndm | 4:e3fceccc589e | 272 | client.send(httpHeader,strlen(httpHeader)); |
jh_ndm | 4:e3fceccc589e | 273 | sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri); |
jh_ndm | 4:e3fceccc589e | 274 | client.send(httpHeader,strlen(httpHeader)); |
jh_ndm | 4:e3fceccc589e | 275 | struct dirent *p; |
jh_ndm | 4:e3fceccc589e | 276 | while((p = readdir(d)) != NULL) { |
jh_ndm | 4:e3fceccc589e | 277 | sprintf(dirName, "%s/%s", fileName, p->d_name); |
jh_ndm | 4:e3fceccc589e | 278 | //uart0.printf("%s\n", dirName); |
jh_ndm | 4:e3fceccc589e | 279 | DIR *subDir = opendir(dirName); |
jh_ndm | 4:e3fceccc589e | 280 | if (subDir != NULL) { |
jh_ndm | 4:e3fceccc589e | 281 | sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name); |
jh_ndm | 4:e3fceccc589e | 282 | } else { |
jh_ndm | 4:e3fceccc589e | 283 | sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name); |
jh_ndm | 4:e3fceccc589e | 284 | } |
jh_ndm | 4:e3fceccc589e | 285 | client.send(httpHeader,strlen(httpHeader)); |
jh_ndm | 4:e3fceccc589e | 286 | } |
jh_ndm | 4:e3fceccc589e | 287 | } |
jh_ndm | 4:e3fceccc589e | 288 | closedir(d); |
jh_ndm | 4:e3fceccc589e | 289 | //uart0.printf("Directory closed\r\n"); |
jh_ndm | 4:e3fceccc589e | 290 | sprintf(httpHeader,"</ul></body></html>"); |
jh_ndm | 4:e3fceccc589e | 291 | client.send(httpHeader,strlen(httpHeader)); |
jh_ndm | 4:e3fceccc589e | 292 | } else { |
jh_ndm | 4:e3fceccc589e | 293 | sprintf(fileName, "/sd%s", uri); |
jh_ndm | 4:e3fceccc589e | 294 | fp = fopen(fileName, "r"); |
jh_ndm | 4:e3fceccc589e | 295 | if (fp == NULL) { |
jh_ndm | 4:e3fceccc589e | 296 | //uart0.printf("File not found\r\n"); |
jh_ndm | 4:e3fceccc589e | 297 | sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
jh_ndm | 4:e3fceccc589e | 298 | client.send(httpHeader,strlen(httpHeader)); |
jh_ndm | 4:e3fceccc589e | 299 | client.send(uri,strlen(uri)); |
jh_ndm | 4:e3fceccc589e | 300 | } else { |
jh_ndm | 4:e3fceccc589e | 301 | //uart0.printf("Sending: header"); |
jh_ndm | 4:e3fceccc589e | 302 | sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); |
jh_ndm | 4:e3fceccc589e | 303 | client.send(httpHeader,strlen(httpHeader)); |
jh_ndm | 4:e3fceccc589e | 304 | //uart0.printf(" file"); |
jh_ndm | 4:e3fceccc589e | 305 | while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) { |
jh_ndm | 4:e3fceccc589e | 306 | client.send(buffer, rdCnt); |
jh_ndm | 4:e3fceccc589e | 307 | //uart0.printf("."); |
jh_ndm | 4:e3fceccc589e | 308 | } |
jh_ndm | 4:e3fceccc589e | 309 | client.send(buffer, rdCnt); |
jh_ndm | 4:e3fceccc589e | 310 | fclose(fp); |
jh_ndm | 4:e3fceccc589e | 311 | //uart0.printf("done\r\n"); |
jh_ndm | 4:e3fceccc589e | 312 | } |
jh_ndm | 4:e3fceccc589e | 313 | } |
jh_ndm | 4:e3fceccc589e | 314 | } |
jh_ndm | 4:e3fceccc589e | 315 | |
jh_ndm | 4:e3fceccc589e | 316 | int main() { |
jh_ndm | 4:e3fceccc589e | 317 | uart0.baud(9600); |
jh_ndm | 4:e3fceccc589e | 318 | uart2.baud(9600); |
jh_ndm | 4:e3fceccc589e | 319 | PWM1.period_us(500); |
jh_ndm | 4:e3fceccc589e | 320 | PWM2.period_us(500); |
jh_ndm | 4:e3fceccc589e | 321 | PWM3.period_us(500); |
jh_ndm | 4:e3fceccc589e | 322 | PWM1 = 0.2; |
jh_ndm | 4:e3fceccc589e | 323 | PWM2 = 0.5; |
jh_ndm | 4:e3fceccc589e | 324 | PWM3 = 0.8; |
jh_ndm | 4:e3fceccc589e | 325 | uart2.printf("uart2:for Loopback test,please input something\n"); |
jh_ndm | 4:e3fceccc589e | 326 | uart0.printf("start testing uart1 digitalOut and PWM and analogIn\r\n"); |
jh_ndm | 4:e3fceccc589e | 327 | digitalTimeout.attach(&attimeoutD,1); |
jh_ndm | 4:e3fceccc589e | 328 | analogTimeout.attach(&attimeoutA,1); |
jh_ndm | 4:e3fceccc589e | 329 | /*iic*/ |
jh_ndm | 4:e3fceccc589e | 330 | char config[2]; |
jh_ndm | 4:e3fceccc589e | 331 | char data[6]; |
jh_ndm | 4:e3fceccc589e | 332 | config[0] = ADXL345_DEVID_REG; |
jh_ndm | 4:e3fceccc589e | 333 | char output; |
jh_ndm | 4:e3fceccc589e | 334 | adxl345.write( ADXL345_I2C_WRITE , config, 1); |
jh_ndm | 4:e3fceccc589e | 335 | adxl345.read( ADXL345_I2C_READ , &output, 1); |
jh_ndm | 4:e3fceccc589e | 336 | uart0.printf("ADXL345 device id is 0x%x.\r\n",output); |
jh_ndm | 4:e3fceccc589e | 337 | config[0] = ADXL345_POWER_CTL_REG; |
jh_ndm | 4:e3fceccc589e | 338 | config[1] = MeasurementMode; |
jh_ndm | 4:e3fceccc589e | 339 | adxl345.write( ADXL345_I2C_WRITE, config, 2); //set power |
jh_ndm | 4:e3fceccc589e | 340 | |
jh_ndm | 4:e3fceccc589e | 341 | config[0]=ADXL345_DATAX0_REG; |
jh_ndm | 4:e3fceccc589e | 342 | /*eth&sd*/ |
jh_ndm | 4:e3fceccc589e | 343 | ledTick.attach(&ledTickfunc,0.5); |
jh_ndm | 4:e3fceccc589e | 344 | uart0.printf("Initializing\r\n"); |
jh_ndm | 4:e3fceccc589e | 345 | uart0.printf("Checking File System\r\n"); |
jh_ndm | 4:e3fceccc589e | 346 | DIR *d = opendir("/sd/"); |
jh_ndm | 4:e3fceccc589e | 347 | if (d != NULL) { |
jh_ndm | 4:e3fceccc589e | 348 | uart0.printf("SD Card Present\r\n"); |
jh_ndm | 4:e3fceccc589e | 349 | } else { |
jh_ndm | 4:e3fceccc589e | 350 | uart0.printf("SD Card Root Directory Not Found\r\n"); |
jh_ndm | 4:e3fceccc589e | 351 | } |
jh_ndm | 4:e3fceccc589e | 352 | uart0.printf("Initializing Ethernet\r\n"); |
jh_ndm | 4:e3fceccc589e | 353 | eth.init((uint8_t*)MAC,IP,MASK,GATEWAY); |
jh_ndm | 4:e3fceccc589e | 354 | uart0.printf("Connecting\r\n"); |
jh_ndm | 4:e3fceccc589e | 355 | eth.connect(); |
jh_ndm | 4:e3fceccc589e | 356 | uart0.printf("IP Address is %s\r\n", eth.getIPAddress()); |
jh_ndm | 4:e3fceccc589e | 357 | server.bind(HTTPD_SERVER_PORT); |
jh_ndm | 4:e3fceccc589e | 358 | server.listen(); |
jh_ndm | 4:e3fceccc589e | 359 | uart0.printf("Server Listening\r\n"); |
jh_ndm | 4:e3fceccc589e | 360 | |
jh_ndm | 4:e3fceccc589e | 361 | while(1) { |
jh_ndm | 4:e3fceccc589e | 362 | //wait(0.1); |
jh_ndm | 4:e3fceccc589e | 363 | /*eth&sd*/ |
jh_ndm | 4:e3fceccc589e | 364 | //uart0.printf("\nWait for new connection...\r\n"); |
jh_ndm | 4:e3fceccc589e | 365 | server.accept(client); |
jh_ndm | 4:e3fceccc589e | 366 | |
jh_ndm | 4:e3fceccc589e | 367 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
jh_ndm | 4:e3fceccc589e | 368 | |
jh_ndm | 4:e3fceccc589e | 369 | //uart0.printf("Connection from: %s\r\n", client.get_address()); |
jh_ndm | 4:e3fceccc589e | 370 | while (1) { |
jh_ndm | 4:e3fceccc589e | 371 | /**/ |
jh_ndm | 4:e3fceccc589e | 372 | if (mybutton == 0) { |
jh_ndm | 4:e3fceccc589e | 373 | uart0.printf("USER Button is Pressed!!!\r\n"); |
jh_ndm | 4:e3fceccc589e | 374 | } |
jh_ndm | 4:e3fceccc589e | 375 | if(uart0.readable()){ |
jh_ndm | 4:e3fceccc589e | 376 | char ch = uart0.getc(); |
jh_ndm | 4:e3fceccc589e | 377 | uart0.printf("%c", ch); |
jh_ndm | 4:e3fceccc589e | 378 | } |
jh_ndm | 4:e3fceccc589e | 379 | if(uart2.readable()){ |
jh_ndm | 4:e3fceccc589e | 380 | char ch2 = uart2.getc(); |
jh_ndm | 4:e3fceccc589e | 381 | uart2.printf("%c", ch2); |
jh_ndm | 4:e3fceccc589e | 382 | } |
jh_ndm | 4:e3fceccc589e | 383 | /*iic*/ |
jh_ndm | 4:e3fceccc589e | 384 | adxl345.write( ADXL345_I2C_WRITE, config, 1); |
jh_ndm | 4:e3fceccc589e | 385 | adxl345.read( ADXL345_I2C_READ , data, 6); |
jh_ndm | 4:e3fceccc589e | 386 | xyz[0]=(int)data[1]<<8|(int)data[0]; |
jh_ndm | 4:e3fceccc589e | 387 | xyz[1]=(int)data[3]<<8|(int)data[2]; |
jh_ndm | 4:e3fceccc589e | 388 | xyz[2]=(int)data[5]<<8|(int)data[4]; |
jh_ndm | 4:e3fceccc589e | 389 | uart0.printf("%6i, %6i, %6i\r\n", xyz[0], xyz[1], xyz[2]); |
jh_ndm | 4:e3fceccc589e | 390 | /**/ |
jh_ndm | 4:e3fceccc589e | 391 | |
jh_ndm | 4:e3fceccc589e | 392 | led2 = true; |
jh_ndm | 4:e3fceccc589e | 393 | int n = client.receive(buffer, sizeof(buffer)); |
jh_ndm | 4:e3fceccc589e | 394 | if (n <= 0){ |
jh_ndm | 4:e3fceccc589e | 395 | led2 = false; |
jh_ndm | 4:e3fceccc589e | 396 | client.close(); |
jh_ndm | 4:e3fceccc589e | 397 | break; |
jh_ndm | 4:e3fceccc589e | 398 | } |
jh_ndm | 4:e3fceccc589e | 399 | //uart0.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer); |
jh_ndm | 4:e3fceccc589e | 400 | if (n >= 1024) { |
jh_ndm | 4:e3fceccc589e | 401 | sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
jh_ndm | 4:e3fceccc589e | 402 | client.send(httpHeader,strlen(httpHeader)); |
jh_ndm | 4:e3fceccc589e | 403 | client.send(buffer,n); |
jh_ndm | 4:e3fceccc589e | 404 | led2 = false; |
jh_ndm | 4:e3fceccc589e | 405 | client.close(); |
jh_ndm | 4:e3fceccc589e | 406 | break; |
jh_ndm | 4:e3fceccc589e | 407 | } else { |
jh_ndm | 4:e3fceccc589e | 408 | buffer[n]=0; |
jh_ndm | 4:e3fceccc589e | 409 | } |
jh_ndm | 4:e3fceccc589e | 410 | if (!strncmp(buffer, "GET ", 4)) { |
jh_ndm | 4:e3fceccc589e | 411 | uristr = buffer + 4; |
jh_ndm | 4:e3fceccc589e | 412 | eou = strstr(uristr, " "); |
jh_ndm | 4:e3fceccc589e | 413 | if (eou == NULL) { |
jh_ndm | 4:e3fceccc589e | 414 | sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); |
jh_ndm | 4:e3fceccc589e | 415 | client.send(httpHeader,strlen(httpHeader)); |
jh_ndm | 4:e3fceccc589e | 416 | client.send(buffer,n); |
jh_ndm | 4:e3fceccc589e | 417 | } else { |
jh_ndm | 4:e3fceccc589e | 418 | *eou = 0; |
jh_ndm | 4:e3fceccc589e | 419 | getFile(uristr); |
jh_ndm | 4:e3fceccc589e | 420 | } |
jh_ndm | 4:e3fceccc589e | 421 | } |
jh_ndm | 4:e3fceccc589e | 422 | } |
jh_ndm | 4:e3fceccc589e | 423 | |
jh_ndm | 4:e3fceccc589e | 424 | } |
jh_ndm | 4:e3fceccc589e | 425 | } |