|
| 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 TSL2561 { |
| 7 | + static const String tag = "TSL2561"; |
| 8 | + |
| 9 | + static const int commandBit = 0x80; |
| 10 | + static const int wordBit = 0x20; |
| 11 | + |
| 12 | + static const int controlPowerOn = 0x03; |
| 13 | + static const int controlPowerOff = 0x00; |
| 14 | + |
| 15 | + static const int registerControl = 0x00; |
| 16 | + static const int registerTiming = 0x01; |
| 17 | + static const int registerId = 0x0A; |
| 18 | + static const int registerChan0Low = 0x0C; |
| 19 | + static const int registerChan1Low = 0x0E; |
| 20 | + |
| 21 | + static const int integrationTime13ms = 0x00; |
| 22 | + static const int integrationTime101ms = 0x01; |
| 23 | + static const int integrationTime402ms = 0x02; |
| 24 | + |
| 25 | + static const int gain0x = 0x00; |
| 26 | + static const int gain16x = 0x10; |
| 27 | + |
| 28 | + static const List<int> addresses = [0x39, 0x29, 0x49]; |
| 29 | + |
| 30 | + final I2C i2c; |
| 31 | + int address = 0x39; |
| 32 | + int timing = integrationTime13ms; |
| 33 | + int gain = gain16x; |
| 34 | + |
| 35 | + TSL2561(this.i2c, ScienceLab scienceLab) { |
| 36 | + () async { |
| 37 | + if (scienceLab.isConnected()) { |
| 38 | + for (final addr in addresses) { |
| 39 | + address = addr; |
| 40 | + await disable(); |
| 41 | + logger.d("$tag: Checking address 0x${address.toRadixString(16)}"); |
| 42 | + try { |
| 43 | + int id = await i2c.readByte(address, registerId); |
| 44 | + if (id != 0xffffffff && (id & 0x0A) == 0x0A) { |
| 45 | + logger.d("$tag: TSL2561 found!"); |
| 46 | + break; |
| 47 | + } else { |
| 48 | + logger.d("$tag: TSL2561 not found."); |
| 49 | + } |
| 50 | + } catch (e) { |
| 51 | + logger.e("$tag: Error reading ID: $e"); |
| 52 | + } |
| 53 | + } |
| 54 | + await enable(); |
| 55 | + await _wait(); |
| 56 | + await i2c |
| 57 | + .writeBulk(address, [commandBit | registerTiming, timing | gain]); |
| 58 | + } |
| 59 | + }(); |
| 60 | + } |
| 61 | + |
| 62 | + Future<int> getID() async { |
| 63 | + try { |
| 64 | + List<int> idList = await i2c.readBulk(address, registerId, 1); |
| 65 | + if (idList.isEmpty) return -1; |
| 66 | + int id = int.parse(idList[0].toRadixString(16), radix: 16); |
| 67 | + logger.d("$tag: ID: $id"); |
| 68 | + return id; |
| 69 | + } catch (e) { |
| 70 | + logger.e("$tag: Error getting ID: $e"); |
| 71 | + rethrow; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + Future<double?> getRaw() async { |
| 76 | + try { |
| 77 | + List<int> infraList = await i2c.readBulk( |
| 78 | + address, commandBit | wordBit | registerChan1Low, 2); |
| 79 | + List<int> fullList = await i2c.readBulk( |
| 80 | + address, commandBit | wordBit | registerChan0Low, 2); |
| 81 | + |
| 82 | + if (infraList.isNotEmpty && fullList.isNotEmpty) { |
| 83 | + int full = ((fullList[1] & 0xff) << 8) | (fullList[0] & 0xff); |
| 84 | + int infra = ((infraList[1] & 0xff) << 8) | (infraList[0] & 0xff); |
| 85 | + return (full - infra).toDouble(); |
| 86 | + } else { |
| 87 | + return 0.0; |
| 88 | + } |
| 89 | + } catch (e) { |
| 90 | + logger.e("$tag: Error reading raw values: $e"); |
| 91 | + return 0.0; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + Future<void> setGain(int gainValue) async { |
| 96 | + switch (gainValue) { |
| 97 | + case 1: |
| 98 | + gain = gain0x; |
| 99 | + break; |
| 100 | + case 16: |
| 101 | + gain = gain16x; |
| 102 | + break; |
| 103 | + default: |
| 104 | + gain = gain16x; |
| 105 | + } |
| 106 | + await i2c.writeBulk(address, [commandBit | registerTiming, gain | timing]); |
| 107 | + } |
| 108 | + |
| 109 | + Future<void> enable() async { |
| 110 | + await i2c |
| 111 | + .writeBulk(address, [commandBit | registerControl, controlPowerOn]); |
| 112 | + } |
| 113 | + |
| 114 | + Future<void> disable() async { |
| 115 | + await i2c |
| 116 | + .writeBulk(address, [commandBit | registerControl, controlPowerOff]); |
| 117 | + } |
| 118 | + |
| 119 | + Future<void> _wait() async { |
| 120 | + switch (timing) { |
| 121 | + case integrationTime13ms: |
| 122 | + await Future.delayed(const Duration(milliseconds: 14)); |
| 123 | + break; |
| 124 | + case integrationTime101ms: |
| 125 | + await Future.delayed(const Duration(milliseconds: 102)); |
| 126 | + break; |
| 127 | + case integrationTime402ms: |
| 128 | + default: |
| 129 | + await Future.delayed(const Duration(milliseconds: 403)); |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments