This is a working HTTP server with I2C, GPIO and PWM commands accessible through URLs. It also includes the ability to serve files from an SD card.
Dependencies: EthernetInterfaceMuri SDFileSystem mbed-rtos mbed
muri.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 #include "muri.h" 00004 00005 I2C i2c(p28, p27); 00006 //PwmOut pwm1(p26); 00007 PwmOut pwm(p25); 00008 DigitalInOut pmd1(p14); 00009 DigitalInOut pmd2(p11); 00010 DigitalInOut pmd3(p12); 00011 DigitalInOut pmd4(p13); 00012 DigitalInOut pmd7(p24); 00013 DigitalInOut pmd8(p23); 00014 DigitalInOut pmd9(p22); 00015 DigitalInOut pmd10(p21); 00016 00017 osTimerDef(poff, pwm_off); 00018 osTimerId off_timer; 00019 00020 void init_dio() 00021 { 00022 pmd1.mode(PullNone); 00023 pmd1.mode(OpenDrain); 00024 pmd1.write(1); 00025 pmd1.output(); 00026 pmd2.mode(PullNone); 00027 pmd2.mode(OpenDrain); 00028 pmd2.write(1); 00029 pmd2.output(); 00030 pmd3.mode(PullNone); 00031 pmd3.mode(OpenDrain); 00032 pmd3.write(1); 00033 pmd3.output(); 00034 pmd4.mode(PullNone); 00035 pmd4.mode(OpenDrain); 00036 pmd4.write(1); 00037 pmd4.output(); 00038 pmd7.mode(PullNone); 00039 pmd7.mode(OpenDrain); 00040 pmd7.write(1); 00041 pmd7.output(); 00042 pmd8.mode(PullNone); 00043 pmd8.mode(OpenDrain); 00044 pmd8.write(1); 00045 pmd8.output(); 00046 pmd9.mode(PullNone); 00047 pmd9.mode(OpenDrain); 00048 pmd9.write(1); 00049 pmd9.output(); 00050 pmd10.mode(PullNone); 00051 pmd10.mode(OpenDrain); 00052 pmd10.write(1); 00053 pmd10.output(); 00054 off_timer = osTimerCreate(osTimer(poff), osTimerOnce, NULL); 00055 } 00056 00057 void cmd_i2c_write(char* qry, char* data, char* resp) 00058 { 00059 int addr = 0; 00060 int dlen = 0; 00061 int dtmp = 0; 00062 if (sscanf(qry, "?%2x", &addr) == 1) { 00063 qry += 3; 00064 while (sscanf(qry, "&%2x", &dtmp) == 1) { 00065 data[dlen] = dtmp; 00066 qry +=3; 00067 dlen +=1; 00068 } 00069 } else { 00070 dlen = -1; 00071 } 00072 if (dlen > 0 ) { 00073 i2c.write(addr, data, dlen); 00074 sprintf(resp,"200 %d\r\n", dlen); 00075 } else { 00076 if (dlen == 0) { 00077 sprintf(resp,"204 No data to write: %s\r\n", qry); 00078 } else { 00079 sprintf(resp,"400 Invalid parameters: %s\r\n", qry); 00080 00081 } 00082 } 00083 } 00084 00085 void cmd_i2c_read(char* qry, char* data, char* resp) 00086 { 00087 int addr = 0; 00088 int dlen = 0; 00089 int dcnt = 0; 00090 if (sscanf(qry, "?%2x", &addr) == 1) { 00091 qry += 3; 00092 if (sscanf(qry, "&%2x", &dlen) == 1) { 00093 if (i2c.read(addr, data, dlen)!=0) { 00094 dlen = -1; 00095 sprintf(resp, "500 I2C read failed\r\n"); 00096 } 00097 } else { 00098 sprintf(resp, "400 Invalid data length: %s\r\n", qry); 00099 } 00100 } else { 00101 dlen = -1; 00102 sprintf(resp, "400 Invalid address: %s\r\n", qry); 00103 } 00104 if (dlen > 0) { 00105 sprintf(resp,"200"); 00106 resp += 3; 00107 for (dcnt = 0; dcnt < dlen; dcnt++) { 00108 sprintf(resp," %02x", data[dcnt]); 00109 resp +=3; 00110 } 00111 } 00112 } 00113 00114 void cmd_i2c_addr_read(char* qry, char* data, char* resp) 00115 { 00116 int addr = 0; 00117 int radd = 0; 00118 int dlen = 0; 00119 int dcnt = 0; 00120 if (sscanf(qry, "?%2x", &addr) == 1) { 00121 qry += 3; 00122 if (sscanf(qry, "&%2x", &radd) == 1) { 00123 qry += 3; 00124 if (sscanf(qry, "&%2x", &dlen) == 1) { 00125 data[0] = radd; 00126 i2c.write(addr, data, 1, false); 00127 if (i2c.read(addr, data, dlen)!=0) { 00128 dlen = -1; 00129 sprintf(resp, "500 I2C read failed\r\n"); 00130 } 00131 } else { 00132 sprintf(resp, "400 Invalid data length: %s\r\n", qry); 00133 } 00134 } else { 00135 dlen = -1; 00136 sprintf(resp, "400 Invalid register address: %s\r\n", qry); 00137 } 00138 } else { 00139 dlen = -1; 00140 sprintf(resp, "400 Invalid address: %s\r\n", qry); 00141 } 00142 if (dlen > 0) { 00143 sprintf(resp,"200"); 00144 resp += 3; 00145 for (dcnt = 0; dcnt < dlen; dcnt++) { 00146 sprintf(resp," %02x", data[dcnt]); 00147 resp +=3; 00148 } 00149 } 00150 } 00151 00152 void cmd_pmd_setio(char* qry, char* data, char* resp) 00153 { 00154 int pdir = -1; 00155 int pdata = -1; 00156 if (sscanf(qry, "?%2x", &pdir) == 1) { 00157 if (pdir == 0) { 00158 pmd1.mode(PullNone); 00159 pmd2.mode(PullNone); 00160 pmd3.mode(PullNone); 00161 pmd4.mode(PullNone); 00162 pmd7.mode(PullNone); 00163 pmd8.mode(PullNone); 00164 pmd9.mode(PullNone); 00165 pmd10.mode(PullNone); 00166 } else { 00167 if (sscanf(qry+3, "&%2x", &pdata) == 1) { 00168 if (pdir & (1 << 0)) { 00169 if (pdata & (1 << 0)) pmd1.mode(PullUp); 00170 else pmd1.mode(PullDown); 00171 } else { 00172 pmd1.mode(PullNone); 00173 } 00174 if (pdir & (1 << 1)) { 00175 if (pdata & (1 << 1)) pmd2.mode(PullUp); 00176 else pmd2.mode(PullDown); 00177 } else { 00178 pmd2.mode(PullNone); 00179 } 00180 if (pdir & (1 << 2)) { 00181 if (pdata & (1 << 2)) pmd3.mode(PullUp); 00182 else pmd3.mode(PullDown); 00183 } else { 00184 pmd3.mode(PullNone); 00185 } 00186 if (pdir & (1 << 3)) { 00187 if (pdata & (1 << 3)) pmd4.mode(PullUp); 00188 else pmd4.mode(PullDown); 00189 } else { 00190 pmd4.mode(PullNone); 00191 } 00192 if (pdir & (1 << 4)) { 00193 if (pdata & (1 << 4)) pmd7.mode(PullUp); 00194 else pmd7.mode(PullDown); 00195 } else { 00196 pmd7.mode(PullNone); 00197 } 00198 if (pdir & (1 << 5)) { 00199 if (pdata & (1 << 5)) pmd8.mode(PullUp); 00200 else pmd8.mode(PullDown); 00201 } else { 00202 pmd8.mode(PullNone); 00203 } 00204 if (pdir & (1 << 6)) { 00205 if (pdata & (1 << 6)) pmd9.mode(PullUp); 00206 else pmd9.mode(PullDown); 00207 } else { 00208 pmd9.mode(PullNone); 00209 } 00210 if (pdir & (1 << 7)) { 00211 if (pdata & (1 << 7)) pmd10.mode(PullUp); 00212 else pmd10.mode(PullDown); 00213 } else { 00214 pmd10.mode(PullNone); 00215 } 00216 } 00217 } 00218 00219 pdata = 0; 00220 pdata += (pmd1 << 0); 00221 pdata += (pmd2 << 1); 00222 pdata += (pmd3 << 2); 00223 pdata += (pmd4 << 3); 00224 pdata += (pmd7 << 4); 00225 pdata += (pmd8 << 5); 00226 pdata += (pmd9 << 6); 00227 pdata += (pmd10 << 7); 00228 sprintf(resp,"200 %02x", pdata); 00229 } else { 00230 sprintf(resp,"400 Invalid parameters: %s\r\n", qry); 00231 } 00232 } 00233 00234 void cmd_pmd_write(char* qry, char* data, char* resp) 00235 { 00236 int pdata = -1; 00237 if (sscanf(qry, "?%2x", &pdata) == 1) { 00238 pmd1 = pdata & (1 << 0); 00239 pmd2 = pdata & (1 << 1); 00240 pmd3 = pdata & (1 << 2); 00241 pmd4 = pdata & (1 << 3); 00242 pmd7 = pdata & (1 << 4); 00243 pmd8 = pdata & (1 << 5); 00244 pmd9 = pdata & (1 << 6); 00245 pmd10 = pdata & (1 << 7); 00246 pdata = 0; 00247 pdata += (pmd1 << 0); 00248 pdata += (pmd2 << 1); 00249 pdata += (pmd3 << 2); 00250 pdata += (pmd4 << 3); 00251 pdata += (pmd7 << 4); 00252 pdata += (pmd8 << 5); 00253 pdata += (pmd9 << 6); 00254 pdata += (pmd10 << 7); 00255 sprintf(resp,"200 %02x", pdata); 00256 } else { 00257 sprintf(resp,"400 Invalid parameters: %s\r\n", qry); 00258 00259 } 00260 } 00261 00262 void cmd_pmd_read(char* qry, char* data, char* resp) 00263 { 00264 int pdata = 0; 00265 pdata += (pmd1 << 0); 00266 pdata += (pmd2 << 1); 00267 pdata += (pmd3 << 2); 00268 pdata += (pmd4 << 3); 00269 pdata += (pmd7 << 4); 00270 pdata += (pmd8 << 5); 00271 pdata += (pmd9 << 6); 00272 pdata += (pmd10 << 7); 00273 sprintf(resp,"200 %02x", pdata); 00274 } 00275 00276 void pwm_off(void const *arg) 00277 { 00278 // pwm.period_us(0); 00279 pwm.pulsewidth_us(0); 00280 } 00281 00282 void cmd_pwm_set(char* qry, char* data, char* resp) 00283 { 00284 int pwmP = 0; 00285 int pwmW = 0; 00286 int pwmD = 0; 00287 if (sscanf(qry, "?%4x&%4x&%4x", &pwmP, &pwmW, &pwmD) == 3) { 00288 pwm.period_us(pwmP); 00289 pwm.pulsewidth_us(pwmW); 00290 osTimerStart(off_timer, pwmD); 00291 sprintf(resp,"200 %04x %04x %04x", pwmP, pwmW, pwmD); 00292 } else { 00293 sprintf(resp,"400 Invalid parameters: %s\r\n", qry); 00294 00295 } 00296 } 00297 00298 void muri(char* uri, char* data, char* resp) 00299 { 00300 // sprintf(resp, "501 %s", uri); 00301 //} 00302 //void get_cgi(char* uri, char* qry) 00303 //{ 00304 char *qry; 00305 printf("muri uri: %s\n", uri); 00306 qry = strstr(uri, "?"); 00307 if (!strncmp(uri, "i2cw?", 5)) { // i2cw [addr] [data] [data] [data] ... 00308 cmd_i2c_write(qry, data, resp); 00309 } else if (!strncmp(uri, "i2cr?", 5)) { // i2cr [addr] [count] 00310 cmd_i2c_read(qry, data, resp); 00311 } else if (!strncmp(uri, "i2car?", 6)) { // i2car [addr] [radd] [count] 00312 cmd_i2c_addr_read(qry, data, resp); 00313 } else if (!strncmp(uri, "pmdio?", 6)) { // pmdio [dir] [data] 00314 cmd_pmd_setio(qry, data, resp); 00315 } else if (!strncmp(uri, "pmdw?", 5)) { // pmdw [data] 00316 cmd_pmd_write(qry, data, resp); 00317 } else if (!strncmp(uri, "pmdr", 4)) { // pmdr 00318 cmd_pmd_read(qry, data, resp); 00319 } else if (!strncmp(uri, "pwmd?", 5)) { // pwmd [period] [width] [duration] 00320 cmd_pwm_set(qry, data, resp); 00321 } else { 00322 sprintf(resp, "501 %s", uri); 00323 } 00324 printf("muri resp: %s\n", resp); 00325 } 00326 00327
Generated on Wed Jul 13 2022 16:34:14 by 1.7.2