|
| 1 | +//go:build cyw43439 |
| 2 | + |
| 3 | +package bluetooth |
| 4 | + |
| 5 | +import ( |
| 6 | + "machine" |
| 7 | + |
| 8 | + "log/slog" |
| 9 | + |
| 10 | + "github.com/soypat/cyw43439" |
| 11 | +) |
| 12 | + |
| 13 | +const maxConnections = 1 |
| 14 | + |
| 15 | +// Adapter represents a SPI connection to the HCI controller on an attached CYW4349 module. |
| 16 | +type Adapter struct { |
| 17 | + hciAdapter |
| 18 | +} |
| 19 | + |
| 20 | +// DefaultAdapter is the default adapter on the current system. |
| 21 | +// |
| 22 | +// Make sure to call Enable() before using it to initialize the adapter. |
| 23 | +var DefaultAdapter = &Adapter{ |
| 24 | + hciAdapter: hciAdapter{ |
| 25 | + isDefault: true, |
| 26 | + connectHandler: func(device Device, connected bool) { |
| 27 | + return |
| 28 | + }, |
| 29 | + connectedDevices: make([]Device, 0, maxConnections), |
| 30 | + }, |
| 31 | +} |
| 32 | + |
| 33 | +// Enable configures the BLE stack. It must be called before any |
| 34 | +// Bluetooth-related calls (unless otherwise indicated). |
| 35 | +func (a *Adapter) Enable() error { |
| 36 | + if debug { |
| 37 | + println("Initializing CYW43439 device") |
| 38 | + } |
| 39 | + |
| 40 | + dev := cyw43439.NewPicoWDevice() |
| 41 | + cfg := cyw43439.DefaultBluetoothConfig() |
| 42 | + if debug { |
| 43 | + cfg.Logger = slog.New(slog.NewTextHandler(machine.USBCDC, &slog.HandlerOptions{ |
| 44 | + Level: slog.LevelDebug - 2, |
| 45 | + })) |
| 46 | + } |
| 47 | + |
| 48 | + err := dev.Init(cfg) |
| 49 | + if err != nil { |
| 50 | + if debug { |
| 51 | + println("Error initializing CYW43439 device", err.Error()) |
| 52 | + } |
| 53 | + return err |
| 54 | + } |
| 55 | + |
| 56 | + transport := &hciSPI{dev: dev} |
| 57 | + |
| 58 | + a.hci, a.att = newBLEStack(transport) |
| 59 | + if debug { |
| 60 | + println("Enabling CYW43439 device") |
| 61 | + } |
| 62 | + |
| 63 | + a.enable() |
| 64 | + |
| 65 | + if debug { |
| 66 | + println("Enabled CYW43439 device") |
| 67 | + } |
| 68 | + |
| 69 | + return nil |
| 70 | +} |
| 71 | + |
| 72 | +type hciSPI struct { |
| 73 | + dev *cyw43439.Device |
| 74 | +} |
| 75 | + |
| 76 | +func (h *hciSPI) startRead() { |
| 77 | +} |
| 78 | + |
| 79 | +func (h *hciSPI) endRead() { |
| 80 | +} |
| 81 | + |
| 82 | +func (h *hciSPI) Buffered() int { |
| 83 | + return h.dev.BufferedHCI() |
| 84 | +} |
| 85 | + |
| 86 | +func (h *hciSPI) ReadByte() (byte, error) { |
| 87 | + var buf [1]byte |
| 88 | + |
| 89 | + r, err := h.dev.HCIReadWriter() |
| 90 | + if err != nil { |
| 91 | + return 0, err |
| 92 | + } |
| 93 | + if _, err := r.Read(buf[:]); err != nil { |
| 94 | + return 0, err |
| 95 | + } |
| 96 | + |
| 97 | + return buf[0], nil |
| 98 | +} |
| 99 | + |
| 100 | +func (h *hciSPI) Read(buf []byte) (int, error) { |
| 101 | + r, err := h.dev.HCIReadWriter() |
| 102 | + if err != nil { |
| 103 | + return 0, err |
| 104 | + } |
| 105 | + |
| 106 | + return r.Read(buf) |
| 107 | +} |
| 108 | + |
| 109 | +func (h *hciSPI) Write(buf []byte) (int, error) { |
| 110 | + w, err := h.dev.HCIReadWriter() |
| 111 | + if err != nil { |
| 112 | + return 0, err |
| 113 | + } |
| 114 | + |
| 115 | + return w.Write(buf) |
| 116 | +} |
0 commit comments