pico-stuff/apps/tcp_server/main.c

99 wiersze
2.2 KiB
C
Czysty Zwykły widok Historia

2021-03-10 02:30:57 +00:00
#include <stdio.h>
2021-03-11 22:50:48 +00:00
#include "bsp/board.h"
2021-03-10 02:30:57 +00:00
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include "usb_network.h"
2021-03-10 02:30:57 +00:00
#include "lwip/tcp.h"
2021-03-04 02:48:48 +00:00
2021-03-10 02:30:57 +00:00
struct tcp_pcb* client;
2021-03-11 22:44:49 +00:00
struct repeating_timer timer;
2021-03-04 02:48:48 +00:00
2021-03-12 01:50:45 +00:00
bool send_timer(struct repeating_timer *t) {
const float conversion_factor = 3.3f / (1 << 12);
float ADC_voltage = adc_read() * conversion_factor;
2021-03-10 02:30:57 +00:00
char str[64];
2021-03-12 01:50:45 +00:00
int len = sprintf(str, "TEMP: %f °C\n", 27 - (ADC_voltage - 0.706) / 0.001721);
2021-03-11 22:44:49 +00:00
tcp_write(client, str, len, 0);
tcp_output(client);
2021-03-10 02:30:57 +00:00
2021-03-12 01:50:45 +00:00
return true;
2021-03-10 02:30:57 +00:00
}
2021-03-11 22:44:49 +00:00
static void srv_close(struct tcp_pcb *pcb){
2021-03-12 01:50:45 +00:00
// Cancel send timer.
cancel_repeating_timer(&timer);
2021-03-11 22:44:49 +00:00
2021-03-10 02:30:57 +00:00
tcp_arg(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_recv(pcb, NULL);
tcp_close(pcb);
2021-03-11 22:44:49 +00:00
}
2021-03-10 02:30:57 +00:00
2021-03-11 22:44:49 +00:00
static void srv_err(void *arg, err_t err) {
// Probably an indication that the client connection went kaput! Stopping stream...
srv_close(client);
2021-03-10 02:30:57 +00:00
}
static err_t srv_receive(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
2021-03-11 23:05:30 +00:00
if (err != ERR_OK && p != NULL) {
2021-03-10 02:30:57 +00:00
goto exception;
2021-03-11 23:05:30 +00:00
}
2021-03-10 02:30:57 +00:00
tcp_recved(pcb, p->tot_len);
tcp_sent(pcb, NULL);
2021-03-11 22:44:49 +00:00
// The connection is closed if the client sends "X".
2021-03-11 23:05:30 +00:00
if (((char*)p->payload)[0] == 'X') {
2021-03-11 22:44:49 +00:00
srv_close(pcb);
2021-03-11 23:05:30 +00:00
}
2021-03-10 02:30:57 +00:00
exception:
pbuf_free(p);
return err;
}
static err_t srv_accept(void * arg, struct tcp_pcb * pcb, err_t err) {
2021-03-11 23:05:30 +00:00
if (err != ERR_OK) {
2021-03-11 22:44:49 +00:00
return err;
2021-03-11 23:05:30 +00:00
}
2021-03-11 22:44:49 +00:00
tcp_setprio(pcb, TCP_PRIO_MAX);
2021-03-10 02:30:57 +00:00
tcp_recv(pcb, srv_receive);
2021-03-11 22:44:49 +00:00
tcp_err(pcb, srv_err);
2021-03-10 02:30:57 +00:00
tcp_poll(pcb, NULL, 4);
2021-03-11 22:44:49 +00:00
client = pcb;
2021-03-12 01:50:45 +00:00
// Start send timer.
add_repeating_timer_ms(50, send_timer, NULL, &timer);
2021-03-11 22:44:49 +00:00
2021-03-12 01:50:45 +00:00
return err;
2021-03-10 02:30:57 +00:00
}
int main(void) {
// Init network RNDIS stack.
network_init();
2021-03-10 02:30:57 +00:00
// Start TCP server.
struct tcp_pcb* pcb = tcp_new();
pcb->so_options |= SOF_KEEPALIVE;
pcb->keep_intvl = 75000000;
tcp_bind(pcb, IP_ADDR_ANY, 7777);
2021-03-11 22:44:49 +00:00
// Start listening for connections.
2021-03-10 02:30:57 +00:00
struct tcp_pcb* listen = tcp_listen(pcb);
tcp_accept(listen, srv_accept);
2021-03-12 01:50:45 +00:00
// Start ADC.
adc_init();
2021-03-12 01:50:45 +00:00
adc_set_temp_sensor_enabled(true);
adc_select_input(4);
2021-03-10 02:30:57 +00:00
// Listen to events.
while (1) {
network_step();
2021-03-10 02:30:57 +00:00
}
2021-03-03 20:10:07 +00:00
2021-03-10 02:30:57 +00:00
return 0;
2021-03-11 22:44:49 +00:00
}