goland操作蓝牙操作gpio

源码 加入GPIO的部分

package main

// This example implements a NUS (Nordic UART Service) peripheral.
// I can't find much official documentation on the protocol, but this can be
// helpful:
// https://learn.adafruit.com/introducing-adafruit-ble-bluetooth-low-energy-friend/uart-service
//
// Code to interact with a raw terminal is in separate files with build tags.

import (
	"fmt"
	"os"

	"tinygo.org/x/bluetooth"
	"tinygo.org/x/bluetooth/rawterm"

	"github.com/stianeikeland/go-rpio/v4"
)

var (
	serviceUUID = bluetooth.ServiceUUIDNordicUART
	rxUUID      = bluetooth.CharacteristicUUIDUARTRX
	txUUID      = bluetooth.CharacteristicUUIDUARTTX
	pin         = rpio.Pin(21)
)

func main() {
	println("starting")
	if err := rpio.Open(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	// Unmap gpio memory when done
	defer rpio.Close()

	// Set pin to output mode
	pin.Output()

	adapter := bluetooth.DefaultAdapter
	must("enable BLE stack", adapter.Enable())
	adv := adapter.DefaultAdvertisement()
	must("config adv", adv.Configure(bluetooth.AdvertisementOptions{
		LocalName:    "NUS", // Nordic UART Service
		ServiceUUIDs: []bluetooth.UUID{serviceUUID},
	}))
	must("start adv", adv.Start())

	var rxChar bluetooth.Characteristic
	var txChar bluetooth.Characteristic
	must("add service", adapter.AddService(&bluetooth.Service{
		UUID: serviceUUID,
		Characteristics: []bluetooth.CharacteristicConfig{
			{
				Handle: &rxChar,
				UUID:   rxUUID,
				Flags:  bluetooth.CharacteristicWritePermission | bluetooth.CharacteristicWriteWithoutResponsePermission,
				WriteEvent: func(client bluetooth.Connection, offset int, value []byte) {
					txChar.Write(value)

					for _, c := range value {
						rawterm.Putchar(c)
						fmt.Println(c)
						if c == '1' {
							pin.High()
							fmt.Println("on")
						} else if c == '2' {
							pin.Low()
							fmt.Println("off")
						} else if c == '3' {
							pin.Toggle()
							fmt.Println("Toggle")
						}

					}
				},
			},
			{
				Handle: &txChar,
				UUID:   txUUID,
				Flags:  bluetooth.CharacteristicNotifyPermission | bluetooth.CharacteristicReadPermission,
			},
		},
	}))

	rawterm.Configure()
	defer rawterm.Restore()
	print("NUS console enabled, use Ctrl-X to exit\r\n")
	var line []byte
	for {
		ch := rawterm.Getchar()
		rawterm.Putchar(ch)
		line = append(line, ch)

		// Send the current line to the central.
		if ch == '\x18' {
			// The user pressed Ctrl-X, exit the terminal.
			break
		} else if ch == '\n' {
			sendbuf := line // copy buffer
			// Reset the slice while keeping the buffer in place.
			line = line[:0]

			// Send the sendbuf after breaking it up in pieces.
			for len(sendbuf) != 0 {
				// Chop off up to 20 bytes from the sendbuf.
				partlen := 20
				if len(sendbuf) < 20 {
					partlen = len(sendbuf)
				}
				part := sendbuf[:partlen]
				sendbuf = sendbuf[partlen:]
				// This also sends a notification.
				_, err := txChar.Write(part)
				must("send notification", err)
			}
		}
	}
}

func must(action string, err error) {
	if err != nil {
		panic("failed to " + action + ": " + err.Error())
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值