const bool wifi(const struct ip_info *info) { uint8_t mac_address[6] ALIGNED; char *ap_password = NULL; struct ip_info ip_config; oled_info_t oled_info = { OLED_128x64_I2C_DEFAULT }; const bool oled_connected = oled_start(&oled_info); if (!wifi_detect()){ (void) console_status(CONSOLE_YELLOW, WIFI_NOT_CONNECTED); OLED_CONNECTED(oled_connected, oled_puts(&oled_info, WIFI_NOT_CONNECTED)); return false; } (void) ap_params_init(); ap_password = (char *) ap_params_get_password(); (void) console_status(CONSOLE_YELLOW, STARTING_WIFI); OLED_CONNECTED(oled_connected, oled_status(&oled_info, STARTING_WIFI)); wifi_ap_init(ap_password); printf("ESP8266 information\n"); printf(" SDK : %s\n", system_get_sdk_version()); printf(" Firmware : %s\n\n", wifi_get_firmware_version()); if (network_params_init()) { (void) console_status(CONSOLE_YELLOW, CHANGING_TO_STATION_MODE); OLED_CONNECTED(oled_connected, oled_status(&oled_info, CHANGING_TO_STATION_MODE)); ssid = network_params_get_ssid(); if (network_params_is_use_dhcp()) { wifi_station(ssid, network_params_get_password()); } else { ip_config.ip.addr = network_params_get_ip_address(); ip_config.netmask.addr = network_params_get_net_mask(); ip_config.gw.addr = network_params_get_default_gateway(); wifi_station_ip(ssid, network_params_get_password(), &ip_config); } } opmode = wifi_get_opmode(); if (opmode == WIFI_STA) { printf("WiFi mode : Station (AP: %s)\n", network_params_get_ssid()); } else { printf("WiFi mode : Access Point (authenticate mode: %s)\n", *ap_password == '\0' ? "Open" : "WPA_WPA2_PSK"); } if (wifi_get_macaddr(mac_address)) { printf(" MAC address : "MACSTR "\n", MAC2STR(mac_address)); } else { (void) console_error("wifi_get_macaddr"); OLED_CONNECTED(oled_connected, oled_status(&oled_info, "E: wifi_get_macaddr")); } printf(" Hostname : %s\n", wifi_get_hostname()); if (wifi_get_ip_info(&ip_config)) { printf(" IP-address : " IPSTR "\n", IP2STR(ip_config.ip.addr)); printf(" Netmask : " IPSTR "\n", IP2STR(ip_config.netmask.addr)); printf(" Gateway : " IPSTR "\n", IP2STR(ip_config.gw.addr)); if (opmode == WIFI_STA) { const _wifi_station_status status = wifi_station_get_connect_status(); printf(" Status : %s\n", wifi_station_status(status)); if (status != WIFI_STATION_GOT_IP) { (void) console_error("Not connected!"); if (oled_connected) { oled_set_cursor(&oled_info, 2, 0); (void) oled_puts(&oled_info, wifi_station_status(status)); oled_set_cursor(&oled_info, 5, 0); (void) oled_printf(&oled_info, "SSID : %s\n", network_params_get_ssid()); oled_status(&oled_info, "E: Not connected!"); } for (;;) ; } } } else { (void) console_error("wifi_get_ip_info"); OLED_CONNECTED(oled_connected, oled_status(&oled_info, "E: wifi_get_ip_info")); } if (fota_params_init()) { OLED_CONNECTED(oled_connected, oled_status(&oled_info, "FOTA mode")); console_newline(); fota(fota_params_get_server()); for (;;) ; } (void) console_status(CONSOLE_GREEN, WIFI_STARTED); OLED_CONNECTED(oled_connected, oled_status(&oled_info, WIFI_STARTED)); memcpy((void *)info, (const void *)&ip_config, sizeof(struct ip_info)); return true; }
/** * @fn sensorTag_HandleDisp * * @brief * * @param * * @return */ static void sensorTag_HandleDisp(unsigned char mode, void *p) { VOID *p; UTCTimeStruct time; if ((mode & 0xF0) == MODE_SLEEP) { // display time sensorTag_ClockGet(&time, 0); oled_set_font(&num8x16); oled_draw_icon(1, 0, time.hour/10); oled_draw_icon(1, 8, time.hour%10); oled_draw_icon(1, 16, 10); oled_draw_icon(1, 24, time.minutes/10); oled_draw_icon(1, 32, time.minutes%10); // display sleep icon oled_set_font(&icon16x16); oled_draw_icon(0, 44, 6); // display battery sensorTag_BattDisp(batt_get_level()); return; } switch (mode & 0x0F) { case MODE_TIME: if ((mode & 0xF0) == MODE_NORMAL) { // display time sensorTag_ClockGet(&time, 0); oled_set_font(&num8x16); oled_draw_icon(1, 0, time.hour/10); oled_draw_icon(1, 8, time.hour%10); oled_draw_icon(1, 16, 10); oled_draw_icon(1, 24, time.minutes/10); oled_draw_icon(1, 32, time.minutes%10); // display clock icon oled_set_font(&icon16x16); oled_draw_icon(0, 44, 13); } else { // display chronograph sensorTag_ClockGet(&time, workout.time); oled_set_font(&num8x16); oled_draw_icon(0, 0, time.hour/10); oled_draw_icon(0, 8, time.hour%10); oled_draw_icon(0, 16, 10); oled_draw_icon(0, 24, time.minutes/10); oled_draw_icon(0, 32, time.minutes%10); oled_draw_icon(0, 40, 10); oled_draw_icon(0, 48, time.seconds/10); oled_draw_icon(0, 56, time.seconds%10); } // display battery sensorTag_BattDisp(batt_get_level()); break; case MODE_STEP: // display step icon oled_set_font(&icon16x16); if ((mode & 0xF0) == MODE_NORMAL) { oled_draw_icon(0, 44, 0); } else { oled_draw_icon(0, 44, 1); } // display step oled_set_font(&num8x16); if ((mode & 0xF0) == MODE_NORMAL) { oled_draw_num(1, 0, normal.steps); } else { oled_draw_num(1, 0, normal.steps - workout.steps); } break; case MODE_CALORIE: // display calorie icon oled_set_font(&icon16x16); if ((mode & 0xF0) == MODE_NORMAL) { oled_draw_icon(0, 44, 2); } else { oled_draw_icon(0, 44, 3); } // display calorie oled_set_font(&num8x16); if ((mode & 0xF0) == MODE_NORMAL) { oled_draw_num(1, 0, normal.calorie); } else { oled_draw_num(1, 0, workout.calorie); } break; case MODE_DISTANCE: // display distance icon oled_set_font(&icon16x16); if ((mode & 0xF0) == MODE_NORMAL) { oled_draw_icon(0, 44, 4); } else { oled_draw_icon(0, 44, 5); } // display distance oled_set_font(&num8x16); if ((mode & 0xF0) == MODE_NORMAL) { oled_draw_num(1, 0, normal.distance); } else { oled_draw_num(1, 0, workout.distance); } break; case MODE_DBG: oled_set_font(&fonts7x8); oled_gotoxy(0, 0); oled_puts("x:"); oled_puts_04x(acc[0]); oled_gotoxy(1, 0); oled_puts("y:"); oled_puts_04x(acc[1]); oled_gotoxy(2, 0); oled_puts("z:"); oled_puts_04x(acc[2]); oled_gotoxy(3, 0); oled_puts("s:"); oled_puts_05u(normal.steps); break; } }
int main(void) { dhcp dhcp_session; int dhcp_state; http_server http; http_socket httpsock[4]; http_content httpcontent[4]; char oled_msg[17]; char *pnt; int step; api_init(); uart_puts(" * eCowLogic firmware \r\n"); hw_systick( hw_getfreq() / 1000 ); dhcp_session.socket = 2; dhcp_session.buffer = (u8 *)buffer_dhcp; dhcp_init(&dhcp_session); oled_pos(1, 0); oled_puts("Reseau (DHCP) "); step = 0; while(1) { dhcp_state = dhcp_run(&dhcp_session); if (dhcp_state == DHCP_IP_LEASED) break; step++; oled_pos(1, 13); if (step == 1) oled_puts(". "); if (step == 2) oled_puts(".. "); if (step == 3) oled_puts("..."); if (step == 4) { step = 0; dhcp_session.tick_1s++; uart_putc('.'); oled_puts(" "); } delay(250); } pnt = oled_msg; pnt += b2ds(pnt, dhcp_session.dhcp_my_ip[0]); *pnt++ = '.'; pnt += b2ds(pnt, dhcp_session.dhcp_my_ip[1]); *pnt++ = '.'; pnt += b2ds(pnt, dhcp_session.dhcp_my_ip[2]); *pnt++ = '.'; pnt += b2ds(pnt, dhcp_session.dhcp_my_ip[3]); for ( ; pnt < (oled_msg + 16); pnt++) *pnt = ' '; oled_msg[16] = 0; uart_puts("DHCP: LEASED ! "); uart_puts(oled_msg); uart_puts("\r\n"); oled_pos(1, 0); oled_puts(oled_msg); spi_init(); pld_init(); //net_init(); /* Init HTTP content */ strcpy(httpcontent[0].name, "/pld"); httpcontent[0].wildcard = 0; httpcontent[0].cgi = cgi_ng_pld; httpcontent[0].next = &httpcontent[1]; /* Init HTTP content */ strcpy(httpcontent[1].name, "/spi"); httpcontent[1].wildcard = 0; httpcontent[1].cgi = cgi_spi; httpcontent[1].next = &httpcontent[2]; /* Init HTTP content */ strcpy(httpcontent[2].name, "/infos"); httpcontent[2].wildcard = 0; httpcontent[2].cgi = cgi_info; httpcontent[2].next = &httpcontent[3]; /* Init HTTP content */ strcpy(httpcontent[3].name, "/"); httpcontent[3].wildcard = 1; httpcontent[3].cgi = cgi_ng_page; httpcontent[3].next = 0; /* Init HTTP socket */ httpsock[0].id = 4; httpsock[0].state = 0; httpsock[0].server = &http; httpsock[0].next = &httpsock[1]; /* Init HTTP socket */ httpsock[1].id = 5; httpsock[1].state = 0; httpsock[1].server = &http; httpsock[1].next = &httpsock[2]; /* Init HTTP socket */ httpsock[2].id = 6; httpsock[2].state = 0; httpsock[2].server = &http; httpsock[2].next = &httpsock[3]; /* Init HTTP socket */ httpsock[3].id = 7; httpsock[3].state = 0; httpsock[3].server = &http; httpsock[3].next = 0; /* Init the new HTTP layer */ http.port = 80; http.err404 = 0; http.err403 = 0; http.keepalive = 10; http.socks = &httpsock[0]; http.contents = &httpcontent[0]; http_init(&http); while(1) { http_run(&http); } }
static void upd_flash(tftp *tftp) { char str[17]; int tftp_block; u32 waddr; u32 sector, last_sector; int len; tftp_init(tftp); tftp->filename = upd_mem; waddr = 0; last_sector= 0xFFFFFFFF; while(1) { tftp_run(tftp); if ( (tftp->state == 2) || (tftp->state == 3)) { if (tftp->lastblock != tftp_block) { tftp_block = tftp->lastblock; strcpy(str, "Load flash "); b2ds(&str[11], tftp_block); oled_pos(0, 0); oled_puts(str); sector = (tftp_block << 9) & 0xFFFF0000; if (sector != last_sector) { flash_erase(sector); last_sector = sector; } uart_puts(str); uart_putc('\r'); len = 0; if (tftp->length > 4) { u8 *dat; dat = (u8 *)&tftp->data[4]; len = tftp->length - 4; if (len > 256) { flash_write(waddr, dat, 256); len -= 256; /* Update remaining data length */ dat += 256; /* Move the source pointer */ waddr += 256; /* Update the dest address */ /* Wait for the write transfer ends */ while(flash_status() & 1) ; } flash_write(waddr, dat, len); /* Wait for the write transfer ends */ while(flash_status() & 1) ; /* Update write address for next packet */ waddr += len; } } tftp_ack(tftp); } if (tftp->state == 3) { uart_crlf(); break; } if (tftp->state == 98) { uart_puts("TFTP timeout, restart\r\n"); oled_pos(0, 0); oled_puts("TFTP: timeout!"); tftp->timestamp = 0x3000; tftp->state = 0; } if (tftp->state == 99) { uart_puts("upd_firmware() TFTP error 99\r\n"); break; } } }