Skip to content
Open
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/mlx90614.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'dart:async';
import '../peripherals/i2c.dart';

class MLX90614 {
final I2C i2c;
// The default I2C address for MLX90614 is 0x5A
static const int address = 0x5A;

// Register addresses
static const int ambientTempReg = 0x06;
static const int objectTempReg = 0x07;

MLX90614(this.i2c);

/// Reads the Ambient (Room) Temperature
Future<double> getAmbientTemperature() async {
return _readTemperature(ambientTempReg);
}

/// Reads the Object (Target) Temperature
Future<double> getObjectTemperature() async {
return _readTemperature(objectTempReg);
}

/// Helper function to handle the math
Future<double> _readTemperature(int reg) async {
// Read 2 bytes from the specific register
List<int> data = await i2c.readBulk(address, reg, 2);

if (data.length < 2) {
throw Exception("Failed to read temperature from MLX90614");
}

// MLX90614 sends LSB first, then MSB.
int lsb = data[0];
int msb = data[1];

// Combine the bytes: (MSB << 8) | LSB
// We apply the mask 0x7FFF to ignore the error flag (Bit 15)
// ensuring we only process valid temperature data bits.
int rawValue = ((msb << 8) | lsb) & 0x7FFF;

// Formula from datasheet:
// The sensor returns temperature in Kelvin * 50.
// Multiply by 0.02 to get Kelvin.
// Subtract 273.15 to convert Kelvin to Celsius.
double tempCelsius = (rawValue * 0.02) - 273.15;

return tempCelsius;
}
}
5 changes: 4 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -523,5 +523,8 @@
"light": "Light",
"darkExperimental": "Dark (Experimental)",
"system": "System",
"shareApp": "Share App"
"shareApp": "Share App",
"mlxObjectTemp": "Object Temperature",
"mlxAmbientTemp": "Ambient Temperature",
"mlx90614Config": "MLX90614 Configurations"
}
18 changes: 18 additions & 0 deletions lib/l10n/app_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3255,6 +3255,24 @@ abstract class AppLocalizations {
/// In en, this message translates to:
/// **'Share App'**
String get shareApp;

/// No description provided for @mlxObjectTemp.
///
/// In en, this message translates to:
/// **'Object Temperature'**
String get mlxObjectTemp;

/// No description provided for @mlxAmbientTemp.
///
/// In en, this message translates to:
/// **'Ambient Temperature'**
String get mlxAmbientTemp;

/// No description provided for @mlx90614Config.
///
/// In en, this message translates to:
/// **'MLX90614 Configurations'**
String get mlx90614Config;
}

class _AppLocalizationsDelegate
Expand Down
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_de.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,4 +1696,13 @@ class AppLocalizationsDe extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,4 +1696,13 @@ class AppLocalizationsEn extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_es.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,4 +1696,13 @@ class AppLocalizationsEs extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_fr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,4 +1696,13 @@ class AppLocalizationsFr extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_he.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,4 +1696,13 @@ class AppLocalizationsHe extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_hi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,4 +1696,13 @@ class AppLocalizationsHi extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_id.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,4 +1696,13 @@ class AppLocalizationsId extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_ja.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,4 +1696,13 @@ class AppLocalizationsJa extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_nb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,4 +1696,13 @@ class AppLocalizationsNb extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_pt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,15 @@ class AppLocalizationsPt extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}

/// The translations for Portuguese, as used in Brazil (`pt_BR`).
Expand Down
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_ru.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,4 +1696,13 @@ class AppLocalizationsRu extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_uk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,4 +1696,13 @@ class AppLocalizationsUk extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_vi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,4 +1696,13 @@ class AppLocalizationsVi extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}
9 changes: 9 additions & 0 deletions lib/l10n/app_localizations_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,15 @@ class AppLocalizationsZh extends AppLocalizations {

@override
String get shareApp => 'Share App';

@override
String get mlxObjectTemp => 'Object Temperature';

@override
String get mlxAmbientTemp => 'Ambient Temperature';

@override
String get mlx90614Config => 'MLX90614 Configurations';
}

/// The translations for Chinese, using the Han script (`zh_Hant`).
Expand Down
5 changes: 5 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:pslab/l10n/app_localizations.dart';
import 'package:pslab/providers/board_state_provider.dart';
import 'package:pslab/providers/locator.dart';
import 'package:pslab/providers/settings_config_provider.dart';
import 'package:pslab/providers/mlx90614_provider.dart'; // <--- 1. IMPORT ADDED
import 'package:pslab/view/accelerometer_screen.dart';
import 'package:pslab/view/barometer_screen.dart';
import 'package:pslab/view/connect_device_screen.dart';
Expand Down Expand Up @@ -42,6 +43,10 @@ void main() {
ChangeNotifierProvider<BoardStateProvider>(
create: (context) => getIt<BoardStateProvider>(),
),
// <--- 2. PROVIDER ADDED
ChangeNotifierProvider<MLX90614Provider>(
create: (context) => getIt<MLX90614Provider>(),
),
],
child: const MyApp(),
),
Expand Down
4 changes: 4 additions & 0 deletions lib/providers/locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:pslab/communication/science_lab.dart';
import 'package:pslab/communication/socket_client.dart';
import 'package:pslab/others/science_lab_common.dart';
import 'package:pslab/providers/board_state_provider.dart';
import 'package:pslab/providers/mlx90614_provider.dart'; // <--- 1. IMPORT ADDED

final GetIt getIt = GetIt.instance;

Expand All @@ -29,6 +30,9 @@ void setupLocator() {
getIt.registerLazySingleton<ScienceLab>(
() => getIt.get<ScienceLabCommon>().getScienceLab());
getIt.registerLazySingleton<BoardStateProvider>(() => BoardStateProvider());

// <--- 2. REGISTRATION ADDED
getIt.registerLazySingleton<MLX90614Provider>(() => MLX90614Provider());
}

void registerAppLocalizations(AppLocalizations appLocalizations) {
Expand Down
49 changes: 49 additions & 0 deletions lib/providers/mlx90614_provider.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import '../communication/sensors/mlx90614.dart';
import '../communication/peripherals/i2c.dart';

class MLX90614Provider with ChangeNotifier {
MLX90614? _sensor;
bool isWorking = false;

// This sensor provides two values
double ambientTemp = 0.0; // Room temperature
double objectTemp = 0.0; // Target temperature

// Initialize the sensor with the I2C connection
Future<void> init(I2C i2c) async {
_sensor ??= MLX90614(i2c);
}

// Start the loop to read data
Future<void> startDataLog() async {
if (_sensor == null) return;

// Check if loop is already running to prevent duplicates
if (isWorking) return;

isWorking = true;
notifyListeners();

while (isWorking) {
try {
// Read both values safely
ambientTemp = await _sensor!.getAmbientTemperature();
objectTemp = await _sensor!.getObjectTemperature();
notifyListeners();
} catch (e) {
debugPrint("Error reading MLX90614: $e");
}

// Wait 1 second before next read
await Future.delayed(const Duration(milliseconds: 1000));
}
}

// Stop the loop
void stopDataLog() {
isWorking = false;
notifyListeners();
}
}
Loading
Loading