GO修改直线代码为事件驱动/蓝牙主机测试

package main

import (
	"fmt"
	"time"
)

type WsManager struct {
	chan_disconnect    chan bool
	chan_reportNewCome chan string
	chan_reportStatus  chan string
	chan_reportHexData chan []byte
	chan_issueNodeCmd  chan string
	chan_issueNodeOta  chan string
	chan_issueGwOta    chan string
}

//结构体模板
var manager = WsManager{
	chan_disconnect:    make(chan bool),
	chan_reportNewCome: make(chan string),
	chan_reportStatus:  make(chan string),
	chan_reportHexData: make(chan []byte),
	chan_issueNodeCmd:  make(chan string),
	chan_issueNodeOta:  make(chan string),
	chan_issueGwOta:    make(chan string),
}

func (manager *WsManager) start() {
	for {
		select {
		case disconnect := <-manager.chan_disconnect:
			fmt.Println(disconnect, "go to do reconnect")
			cnt := 0
			for {
				time.Sleep(time.Second * 2)
				fmt.Println("FOR...")
				cnt = cnt + 1
				if cnt == 3 {
					break
				}

			}
		case reportNewCome := <-manager.chan_reportNewCome:
			fmt.Println(reportNewCome, "go to do wssend -get node ctrl cmd")
		case reportStatus := <-manager.chan_reportStatus:
			fmt.Println(reportStatus, "go to do wssend -send node status")
		case reportHexData := <-manager.chan_reportHexData:
			fmt.Println(reportHexData, "go to do wssend -send hex data")
		case issueNodeCmd := <-manager.chan_issueNodeCmd:
			fmt.Println(issueNodeCmd, "go to do ble -cmd ble cmd")
		case issueNodeOta := <-manager.chan_issueNodeOta:
			fmt.Println(issueNodeOta, "go to do ble -cmd ble ota")
		case issueGwOta := <-manager.chan_issueGwOta:
			fmt.Println(issueGwOta, "go to do gw ota")
		}
	}
}

func main() {
	fmt.Println("start")
	go manager.start()

	manager.chan_disconnect <- true
	time.Sleep(time.Second)

	manager.chan_reportNewCome <- "112233445566"
	time.Sleep(time.Second)

	manager.chan_reportStatus <- "Normal"
	time.Sleep(time.Second)

	manager.chan_reportHexData <- []byte{0xAA, 0xBB}
	time.Sleep(time.Second)

	manager.chan_issueNodeCmd <- "ledon"
	time.Sleep(time.Second)

	manager.chan_issueNodeOta <- "V6.6"
	time.Sleep(time.Second)

	manager.chan_issueGwOta <- "V9.0"
	time.Sleep(time.Second)

	time.Sleep(time.Second)
	fmt.Println("end")
}

++++++++++++go的exanple一样

package main

import (
	"fmt"
	"time"

	"tinygo.org/x/bluetooth"
)

type BleManager struct {
	Chan_disconnect chan bool
	Chan_CCCD       chan bool
	Chan_ReadErr    chan struct{}
	Chan_SendtoNode chan []byte

	adapter     *bluetooth.Adapter
	foundDevice bluetooth.ScanResult
	rx          bluetooth.DeviceCharacteristic
	tx          bluetooth.DeviceCharacteristic
}

var (
	serviceUUID = bluetooth.ServiceUUIDNordicUART
	rxUUID      = bluetooth.CharacteristicUUIDUARTRX
	txUUID      = bluetooth.CharacteristicUUIDUARTTX
)

//结构体模板
var Blem = BleManager{
	Chan_disconnect: make(chan bool),
	Chan_CCCD:       make(chan bool),
	Chan_ReadErr:    make(chan struct{}),
}

func Scan() bool {
	err := Blem.adapter.Scan(func(adapter *bluetooth.Adapter, result bluetooth.ScanResult) {
		/*
			if !result.AdvertisementPayload.HasServiceUUID(serviceUUID) {

				fmt.Println(result)
				fmt.Println(result.AdvertisementPayload)
				fmt.Println()
				return
			}
		*/
		fmt.Println(result)
		fmt.Println(result.AdvertisementPayload)
		fmt.Println()
		if result.AdvertisementPayload.LocalName() != "M_KOSON" {
			return
		}

		Blem.foundDevice = result

		// Stop the scan.
		err := adapter.StopScan()
		if err != nil {
			// Unlikely, but we can't recover from this.
			println("failed to stop the scan:", err.Error())
		}
	})
	if err != nil {
		println("could not start a scan:", err.Error())
		return false
	}
	println("Scan Ok")
	return true
}

func Connect() bool {
	if name := Blem.foundDevice.LocalName(); name == "" {
		print("Connecting to ", Blem.foundDevice.Address.String(), "...")
		println()
	} else {
		print("Connecting to ", name, " (", Blem.foundDevice.Address.String(), ")...")
		println()
	}

	// Found a NUS peripheral. Connect to it.
	device, err := Blem.adapter.Connect(Blem.foundDevice.Address, bluetooth.ConnectionParams{})
	if err != nil {
		println("Failed to connect:", err.Error())
		return false
	}

	// Connected. Look up the Nordic UART Service.
	println("Discovering service...")
	services, err := device.DiscoverServices([]bluetooth.UUID{serviceUUID})
	if err != nil {
		println("Failed to discover the Nordic UART Service:", err.Error())
		return false
	}
	service := services[0]

	// Get the two characteristics present in this service.
	chars, err := service.DiscoverCharacteristics([]bluetooth.UUID{rxUUID, txUUID})
	if err != nil {
		println("Failed to discover RX and TX characteristics:", err.Error())
		return false
	}

	if chars[0].UUID() == txUUID {
		Blem.tx = chars[0]
		Blem.rx = chars[1]
	} else {
		Blem.tx = chars[1]
		Blem.rx = chars[0]
	}
	fmt.Printf("[BLE] rx service %v\r\n", Blem.rx)
	fmt.Printf("[BLE] tx service %v\r\n", Blem.tx)
	return true

}
func (Blem *BleManager) Start() {

	Blem.adapter = bluetooth.DefaultAdapter
	// Enable BLE interface.
	err := Blem.adapter.Enable()
	if err != nil {
		println("could not enable the BLE stack:", err.Error())
		return
	}

	// Scan for NUS peripheral.

	for {
		select {

		case disconnect := <-Blem.Chan_disconnect:
			fmt.Println(disconnect, "go to do scan")
			println("Scanning...")
		tag:
			if Scan() == false {
				goto tag
			}
			if Connect() == false {
				goto tag
			}
		case cccd := <-Blem.Chan_CCCD:
			fmt.Println(cccd, "go to enable notify")
			err = Blem.tx.EnableNotifications(func(value []byte) {
				fmt.Printf("+v\r\n", value)

			})

		case data := <-Blem.Chan_SendtoNode:
			fmt.Println(data, "go to ble send")
			_, err := Blem.rx.WriteWithoutResponse(data)
			if err != nil {
				println("could not send:", err.Error())
			}
		case reportNewCome := <-Blem.Chan_ReadErr:
			fmt.Println(reportNewCome, "go to do wssend -get node ctrl cmd")

		}
	}
}

func main() {
	fmt.Println("start")
	go Blem.Start()

	time.Sleep(time.Second * 2)
	Blem.Chan_disconnect <- true

	time.Sleep(time.Second)
	Blem.Chan_CCCD <- true

	for {
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值