1
0
Fork 0
mirror of https://github.com/kou029w/k2ping.git synced 2025-01-18 16:07:59 +00:00

LCDにステータスを表示できるようにする

This commit is contained in:
Nebel 2017-04-15 23:30:50 +09:00
parent 584cd55782
commit f61d57a603

View file

@ -2,11 +2,30 @@
#include <Wire.h> #include <Wire.h>
#include <LiquidCrystal.h> #include <LiquidCrystal.h>
extern "C" {
#include <ping.h>
}
// Digital pins for WeMos D1 // Digital pins for WeMos D1
static const uint8_t D[] = {3, 1, 16, 5, 4, 14, 12, 13, 0, 2, 15}; static const uint8_t D[] = {3, 1, 16, 5, 4, 14, 12, 13, 0, 2, 15};
// LCD Keypad Shield for Arduino // LCD Keypad Shield for Arduino
LiquidCrystal lcd(D[8], D[9], D[4], D[5], D[6], D[7]); LiquidCrystal lcd(D[8], D[9], D[4], D[5], D[6], D[7]);
void p(char *fmt, ...){
char buf[17];
va_list args;
va_start (args, fmt);
vsnprintf(buf, 17, fmt, args);
va_end (args);
lcd.print(buf);
}
uint8_t errors = 0;
uint8_t success = 0;
int min_time = 0;
int avg_time = 0;
int max_time = 0;
int total_bytes = 0;
void setup() { void setup() {
lcd.begin(16, 2); lcd.begin(16, 2);
@ -33,8 +52,68 @@ void setup() {
lcd.print("Connected"); lcd.print("Connected");
lcd.setCursor(0, 1); lcd.setCursor(0, 1);
lcd.print(WiFi.localIP()); lcd.print(WiFi.localIP());
delay(1000);
IPAddress dest_ip;
WiFi.hostByName("www.google.com", dest_ip);
lcd.clear();
lcd.print(dest_ip);
lcd.setCursor(15, 0);
lcd.blink();
while (analogRead(0) >= 128) yield();
lcd.noBlink();
// Let's enjoy ping!
ping(dest_ip);
} }
void loop() { void loop() {
; ;
} }
void ping(IPAddress dest_ip) {
ping_option opt;
memset(&opt, 0, sizeof(struct ping_option));
opt.count = 4;
opt.coarse_time = 1;
opt.ip = dest_ip;
opt.recv_function = reinterpret_cast<ping_recv_function>(&ping_recv_cb);
opt.sent_function = NULL;
lcd.clear();
lcd.print(IPAddress(opt.ip));
lcd.setCursor(15, 0);
lcd.print("*");
ping_start(&opt);
}
void ping_recv_cb(void *_option, void *_resp) {
ping_resp* resp = reinterpret_cast<struct ping_resp*>(_resp);
ping_option* opt = reinterpret_cast<struct ping_option*>(_option);
if (resp->ping_err == -1) {
errors++;
lcd.setCursor(0, 1);
lcd.print("Transmit failed.");
} else {
success++;
avg_time += resp->resp_time;
if (min_time == 0 || resp->resp_time < min_time) min_time = resp->resp_time;
if (resp->resp_time > max_time) max_time = resp->resp_time;
total_bytes += resp->bytes;
lcd.setCursor(0, 1);
p((char*)"#%-2d%3dbyte%4dms", resp->seqno, resp->bytes, resp->resp_time);
}
if (errors + success >= opt->count) {
avg_time /= opt->count;
lcd.clear();
p((char*)"%4dbyte%3d%%loss", total_bytes, 100 * errors / opt->count);
lcd.setCursor(0, 2);
p((char*)"%4d/%4d/%4dms", min_time, avg_time, max_time);
}
}