-
Notifications
You must be signed in to change notification settings - Fork 44
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
adxl345: I²C support #67
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
a0f580f
adxl345: Driver, initial commit for review.
benoit-pereira-da-silva d3ad1aa
Added sensitivity option.
benoit-pereira-da-silva 006e2f0
Keep it simple remove useless Acceleration func
benoit-pereira-da-silva 76bf276
Expose the simpliest api possible.
benoit-pereira-da-silva 9f4770f
Added a bunch of clarifications
benoit-pereira-da-silva 75b1480
Sensitivity String returns a human readable value
benoit-pereira-da-silva 7e0a946
- Mark the register constants more explicitly.
benoit-pereira-da-silva 99ce1e2
Licence compliance, adding Copyright 2023 The Periph Authors. All rig…
benoit-pereira-da-silva a5a4a2c
Doc
benoit-pereira-da-silva b62bbe2
Prepare to support I2C in the future
benoit-pereira-da-silva 9ddc052
Some adxlXXX device may be well-supported so i added an internal swit…
benoit-pereira-da-silva 5eba749
Added I²C support.
benoit-pereira-da-silva 3dfa619
Fixing inconsistent Read of device id on multiple instantiation.
benoit-pereira-da-silva 8778b8a
adxl345: Driver, initial commit for review.
benoit-pereira-da-silva 641bb8a
Added sensitivity option.
benoit-pereira-da-silva dc6dcef
Keep it simple remove useless Acceleration func
benoit-pereira-da-silva e9a2fa7
Expose the simpliest api possible.
benoit-pereira-da-silva 4c68073
Added a bunch of clarifications
benoit-pereira-da-silva 490bf3b
Sensitivity String returns a human readable value
benoit-pereira-da-silva d22b104
- Mark the register constants more explicitly.
benoit-pereira-da-silva d4248eb
Licence compliance, adding Copyright 2023 The Periph Authors. All rig…
benoit-pereira-da-silva 8f0a10c
Doc
benoit-pereira-da-silva c7ff3e4
Prepare to support I2C in the future
benoit-pereira-da-silva c666127
Some adxlXXX device may be well-supported so i added an internal swit…
benoit-pereira-da-silva 5fec9d4
Added I²C support.
benoit-pereira-da-silva d15a6e9
Fixing inconsistent Read of device id on multiple instantiation.
benoit-pereira-da-silva c89b202
Merge branch 'main' of github.com:benoit-pereira-da-silva/devices int…
benoit-pereira-da-silva 60fc493
moving example to example_test.go file
benoit-pereira-da-silva 0c715a3
Renamed ExampleNewI2C & ExampleNewSpi to follow the recommandations. …
benoit-pereira-da-silva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Copyright 2023 The Periph Authors. All rights reserved. | ||
// Use of this source code is governed under the Apache License, Version 2.0 | ||
// that can be found in the LICENSE file. | ||
|
||
package adxl345 | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"periph.io/x/conn/v3/i2c/i2creg" | ||
"periph.io/x/conn/v3/spi/spireg" | ||
"periph.io/x/host/v3" | ||
"time" | ||
) | ||
|
||
var I2CAddr uint16 = 0x53 | ||
|
||
// ExampleNewI2C uses an adxl345 device connected by I²C. | ||
// You can set the I²C address by setting I2CAddr (default is 0x53). | ||
// it reads the acceleration values every 30ms for 30 seconds. | ||
// You can i use `i2dctools` to find the I²C bus number | ||
// e.g : sudo apt-get install i2c-tools | ||
// | ||
// sudo i2cdetect -y 1 | ||
func ExampleNewI2C() { | ||
mustInitHost() | ||
|
||
// Use i2creg to find the first available I²C bus. | ||
// Generally I2C1 on raspberry pi. | ||
p, err := i2creg.Open("") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Print(p.String()) | ||
|
||
defer p.Close() | ||
|
||
d, err := NewI2C(p, I2CAddr, &DefaultOpts) | ||
if err != nil { | ||
panic(err) | ||
} | ||
measure(d, 30*time.Second) | ||
} | ||
|
||
// ExampleNewSpi uses an adxl345 device connected by SPI. | ||
// it reads the acceleration values every 30ms for 30 seconds. | ||
func ExampleNewSpi() { | ||
|
||
mustInitHost() | ||
|
||
// Use spireg SPI port registry to find the first available SPI bus. | ||
p, err := spireg.Open("") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
defer p.Close() | ||
|
||
d, err := NewSpi(p, &DefaultOpts) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
measure(d, 30*time.Second) | ||
} | ||
|
||
// mustInitHost Make sure host is initialized. | ||
func mustInitHost() { | ||
|
||
if _, err := host.Init(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
// measure reads the acceleration values every 30ms for <duration> seconds | ||
func measure(d *Dev, duration time.Duration) { | ||
|
||
fmt.Println(d.String()) | ||
|
||
mode := d.Mode() | ||
// use a ticker to read the acceleration values every 200ms | ||
ticker := time.NewTicker(30 * time.Millisecond) | ||
defer ticker.Stop() | ||
|
||
// stop after 3 seconds | ||
stop := time.After(duration) | ||
|
||
for { | ||
select { | ||
case <-stop: | ||
return | ||
case <-ticker.C: | ||
a := d.Update() | ||
fmt.Println(mode, a) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: if you use
package adxl345_test
, the example in the documentation becomes copy-pastable as-is.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll resolve to commit, this can be done in a follow up and is optional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been pushing the change.