Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples: add example that makes connections to multiple peripherals #318

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

deadprogram
Copy link
Member

@deadprogram deadprogram commented Jan 3, 2025

This PR adds a new example that makes connections to multiple peripherals.

It has been tested and is known to be working on Linux and on HCI with NINAFW, as long as #317 has been merged first.

On Linux I tested with 6 simultaneous connections. It might allow more.
On HCI I tested with 3 simultaneous connects. Again, more might also work.

@ysoldak
Copy link
Contributor

ysoldak commented Jan 8, 2025

What is the main purpose of the example? Show it is possible to connect to several peripherals and subscribe to notifications from them? If so, I find all this dance with specifying device addresses unnecessary -- it just bloats the example code. Can we instead just subscribe to any HR bluetooth device we can find? Simplifies code and makes it easier to try out.

@ysoldak
Copy link
Contributor

ysoldak commented Jan 8, 2025

I've tried to see if it works with SoftDevice (xiao-ble) and it does not seem to do.

$ tinygo flash -target xiao-ble -ldflags="-X main.Devices=84:CC:A8:2E:C1:CE,84:CC:A8:79:12:F6" -monitor ./examples/multiples/
connected to 84:CC:A8:2E:C1:CE
connected to 84:CC:A8:79:12:F6
discovering services/characteristics

It is stuck here ☝️

Two heart rate boards are Nano-RP2040, flashed like this:

tinygo flash -target=nano-rp2040 --size=short ./examples/heartrate

// ContextWithSignal creates a context canceled when SIGINT or SIGTERM are notified
func contextWithSignal(ctx context.Context) context.Context {
newCtx, cancel := context.WithCancel(ctx)
signals := make(chan os.Signal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
signals := make(chan os.Signal)
signals := make(chan os.Signal, 1)

My editor warned me the channel shall be buffered here.

select {
case <-signals:
cancel()
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole block can be simplified:

	go func() {
		<-signals
		cancel()
	}()

// Replace this by using -ldflags="-X main.Devices='[MAC ADDRESS],[MAC ADDRESS]'"
// where [MAC ADDRESS] is the actual MAC address of the peripheral.
// For example:
// tinygo flash -target nano-rp2040 -ldflags="-X main.Devices='7B:36:98:8C:41:1C,7B:36:98:8C:41:1D" ./examples/heartrate-monitor/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// tinygo flash -target nano-rp2040 -ldflags="-X main.Devices='7B:36:98:8C:41:1C,7B:36:98:8C:41:1D" ./examples/heartrate-monitor/
// tinygo flash -target nano-rp2040 -ldflags="-X main.Devices='7B:36:98:8C:41:1C,7B:36:98:8C:41:1D" ./examples/multiples/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants