-
Notifications
You must be signed in to change notification settings - Fork 827
feat: Add support for sensor MLX90614 #3025
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
Open
Inuth0603
wants to merge
3
commits into
fossasia:flutter
Choose a base branch
from
Inuth0603:feat/mlx90614-sensor
base: flutter
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| 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 { | ||
| // FIX: Use readBulk instead of read. | ||
| // Arguments: (Device Address, Register Address, Bytes to Read) | ||
| 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 | ||
| int rawValue = (msb << 8) | lsb; | ||
|
|
||
| // 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:provider/provider.dart'; | ||
| import 'package:pslab/communication/science_lab.dart'; | ||
| import 'package:pslab/communication/peripherals/i2c.dart'; | ||
| import 'package:pslab/providers/locator.dart'; | ||
| import '../providers/mlx90614_provider.dart'; | ||
|
|
||
| class MLX90614Screen extends StatefulWidget { | ||
| const MLX90614Screen({super.key}); | ||
|
|
||
| @override | ||
| State<MLX90614Screen> createState() => _MLX90614ScreenState(); | ||
| } | ||
|
|
||
| class _MLX90614ScreenState extends State<MLX90614Screen> { | ||
| @override | ||
| void initState() { | ||
| super.initState(); | ||
| WidgetsBinding.instance.addPostFrameCallback((_) { | ||
| if (!mounted) return; | ||
|
|
||
| final provider = Provider.of<MLX90614Provider>(context, listen: false); | ||
|
|
||
| // Get the ScienceLab instance via the locator | ||
| final scienceLab = getIt<ScienceLab>(); | ||
|
|
||
| if (scienceLab.isConnected()) { | ||
| // Create I2C helper and init | ||
| I2C i2c = I2C(scienceLab.mPacketHandler); | ||
| provider.init(i2c); | ||
| provider.startDataLog(); | ||
| } else { | ||
| ScaffoldMessenger.of(context).showSnackBar( | ||
| const SnackBar(content: Text('Device not connected')), | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @override | ||
| void dispose() { | ||
| // Stop the data loop when leaving the screen | ||
| if (mounted) { | ||
| Provider.of<MLX90614Provider>(context, listen: false).stopDataLog(); | ||
| } | ||
| super.dispose(); | ||
| } | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return Scaffold( | ||
| appBar: AppBar( | ||
| title: const Text('MLX90614 Sensor'), | ||
| ), | ||
| body: Consumer<MLX90614Provider>( | ||
| builder: (context, provider, child) { | ||
| return Padding( | ||
| padding: const EdgeInsets.all(16.0), | ||
| child: Column( | ||
| children: [ | ||
| // Card 1: Object Temperature (What you are pointing at) | ||
| _buildSensorCard( | ||
| title: "Object Temperature", | ||
| value: provider.objectTemp.toStringAsFixed(2), | ||
| unit: "°C", | ||
| icon: Icons.thermostat_auto, // Icon representing target | ||
| color: Colors.deepOrangeAccent, | ||
| ), | ||
| const SizedBox(height: 20), | ||
|
|
||
| // Card 2: Ambient Temperature (Room temp) | ||
| _buildSensorCard( | ||
| title: "Ambient Temperature", | ||
| value: provider.ambientTemp.toStringAsFixed(2), | ||
| unit: "°C", | ||
| icon: Icons.home_mini, // Icon representing environment | ||
| color: Colors.blueGrey, | ||
| ), | ||
| ], | ||
| ), | ||
| ); | ||
| }, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| Widget _buildSensorCard({ | ||
| required String title, | ||
| required String value, | ||
| required String unit, | ||
| required IconData icon, | ||
| required Color color, | ||
| }) { | ||
| return Card( | ||
| elevation: 4, | ||
| child: Padding( | ||
| padding: const EdgeInsets.all(20.0), | ||
| child: Row( | ||
| children: [ | ||
| Icon(icon, size: 40, color: color), | ||
| const SizedBox(width: 20), | ||
| Expanded( | ||
| child: Column( | ||
| crossAxisAlignment: CrossAxisAlignment.start, | ||
| children: [ | ||
| Text(title, | ||
| style: const TextStyle(fontSize: 16, color: Colors.grey)), | ||
| Row( | ||
| children: [ | ||
| Text(value, | ||
| style: const TextStyle( | ||
| fontSize: 32, fontWeight: FontWeight.bold)), | ||
| const SizedBox(width: 5), | ||
| Text(unit, style: const TextStyle(fontSize: 20)), | ||
| ], | ||
| ), | ||
| ], | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.