-
Couldn't load subscription status.
- Fork 813
feat: added sensors bh1750 & tsl2561 to complete Luxmeter #2918
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import 'package:pslab/communication/peripherals/i2c.dart'; | ||
|
|
||
| class BH1750 { | ||
| final String tag = "BH1750"; | ||
|
|
||
| final int powerOn = 0x01; | ||
| final int reset = 0x07; | ||
| final int res1000mLx = 0x10; | ||
| final int res500mLx = 0x11; | ||
| final int res4000mLx = 0x13; | ||
|
|
||
| final I2C i2c; | ||
|
|
||
| final List<int> gainChoices = [0x11, 0x10, 0x13]; | ||
| final List<String> gainLiteralChoices = ["500mLx", "1000mLx", "4000mLx"]; | ||
| int gain = 0; | ||
| final List<double> scaling = [2, 1, 0.25]; | ||
|
|
||
| static const int numPlots = 1; | ||
| static const List<String> plotNames = ["Lux"]; | ||
| final int address = 0x23; | ||
| final String name = "Luminosity"; | ||
|
|
||
| BH1750(this.i2c) { | ||
| init(); | ||
| } | ||
|
|
||
| void init() { | ||
| i2c.writeBulk(address, [res500mLx]); | ||
| } | ||
|
|
||
| void setRange(String g) { | ||
| int gainIndex = gainLiteralChoices.indexOf(g); | ||
| if (gainIndex >= 0) { | ||
| i2c.writeBulk(address, [gainChoices[gainIndex]]); | ||
| } | ||
| } | ||
|
|
||
| Future<List<int>> getVals(int numBytes) async { | ||
| return await i2c.simpleRead(address, numBytes); | ||
| } | ||
|
|
||
| Future<double> getRaw() async { | ||
| List<int> vals = await getVals(2); | ||
| if (vals.length == 3) { | ||
| return ((vals[0] << 8) | vals[1]) / 1.2; | ||
| } else { | ||
| return 0.0; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,133 @@ | ||||||||||||||
| import 'dart:async'; | ||||||||||||||
| import 'package:pslab/communication/peripherals/i2c.dart'; | ||||||||||||||
| import 'package:pslab/communication/science_lab.dart'; | ||||||||||||||
| import 'package:pslab/others/logger_service.dart'; | ||||||||||||||
|
|
||||||||||||||
| class TSL2561 { | ||||||||||||||
| static const String tag = "TSL2561"; | ||||||||||||||
|
|
||||||||||||||
| static const int commandBit = 0x80; | ||||||||||||||
| static const int wordBit = 0x20; | ||||||||||||||
|
|
||||||||||||||
| static const int controlPowerOn = 0x03; | ||||||||||||||
| static const int controlPowerOff = 0x00; | ||||||||||||||
|
|
||||||||||||||
| static const int registerControl = 0x00; | ||||||||||||||
| static const int registerTiming = 0x01; | ||||||||||||||
| static const int registerId = 0x0A; | ||||||||||||||
| static const int registerChan0Low = 0x0C; | ||||||||||||||
| static const int registerChan1Low = 0x0E; | ||||||||||||||
|
|
||||||||||||||
| static const int integrationTime13ms = 0x00; | ||||||||||||||
| static const int integrationTime101ms = 0x01; | ||||||||||||||
| static const int integrationTime402ms = 0x02; | ||||||||||||||
|
|
||||||||||||||
| static const int gain0x = 0x00; | ||||||||||||||
| static const int gain16x = 0x10; | ||||||||||||||
|
|
||||||||||||||
| static const List<int> addresses = [0x39, 0x29, 0x49]; | ||||||||||||||
|
|
||||||||||||||
| final I2C i2c; | ||||||||||||||
| int address = 0x39; | ||||||||||||||
| int timing = integrationTime13ms; | ||||||||||||||
| int gain = gain16x; | ||||||||||||||
|
|
||||||||||||||
| TSL2561(this.i2c, ScienceLab scienceLab) { | ||||||||||||||
| () async { | ||||||||||||||
| if (scienceLab.isConnected()) { | ||||||||||||||
| for (final addr in addresses) { | ||||||||||||||
| address = addr; | ||||||||||||||
| await disable(); | ||||||||||||||
| logger.d("$tag: Checking address 0x${address.toRadixString(16)}"); | ||||||||||||||
| try { | ||||||||||||||
| int id = await i2c.readByte(address, registerId); | ||||||||||||||
| if (id != 0xffffffff && (id & 0x0A) == 0x0A) { | ||||||||||||||
| logger.d("$tag: TSL2561 found!"); | ||||||||||||||
| break; | ||||||||||||||
| } else { | ||||||||||||||
| logger.d("$tag: TSL2561 not found."); | ||||||||||||||
| } | ||||||||||||||
| } catch (e) { | ||||||||||||||
| logger.e("$tag: Error reading ID: $e"); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| await enable(); | ||||||||||||||
| await _wait(); | ||||||||||||||
| await i2c | ||||||||||||||
| .writeBulk(address, [commandBit | registerTiming, timing | gain]); | ||||||||||||||
| } | ||||||||||||||
| }(); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| Future<int> getID() async { | ||||||||||||||
| try { | ||||||||||||||
| List<int> idList = await i2c.readBulk(address, registerId, 1); | ||||||||||||||
| if (idList.isEmpty) return -1; | ||||||||||||||
| int id = int.parse(idList[0].toRadixString(16), radix: 16); | ||||||||||||||
| logger.d("$tag: ID: $id"); | ||||||||||||||
| return id; | ||||||||||||||
|
Comment on lines
+66
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Unnecessary conversion in getID: int.parse on a value already an int. The conversion from int to hex string and back to int is unnecessary; you can return idList[0] directly.
Suggested change
|
||||||||||||||
| } catch (e) { | ||||||||||||||
| logger.e("$tag: Error getting ID: $e"); | ||||||||||||||
| rethrow; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| Future<double?> getRaw() async { | ||||||||||||||
| try { | ||||||||||||||
| List<int> infraList = await i2c.readBulk( | ||||||||||||||
| address, commandBit | wordBit | registerChan1Low, 2); | ||||||||||||||
| List<int> fullList = await i2c.readBulk( | ||||||||||||||
| address, commandBit | wordBit | registerChan0Low, 2); | ||||||||||||||
|
|
||||||||||||||
| if (infraList.isNotEmpty && fullList.isNotEmpty) { | ||||||||||||||
| int full = ((fullList[1] & 0xff) << 8) | (fullList[0] & 0xff); | ||||||||||||||
| int infra = ((infraList[1] & 0xff) << 8) | (infraList[0] & 0xff); | ||||||||||||||
| return (full - infra).toDouble(); | ||||||||||||||
| } else { | ||||||||||||||
| return 0.0; | ||||||||||||||
| } | ||||||||||||||
| } catch (e) { | ||||||||||||||
| logger.e("$tag: Error reading raw values: $e"); | ||||||||||||||
| return 0.0; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| Future<void> setGain(int gainValue) async { | ||||||||||||||
| switch (gainValue) { | ||||||||||||||
| case 1: | ||||||||||||||
| gain = gain0x; | ||||||||||||||
| break; | ||||||||||||||
| case 16: | ||||||||||||||
| gain = gain16x; | ||||||||||||||
| break; | ||||||||||||||
| default: | ||||||||||||||
| gain = gain16x; | ||||||||||||||
| } | ||||||||||||||
| await i2c.writeBulk(address, [commandBit | registerTiming, gain | timing]); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| Future<void> enable() async { | ||||||||||||||
| await i2c | ||||||||||||||
| .writeBulk(address, [commandBit | registerControl, controlPowerOn]); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| Future<void> disable() async { | ||||||||||||||
| await i2c | ||||||||||||||
| .writeBulk(address, [commandBit | registerControl, controlPowerOff]); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| Future<void> _wait() async { | ||||||||||||||
| switch (timing) { | ||||||||||||||
| case integrationTime13ms: | ||||||||||||||
| await Future.delayed(const Duration(milliseconds: 14)); | ||||||||||||||
| break; | ||||||||||||||
| case integrationTime101ms: | ||||||||||||||
| await Future.delayed(const Duration(milliseconds: 102)); | ||||||||||||||
| break; | ||||||||||||||
| case integrationTime402ms: | ||||||||||||||
| default: | ||||||||||||||
| await Future.delayed(const Duration(milliseconds: 403)); | ||||||||||||||
| break; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.