Skip to content

Commit

Permalink
Merge pull request #11 from pharo-iot/dev
Browse files Browse the repository at this point in the history
BME280Device
  • Loading branch information
dionisiydk authored Feb 14, 2018
2 parents d35f08e + d88c8fa commit 07c5fb1
Show file tree
Hide file tree
Showing 31 changed files with 235 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,22 @@ baseline: spec
project: 'TelePharoServer' copyFrom: 'TelePharo' with: [
spec loads: 'Server'];
project: 'TelePharoClient' copyFrom: 'TelePharo' with: [
spec loads: 'Client'].

spec loads: 'Client'];
baseline: 'WiringPi' with: [
spec
repository: 'github://pharo-iot/WiringPi:v0.1.x';
loads: 'Core' ].

spec
package: #'PharoThings-Devices-Button' with: [
spec requires: #('PharoThings-Hardware-Core' ). ];
package: #'PharoThings-Devices-Switch' with: [
spec requires: #(#'PharoThings-Devices-Button' ). ];
package: 'PharoThings-GTTools' with: [
spec requires: #('PharoThings-Hardware-Core' ). ];
package: 'PharoThings-Hardware-Core';
package: 'PharoThings-Hardware-Core-Tests' with: [
spec requires: #('PharoThings-Hardware-Core' 'Mocketry' )];

package: #'PharoThings-Devices-I2C' with: [
spec requires: #('PharoThings-Hardware-Core' ) ];

package: #'PharoThings-Devices-ADXL345' with: [
spec requires: #('PharoThings-Devices-I2C' ) ] ;

baseline: 'WiringPi' with: [
spec
repository: 'github://pharo-iot/WiringPi:v0.1.x';
loads: 'Core' ];
spec requires: #('PharoThings-Hardware-Core' ) ];

package: #'PharoThings-Hardware-RaspberryPi' with: [
spec requires: #('PharoThings-Hardware-Core' #'WiringPi' ). ];

Expand All @@ -44,13 +37,18 @@ baseline: spec
package: #'PharoThings-RemoteToolsServer' with: [
spec requires: #('TelePharoServer' ). ];
package: #'PharoThings-RemoteToolsClient' with: [
spec requires: #('PharoThings-RemoteToolsServer' 'TelePharoClient'). ].
spec requires: #('PharoThings-RemoteToolsServer' 'TelePharoClient')].

self devices do: [ :each |
spec package: each key with: [ spec requires: #(#'PharoThings-Hardware-Core'), each value ]].

spec
group: 'Hardware' with: #('PharoThings-Hardware-Core' #'PharoThings-Devices-Button' #'PharoThings-Devices-Switch' #'PharoThings-Devices-I2C' #'PharoThings-Devices-ADXL345');
group: 'Hardware' with: #('PharoThings-Hardware-Core' #'PharoThings-Devices-I2C' 'Devices');
group: 'Raspberry' with: #('Hardware' #'PharoThings-Hardware-RaspberryPi' );
group: 'Arduino' with: #('Hardware' #'PharoThings-Hardware-Arduino' );

group: 'LocalDev' with: #('Raspberry' 'Arduino' 'PharoThings-GTTools' 'Tests');
group: 'Devices' with: (self devices collect: #key);

group: 'LocalDev' with: #('Raspberry' 'Arduino' 'Devices' 'PharoThings-GTTools' 'Tests');
group: 'RemoteDev' with: #('LocalDev' #'PharoThings-RemoteToolsClient');
group: 'RemoteDevServer' with: #('Hardware' #'PharoThings-RemoteToolsServer' );
group: 'Tests' with: #('PharoThings-Hardware-Core-Tests' );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
baselines
devices

^{
#'PharoThings-Devices-Button' -> #().
#'PharoThings-Devices-Switch' -> #(#'PharoThings-Devices-Button').

#'PharoThings-Devices-ADXL345' -> #(#'PharoThings-Devices-I2C').
#'PharoThings-Devices-MCP9808' -> #(#'PharoThings-Devices-I2C').
#'PharoThings-Devices-BME280' -> #(#'PharoThings-Devices-I2C').
}
5 changes: 5 additions & 0 deletions src/PharoThings-Devices-BME280.package/.filetree
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"separateMethodMetaAndSource" : false,
"noMethodMetaData" : true,
"useCypressPropertiesFile" : true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
I provide implementation of temperature/pressure/humidity sensor BME280Device.

The code for initialization and sensors reading is copied from Python example:

https://github.com/ControlEverythingCommunity/BME280/blob/master/Python/BME280.py

In contract to the Pythin example I retrieve all coefficients in connection (setup) time because they are constant parameters.

The method #readParameters returns three values: Celsius, hPa, humidity percents.

Internal Representation and Key Implementation Points.

Instance Variables
hCoeffs: <WordArray> "humidity coefficients"
pCoeffs: <WordArray> "pressure coefficients"
tCoeffs: <WordArray> "temperature coefficients"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
instance creation
defaultI2CAddress
^16r76
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
controlling
connect
"https://github.com/ControlEverythingCommunity/BME280/blob/master/Python/BME280.py"
super connect.
self initTempAndPressureCoeffs.
self initHumidityCoeffs.
"Select control humidity register, 0xF2(242)
0x01(01) Humidity Oversampling = 1"
i2cConnection write8BitsAt: 16rF2 data: 16r01.
"Select Control measurement register, 0xF4(244)
0x27(39) Pressure and Temperature Oversampling rate = 1
Normal mode"
i2cConnection write8BitsAt: 16rF4 data: 16r27.
" Select Configuration register, 0xF5(245)
0xA0(00) Stand_by time = 1000 ms"
i2cConnection write8BitsAt: 16rF5 data: 16rA0
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
decoding
decodeHumidityFrom: dataFromF7
"data: Humidity MSB, Humidity LSB
returns percents"
| raw temperature var_H humidity |
raw := ((dataFromF7 at: 7) << 8) | (dataFromF7 at: 8).
temperature := self decodeTemperatureFrom: dataFromF7.
var_H := (temperature * 5120.0) - 76800.0.
var_H := (raw - ((hCoeffs at: 4) * 64.0 + ((hCoeffs at: 5) / 16384.0 * var_H))) * ((hCoeffs at: 2) / 65536.0 * (1.0 + ((hCoeffs at: 6) / 67108864.0 * var_H * (1.0 + ((hCoeffs at: 3) / 67108864.0 * var_H))))).
humidity := var_H * (1.0 - ((hCoeffs at: 1) * var_H / 524288.0)).
^humidity min: 100 max: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
decoding
decodePressureFrom: dataFromF7
"data: Pressure MSB, Pressure LSB, Pressure xLSB
returns hPa"
| raw temperature var1 var2 p |
raw := ((dataFromF7 at: 1) << 12) | ((dataFromF7 at: 2) << 4) | ((dataFromF7 at: 3) >> 4).
temperature := self decodeTemperatureFrom: dataFromF7.
var1 := ((temperature * 5120.0) / 2.0) - 64000.0.
var2 := var1 * var1 * (pCoeffs at: 6) / 32768.0.
var2 := var2 + (var1 * (pCoeffs at: 5) * 2.0).
var2 := (var2 / 4.0) + ((pCoeffs at: 4) * 65536.0).
var1 := ((pCoeffs at: 3) * var1 * var1 / 524288.0 + (( pCoeffs at: 2) * var1)) / 524288.0.
var1 := (1.0 + (var1 / 32768.0)) * (pCoeffs at: 1).
p := 1048576.0 - raw.
p := (p - (var2 / 4096.0)) * 6250.0 / var1.
var1 := (pCoeffs at: 9) * p * p / 2147483648.0.
var2 := p * (pCoeffs at: 8) / 32768.0.
^(p + ((var1 + var2 + (pCoeffs at: 7)) / 16.0)) / 100
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
decoding
decodeSignedWordArrayFrom: coeffs at: index size: size
| result word |
result := Array new: size.
1 to: size do: [ :i |
word := self decodeSignedWordFrom: coeffs at: index + ((i - 1) * 2).
result at: i put: word ].
^ result
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
decoding
decodeSignedWordFrom: coeffs at: index
| unsigned |
unsigned := self decodeUnsignedWordFrom: coeffs at: index.
^ unsigned > 32767
ifTrue: [ unsigned - 65536 ]
ifFalse: [ unsigned ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
decoding
decodeTemperatureFrom: dataFromF7
"data: Temperature MSB, Temperature LSB, Temperature xLSB.
returns Celsius"
| raw var1 var2 |
raw := ((dataFromF7 at: 4) << 12) | ((dataFromF7 at: 5) << 4) | ((dataFromF7 at: 6) >> 4).

var1 := (raw / 16384.0 - ((tCoeffs at: 1) / 1024.0)) * (tCoeffs at: 2).
var2 := ((raw / 131072.0 - ((tCoeffs at: 1) / 8192.0)) * (raw / 131072.0 - ((tCoeffs at: 1) / 8192.0))) * (tCoeffs at: 3).
^(var1 + var2) / 5120.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
decoding
decodeUnsignedWordFrom: coeffs at: index
^(coeffs at: index + 1) * 256 + (coeffs at: index)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
controlling
initHumidityCoeffs
| data h1 h2 h3 h4 h5 h6 |
h1 := i2cConnection read8BitsAt: 16rA1.
data := i2cConnection read8BitsArray: 7 startingAt: 16rE1.
h2 := self decodeSignedWordFrom: data at: 1.
h3 := (data at: 3) bitAnd: 16rFF.
h4 := (data at: 4) * 16 + ((data at: 5) bitAnd: 16rF).
h4 > 32767 ifTrue: [ h4 := h4 - 65536].
h5 := ((data at: 5) / 16) + ((data at: 6) * 16).
h5 > 32767 ifTrue: [ h5 := h5 - 65536].
h6 := data at: 7.
h6 > 127 ifTrue: [ h6 := h6 - 256].
hCoeffs := {h1. h2. h3. h4. h5. h6}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
controlling
initTempAndPressureCoeffs

| data |
data := i2cConnection read8BitsArray: 24 startingAt: 16r88.

tCoeffs := {self decodeUnsignedWordFrom: data at: 1}
, (self decodeSignedWordArrayFrom: data at: 3 size: 2).
pCoeffs := {self decodeUnsignedWordFrom: data at: 7}
, (self decodeSignedWordArrayFrom: data at: 9 size: 8)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
controlling
readParameters
"Pressure MSB, Pressure LSB, Pressure xLSB, Temperature MSB, Temperature LSB
Temperature xLSB, Humidity MSB, Humidity LSB"
| data |
data := i2cConnection read8BitsArray: 8 startingAt: 16rF7.

^{
self decodeTemperatureFrom: data.
self decodePressureFrom: data.
self decodeHumidityFrom: data
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
controlling
readTemperature
^self readParameters first
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"commentStamp" : "DenisKudryashov 2/14/2018 16:01",
"super" : "PotI2CDevice",
"category" : "PharoThings-Devices-BME280",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [
"tCoeffs",
"hCoeffs",
"pCoeffs"
],
"name" : "PotBME280Device",
"type" : "normal"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SystemOrganization addCategory: #'PharoThings-Devices-BME280'!
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(name 'PharoThings-Devices-BME280')
1 change: 1 addition & 0 deletions src/PharoThings-Devices-BME280.package/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }
5 changes: 5 additions & 0 deletions src/PharoThings-Devices-MCP9808.package/.filetree
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"separateMethodMetaAndSource" : false,
"noMethodMetaData" : true,
"useCypressPropertiesFile" : true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
I provide implementation of temperature sensor MCP9808.

The code for initialization and sensors reading is copied from Python example:

https://github.com/ControlEverythingCommunity/MCP9808/blob/master/Python/MCP9808.py

The method #readTemperature returns the value in Celsius
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
instance creation
defaultI2CAddress
^16r18
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
controlling
connect
"The code is copied from Python:
https://github.com/ControlEverythingCommunity/MCP9808/blob/master/Python/MCP9808.py"
super connect.

"Select configuration register, 0x01(1)
0x0000(00) Continuous conversion mode, Power-up default"
i2cConnection write16BitsAt: 16r01 data: #[16r00 16r00] asInteger.

"Select resolution rgister, 0x08(8)
0x03(03) Resolution = +0.0625 / C"
i2cConnection write8BitsAt: 16r08 data: 16r03.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
controlling
readTemperature
| rawData ctemp |
"Read data back from 0x05(5), 2 bytes
Temp MSB, TEMP LSB"
rawData := (i2cConnection read16BitsAt: 16r05) asByteArray.

"Convert the data to 13-bits"
ctemp := ((rawData second bitAnd: 16r1F) * 256) + rawData first.
ctemp > 4095 ifTrue: [ ctemp := ctemp - 8192].
^ctemp * 0.0625
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"commentStamp" : "DenisKudryashov 2/13/2018 18:18",
"super" : "PotI2CDevice",
"category" : "PharoThings-Devices-MCP9808",
"classinstvars" : [ ],
"pools" : [ ],
"classvars" : [ ],
"instvars" : [ ],
"name" : "PotMCP9808Device",
"type" : "normal"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SystemOrganization addCategory: #'PharoThings-Devices-MCP9808'!
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(name 'PharoThings-Devices-MCP9808')
1 change: 1 addition & 0 deletions src/PharoThings-Devices-MCP9808.package/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ }

0 comments on commit 07c5fb1

Please sign in to comment.