Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions lib/communication/sensors/bh1750.dart
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;
}
}
}
133 changes: 133 additions & 0 deletions lib/communication/sensors/tsl2561.dart
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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
int id = int.parse(idList[0].toRadixString(16), radix: 16);
logger.d("$tag: ID: $id");
return id;
int id = idList[0];
logger.d("$tag: ID: $id");
return id;

} 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;
}
}
}
32 changes: 25 additions & 7 deletions lib/providers/luxmeter_config_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/foundation.dart';
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:pslab/models/luxmeter_config.dart';
import 'package:pslab/others/logger_service.dart';

class LuxMeterConfigProvider extends ChangeNotifier {
LuxMeterConfig _config = const LuxMeterConfig();
Expand All @@ -13,18 +14,30 @@ class LuxMeterConfigProvider extends ChangeNotifier {
}

Future<void> _loadConfigFromPrefs() async {
final prefs = await SharedPreferences.getInstance();
final jsonString = prefs.getString('lux_config');
if (jsonString != null) {
final Map<String, dynamic> jsonMap = json.decode(jsonString);
_config = LuxMeterConfig.fromJson(jsonMap);
try {
final prefs = await SharedPreferences.getInstance();
final jsonString = prefs.getString('lux_config');
if (jsonString != null) {
final Map<String, dynamic> jsonMap = json.decode(jsonString);
_config = LuxMeterConfig.fromJson(jsonMap);
logger.d("Loaded LuxMeterConfig: ${_config.toJson()}");
notifyListeners();
}
} catch (e) {
logger.e("Error loading LuxMeterConfig from prefs: $e");
_config = const LuxMeterConfig();
notifyListeners();
}
}

Future<void> _saveConfigToPrefs() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('lux_config', json.encode(_config.toJson()));
try {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('lux_config', json.encode(_config.toJson()));
logger.d("Saved LuxMeterConfig: ${_config.toJson()}");
} catch (e) {
logger.e("Error saving LuxMeterConfig to prefs: $e");
}
}

void updateConfig(LuxMeterConfig newConfig) {
Expand All @@ -46,6 +59,11 @@ class LuxMeterConfigProvider extends ChangeNotifier {
}

void updateActiveSensor(String activeSensor) {
if (activeSensor != "In-built Sensor" &&
activeSensor != "BH1750" &&
activeSensor != "TSL2561") {
activeSensor = "In-built Sensor";
}
_config = _config.copyWith(activeSensor: activeSensor);
notifyListeners();
_saveConfigToPrefs();
Expand Down
Loading
Loading