forked from maxnet/pico-webserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.c
75 lines (63 loc) · 1.73 KB
/
webserver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "pico/stdlib.h"
#include "pico/bootrom.h"
#include "hardware/watchdog.h"
#include "hardware/structs/watchdog.h"
#include "tusb_lwip_glue.h"
#define LED_PIN 25
// let our webserver do some dynamic handling
static const char *cgi_toggle_led(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
{
gpio_put(LED_PIN, !gpio_get(LED_PIN));
return "/index.shtml";
}
static const char *cgi_reset_usb_boot(int iIndex, int iNumParams, char *pcParam[], char *pcValue[])
{
reset_usb_boot(0, 0);
return "/index.shtml";
}
static const tCGI cgi_handlers[] = {
{
"/toggle_led",
cgi_toggle_led
},
{
"/reset_usb_boot",
cgi_reset_usb_boot
}
};
u16_t adc_ssi_handler(int iIndex, char *buf, int buflen, u16_t current_tag_number, u16_t *next_tag_number)
{
static char counter=0;
// ...put up to buflen bytes in buf and return number of bytes put there
for (int i=0; i<buflen; i++) {
buf[i] = counter++; // perhaps i + iIndex ?
}
// if ( current_tag_number < 100 ) {
// should go forever, but it stops at 12'582'721 bytes
// which are transferred at 136 kB / s (a bit slower than needed for full ADC speed)
*next_tag_number = current_tag_number + 1;
// }
return buflen;
}
const char * ssi_tags[] = {
"adc"
};
int main()
{
// Initialize tinyusb, lwip, dhcpd and httpd
init_lwip();
wait_for_netif_is_up();
dhcpd_init();
httpd_init();
http_set_cgi_handlers(cgi_handlers, LWIP_ARRAYSIZE(cgi_handlers));
http_set_ssi_handler(adc_ssi_handler, ssi_tags, LWIP_ARRAYSIZE(ssi_tags));
// For toggle_led
gpio_init(LED_PIN);
gpio_set_dir(LED_PIN, GPIO_OUT);
while (true)
{
tud_task();
service_traffic();
}
return 0;
}