|
| 1 | +import 'dart:async'; |
| 2 | +import 'package:pslab/communication/peripherals/i2c.dart'; |
| 3 | +import 'package:pslab/communication/science_lab.dart'; |
| 4 | +import 'package:pslab/others/logger_service.dart'; |
| 5 | + |
| 6 | +class CCS811 { |
| 7 | + static const String tag = "CCS811"; |
| 8 | + static const int address = 0x5A; |
| 9 | + |
| 10 | + static const int algResultData = 0x02; |
| 11 | + static const int hwId = 0x20; |
| 12 | + static const int fwBootVersion = 0x23; |
| 13 | + static const int fwAppVersion = 0x24; |
| 14 | + static const int measMode = 0x01; |
| 15 | + static const int hwVersion = 0x21; |
| 16 | + static const int appStart = 0xF4; |
| 17 | + |
| 18 | + static const int driveMode1Sec = 0x01; |
| 19 | + |
| 20 | + final I2C i2c; |
| 21 | + |
| 22 | + CCS811._(this.i2c); |
| 23 | + |
| 24 | + static Future<CCS811> create(I2C i2c, ScienceLab scienceLab) async { |
| 25 | + final ccs811 = CCS811._(i2c); |
| 26 | + if (scienceLab.isConnected()) { |
| 27 | + await ccs811._initialize(); |
| 28 | + } |
| 29 | + return ccs811; |
| 30 | + } |
| 31 | + |
| 32 | + Future<void> _initialize() async { |
| 33 | + try { |
| 34 | + await _fetchID(); |
| 35 | + await _appStart(); |
| 36 | + await Future.delayed(const Duration(milliseconds: 100)); |
| 37 | + await _disableInterrupt(); |
| 38 | + await _setMeasMode(); |
| 39 | + } catch (e) { |
| 40 | + logger.e("$tag Error initializing: $e"); |
| 41 | + rethrow; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + Future<void> _setMeasMode() async { |
| 46 | + int config = (1 << 2) | (driveMode1Sec << 4); |
| 47 | + await i2c.write(address, [config], measMode); |
| 48 | + } |
| 49 | + |
| 50 | + Future<void> _disableInterrupt() async { |
| 51 | + int config = (1 << 2) | (3 << 4); |
| 52 | + await i2c.write(address, [config], measMode); |
| 53 | + } |
| 54 | + |
| 55 | + Future<void> _fetchID() async { |
| 56 | + try { |
| 57 | + List<int> hwIdData = await i2c.readBulk(address, hwId, 1); |
| 58 | + await Future.delayed(const Duration(milliseconds: 20)); |
| 59 | + |
| 60 | + List<int> hwVerData = await i2c.readBulk(address, hwVersion, 1); |
| 61 | + await Future.delayed(const Duration(milliseconds: 20)); |
| 62 | + |
| 63 | + List<int> bootVerData = await i2c.readBulk(address, fwBootVersion, 2); |
| 64 | + await Future.delayed(const Duration(milliseconds: 20)); |
| 65 | + |
| 66 | + List<int> appVerData = await i2c.readBulk(address, fwAppVersion, 2); |
| 67 | + await Future.delayed(const Duration(milliseconds: 20)); |
| 68 | + |
| 69 | + if (hwIdData.isNotEmpty) { |
| 70 | + logger.d("$tag Hardware ID: ${hwIdData[0] & 0xFF}"); |
| 71 | + } |
| 72 | + if (hwVerData.isNotEmpty) { |
| 73 | + logger.d("$tag Hardware Version: ${hwVerData[0] & 0xFF}"); |
| 74 | + } |
| 75 | + if (bootVerData.length >= 2) { |
| 76 | + logger |
| 77 | + .d("$tag Boot Version: ${(bootVerData[0] << 8) | bootVerData[1]}"); |
| 78 | + } |
| 79 | + if (appVerData.length >= 2) { |
| 80 | + logger.d("$tag App Version: ${(appVerData[0] << 8) | appVerData[1]}"); |
| 81 | + } |
| 82 | + } catch (e) { |
| 83 | + logger.e("$tag Error fetching IDs: $e"); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + Future<void> _appStart() async { |
| 88 | + await i2c.write(address, [], appStart); |
| 89 | + } |
| 90 | + |
| 91 | + String _decodeError(int error) { |
| 92 | + String e = ""; |
| 93 | + if ((error & 1) > 0) e += ", Invalid register address ID"; |
| 94 | + if ((error & (1 << 1)) > 0) e += ", Invalid mailbox ID"; |
| 95 | + if ((error & (1 << 2)) > 0) e += ", Unsupported mode to MEAS_MODE"; |
| 96 | + if ((error & (1 << 3)) > 0) { |
| 97 | + e += ", Resistance measurement max range reached"; |
| 98 | + } |
| 99 | + if ((error & (1 << 4)) > 0) e += ", Heater current out of range"; |
| 100 | + if ((error & (1 << 5)) > 0) e += ", Heater voltage applied incorrectly"; |
| 101 | + |
| 102 | + return e.isNotEmpty ? "Error: ${e.substring(2)}" : "Unknown error"; |
| 103 | + } |
| 104 | + |
| 105 | + Future<Map<String, int>> getRawData() async { |
| 106 | + try { |
| 107 | + List<int> data = await i2c.readBulk(address, algResultData, 8); |
| 108 | + if (data.length < 8) { |
| 109 | + throw Exception("Expected 8 bytes but got ${data.length}"); |
| 110 | + } |
| 111 | + |
| 112 | + int eCO2 = ((data[0] & 0xFF) << 8) | (data[1] & 0xFF); |
| 113 | + int tvoc = ((data[2] & 0xFF) << 8) | (data[3] & 0xFF); |
| 114 | + int errorId = data[5] & 0xFF; |
| 115 | + |
| 116 | + if (errorId > 0) { |
| 117 | + logger.e("$tag ${_decodeError(errorId)}"); |
| 118 | + } |
| 119 | + |
| 120 | + return { |
| 121 | + 'eCO2': eCO2, |
| 122 | + 'TVOC': tvoc, |
| 123 | + }; |
| 124 | + } catch (e) { |
| 125 | + logger.e("$tag Error getting raw data: $e"); |
| 126 | + rethrow; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments