zig-riscv-embedded

Experimental Zig-based CoAP node for the HiFive1 RISC-V board

git clone https://git.8pit.net/zig-riscv-embedded.git

 1package main
 2
 3import (
 4	"os"
 5
 6	"github.com/Lobaro/slip"
 7)
 8
 9type Dispatcher struct {
10	coap   *CoapEndpoint
11	serial *SerialEndpoint
12}
13
14func (d *Dispatcher) handleCoap(data []byte) {
15	d.serial.TX <- data
16}
17
18func (d *Dispatcher) handleSerial(pkt Packet) {
19	switch pkt.FrameType {
20	case slip.FRAME_DIAGNOSTIC:
21		_, err := os.Stdout.WriteString(string(pkt.Data))
22		if err != nil {
23			logger.Println("handleSerial:", err)
24		}
25	case slip.FRAME_COAP:
26		d.coap.TX <- pkt.Data
27	default:
28		logger.Printf("handleSerial: Unsupported frame type: 0x%x\n", pkt.FrameType)
29	}
30}
31
32func (d *Dispatcher) Run() {
33	for {
34		select {
35		case data := <-d.coap.RX:
36			d.handleCoap(data)
37		case pkt := <-d.serial.RX:
38			d.handleSerial(pkt)
39		}
40	}
41}