diff --git a/Examples/Microsoft.PowerShell.IoT.ADXL345/Microsoft.PowerShell.IoT.ADXL345.psd1 b/Examples/Microsoft.PowerShell.IoT.ADXL345/Microsoft.PowerShell.IoT.ADXL345.psd1 new file mode 100644 index 0000000..33931dd --- /dev/null +++ b/Examples/Microsoft.PowerShell.IoT.ADXL345/Microsoft.PowerShell.IoT.ADXL345.psd1 @@ -0,0 +1,37 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +@{ + +# Root module file +RootModule = 'Microsoft.PowerShell.IoT.ADXL345.psm1' + +# Version number of this module. +ModuleVersion = '0.1.0' + +# ID used to uniquely identify this module +GUID = '78f2d4bb-195e-4143-8f54-0a6d8c68612e' + +# Author of this module +Author = 'Julien Nury' + +# Description of the functionality provided by this module +Description = 'A set of functions to interact with ADXL345s accelerometer thru I2C' + +# Modules that must be imported into the global environment prior to importing this module +RequiredModules = @('Microsoft.PowerShell.IoT') + +# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. +FunctionsToExport = 'Get-ADXL345Device', 'Get-ADXL345Data' + +# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. +CmdletsToExport = @() + +# Variables to export from this module +VariablesToExport = @() + +# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. +AliasesToExport = @() + +} + diff --git a/Examples/Microsoft.PowerShell.IoT.ADXL345/Microsoft.PowerShell.IoT.ADXL345.psm1 b/Examples/Microsoft.PowerShell.IoT.ADXL345/Microsoft.PowerShell.IoT.ADXL345.psm1 new file mode 100644 index 0000000..5c04366 --- /dev/null +++ b/Examples/Microsoft.PowerShell.IoT.ADXL345/Microsoft.PowerShell.IoT.ADXL345.psm1 @@ -0,0 +1,120 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +# Inspired from https://github.com/sparkfun/SparkFun_ADXL345_Arduino_Library + +# Device registers and parameters + +$script:ADXL345_ADDRESS = 0x53 # I2c address +$script:ADXL345_POWER_CTL = 0x2D # Power-Saving Features Control +$script:ADXL345_DATAX0 = 0x32 # X-Axis Data 0 +$script:ADXL345_DATAX1 = 0x33 # X-Axis Data 1 +$script:ADXL345_DATAY0 = 0x34 # Y-Axis Data 0 +$script:ADXL345_DATAY1 = 0x35 # Y-Axis Data 1 +$script:ADXL345_DATAZ0 = 0x36 # Z-Axis Data 0 +$script:ADXL345_DATAZ1 = 0x37 # Z-Axis Data 1 +$script:ADXL345_GAINX = 0.00376390 # Gain to convert X value in g +$script:ADXL345_GAINY = 0.00376009 # Gain to convert Y value in g +$script:ADXL345_GAINZ = 0.00349265 # Gain to convert Z value in g + +# Published functions +function Get-ADXL345Device { + [CmdletBinding()] + param + ( + [ValidateNotNullOrEmpty()] + [int] + $Id = $script:ADXL345_ADDRESS, + + [ValidateNotNullOrEmpty()] + [string] + $FriendlyName = "ADXL345" + ) + + $Device = Get-I2CDevice -Id $Id -FriendlyName $FriendlyName + InitializeDevice -Device $Device + return $Device +} + +function Get-ADXL345Data { + param ( + [Parameter(ValueFromPipeline)] + [ValidateNotNullOrEmpty()] + [Microsoft.PowerShell.IoT.I2CDevice] + $Device = (Get-ADXL345Device), + + [Parameter()] + [Double] + $Limit, + + [Switch] + $Raw + ) + + try { + $xValue0 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAX0 -ByteCount 1 + $xValue1 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAX1 -ByteCount 1 + $xValue = [int16]($xValue1.Data[0]) -shl 8 -bor [int16]($xValue0.Data[0]) + + $yValue0 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAY0 -ByteCount 1 + $yValue1 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAY1 -ByteCount 1 + $yValue = [int16]($yValue1.Data[0]) -shl 8 -bor [int16]($yValue0.Data[0]) + + $zValue0 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAZ0 -ByteCount 1 + $zValue1 = Get-I2CRegister -Device $Device -Register $script:ADXL345_DATAZ1 -ByteCount 1 + $zValue = [int16]($zValue1.Data[0]) -shl 8 -bor [int16]($zValue0.Data[0]) + } + catch { + Throw "Unable to retreive data from device '$($Device.FriendlyName)'. Message: $($_.Exception.Message)" + } + + if (-not $Raw) { + $xValue = $xValue * $script:ADXL345_GAINX + $yValue = $yValue * $script:ADXL345_GAINY + $zValue = $zValue * $script:ADXL345_GAINZ + } + + if ($Limit) { + $xValue = FilterValue -Value $xValue -Maximum $Limit + $yValue = FilterValue -Value $yValue -Maximum $Limit + $zValue = FilterValue -Value $zValue -Maximum $Limit + } + + return [PSObject]@{x = $xValue; y = $yValue; z = $zvalue} +} + +# Internal functions +function InitializeDevice { + Param ( + [Parameter(Mandatory)] + [Microsoft.PowerShell.IoT.I2CDevice] + $Device + ) + + try { + Set-I2CRegister -Device $Device -Register $script:ADXL345_POWER_CTL -Data 0x00 + Set-I2CRegister -Device $Device -Register $script:ADXL345_POWER_CTL -Data 0x10 + Set-I2CRegister -Device $Device -Register $script:ADXL345_POWER_CTL -Data 0x08 + } + catch { + Throw "Unable to initialize device '$($Device.FriendlyName)'. Message: $($_.Exception.Message)" + } +} + +function FilterValue { + param ( + [Parameter(Mandatory)] + [Double] + $Value, + + [Parameter(Mandatory)] + [Double] + $Maximum + ) + + if ($Value -ge 0) { + return [Math]::Min($value, $Maximum) + } else { + return [Math]::Max($Value, -$Maximum) + } +} diff --git a/Examples/Microsoft.PowerShell.IoT.ADXL345/README.md b/Examples/Microsoft.PowerShell.IoT.ADXL345/README.md new file mode 100644 index 0000000..75f6b29 --- /dev/null +++ b/Examples/Microsoft.PowerShell.IoT.ADXL345/README.md @@ -0,0 +1,74 @@ +# Example module Microsoft.PowerShell.IoT.ADXL345 + +This PowerShell module is to interact with [ADXL345 accelerometer](http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf) for reading acceleration on 3 axis. + +## Hardware setup + +Several vendors have breakout boards with ADXL345 sensor. In this example we'll use [SparkFun Triple Axis Accelerometer Breakout](https://www.sparkfun.com/products/9836). + +ADXL345 sensor supports both I2C and SPI interfaces; here we'll use I2C. + +Wiring diagram with Raspberry Pi 3 looks like this: + +![ADXL345_Wiring](https://user-images.githubusercontent.com/9315492/39673576-40f7e8b4-513f-11e8-8b69-314237f99bd1.png) + +## Software setup + +### Install PowerShell Core on Raspberry Pi + +Installation instructions can be found [here](https://github.com/PowerShell/PowerShell/tree/master/docs/installation/linux.md#raspbian). + +### Enable I2C interface on Raspberry Pi + +1. `sudo raspi-config` +2. `5 Interfacing options` +3. `P5 I2C` +4. `Would you like ARM I2C interface to be enabled -> Yes` + +### Start Powershell and install modules + +**Don't forget to start PowerShell with sudo** or you'll be unable to access I2C bus. + +```powershell +sudo pwsh +Install-Module -Name Microsoft.PowerShell.IoT +git clone https://github.com/PowerShell/PowerShell-IoT.git +Import-Module ./PowerShell-IoT/Examples/Microsoft.PowerShell.IoT.ADXL345 +``` + +### Collect Data + +To simply collect acceleration values in g: + +```powershell +PS /home/pi> $accelerometer = Get-ADXL345Device +PS /home/pi> Get-ADXL345Data -Device $accelerometer + +Name Value +---- ----- +y -0.03008072 +x 0.0828058 +z 0.86966985 +``` + +To represent current acceleration on the 3 axis with bargraphs: + +```powershell +PS /home/pi> $accelerometer = Get-ADXL345Device +PS /home/pi> while ($true) { + $data = Get-ADXL345Data -Device $accelerometer -Limit 1 + Write-Progress -id 1 -Activity 'X axis' -Status 'Acceleration' -PercentComplete ($data.x * 50 + 50) + Write-Progress -id 2 -Activity 'Y axis' -Status 'Acceleration' -PercentComplete ($data.y * 50 + 50) + Write-Progress -id 3 -Activity 'Z axis' -Status 'Acceleration' -PercentComplete ($data.z * 50 + 50) +} + +X axis +Acceleration +[ooooooooooooooooooooooooooooooooooooooooooooooooooo ] +Y axis +Acceleration +[ooooooooooooooooooooooooooooooooooooooooooooooooo ] +Z axis +Acceleration +[ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo ] +``` \ No newline at end of file diff --git a/Examples/README.md b/Examples/README.md index 6bce3df..b8aa594 100644 --- a/Examples/README.md +++ b/Examples/README.md @@ -9,4 +9,5 @@ Example modules that leverage PowerShell IoT in a specific way. | [Microsoft.PowerShell.IoT.LED](/Examples/Microsoft.PowerShell.IoT.LED) | Easy | GPIO | | [Microsoft.PowerShell.IoT.BME280](/Examples/Microsoft.PowerShell.IoT.BME280) | Medium | I2C | | [Microsoft.PowerShell.IoT.SSD1306](/Examples/Microsoft.PowerShell.IoT.SSD1306) | Medium | I2C | +| [Microsoft.PowerShell.IoT.ADXL345](/Examples/Microsoft.PowerShell.IoT.ADXL345) | Medium | I2C | | [Microsoft.PowerShell.Plant](/Examples/Microsoft.PowerShell.IoT.Plant) | Hard | GPIO |