44import time
55
66import ads1015
7- import RPi .GPIO as GPIO
7+ import gpiod
8+ import gpiodevice
9+ from gpiod .line import Direction , Value
810
9- MICS6814_HEATER_PIN = 24
1011MICS6814_GAIN = 6.144
1112
13+ OUTH = gpiod .LineSettings (direction = Direction .OUTPUT , output_value = Value .ACTIVE )
14+ PLATFORMS = {
15+ "Radxa ROCK 5B" : {"heater" : ("PIN_18" , OUTH )},
16+ "Raspberry Pi 5" : {"heater" : ("PIN18" , OUTH )},
17+ "Raspberry Pi 4" : {"heater" : ("GPIO24" , OUTH )}
18+ }
19+
1220ads1015 .I2C_ADDRESS_DEFAULT = ads1015 .I2C_ADDRESS_ALTERNATE
1321_is_setup = False
1422_is_available = False
1523_adc_enabled = False
1624_adc_gain = 6.148
25+ _heater = None
1726
1827
1928class Mics6814Reading (object ):
20- __slots__ = ' oxidising' , ' reducing' , ' nh3' , ' adc'
29+ __slots__ = " oxidising" , " reducing" , " nh3" , " adc"
2130
2231 def __init__ (self , ox , red , nh3 , adc = None ):
2332 self .oxidising = ox
@@ -26,24 +35,20 @@ def __init__(self, ox, red, nh3, adc=None):
2635 self .adc = adc
2736
2837 def __repr__ (self ):
29- fmt = """Oxidising: {ox :05.02f} Ohms
30- Reducing: {red :05.02f} Ohms
31- NH3: {nh3:05.02f} Ohms"""
38+ fmt = f """Oxidising: { self . oxidising :05.02f} Ohms
39+ Reducing: { self . reducing :05.02f} Ohms
40+ NH3: { self . nh3 :05.02f} Ohms"""
3241 if self .adc is not None :
33- fmt += """
34- ADC: {adc:05.02f} Volts
42+ fmt += f """
43+ ADC: { self . adc :05.02f} Volts
3544"""
36- return fmt .format (
37- ox = self .oxidising ,
38- red = self .reducing ,
39- nh3 = self .nh3 ,
40- adc = self .adc )
45+ return fmt
4146
4247 __str__ = __repr__
4348
4449
4550def setup ():
46- global adc , adc_type , _is_setup , _is_available
51+ global adc , adc_type , _is_setup , _is_available , _heater
4752 if _is_setup :
4853 return
4954 _is_setup = True
@@ -56,17 +61,15 @@ def setup():
5661 _is_available = False
5762 return
5863
59- adc .set_mode (' single' )
64+ adc .set_mode (" single" )
6065 adc .set_programmable_gain (MICS6814_GAIN )
61- if adc_type == ' ADS1115' :
66+ if adc_type == " ADS1115" :
6267 adc .set_sample_rate (128 )
6368 else :
6469 adc .set_sample_rate (1600 )
6570
66- GPIO .setwarnings (False )
67- GPIO .setmode (GPIO .BCM )
68- GPIO .setup (MICS6814_HEATER_PIN , GPIO .OUT )
69- GPIO .output (MICS6814_HEATER_PIN , 1 )
71+ _heater = gpiodevice .get_pins_for_platform (PLATFORMS )[0 ]
72+
7073 atexit .register (cleanup )
7174
7275
@@ -88,7 +91,10 @@ def set_adc_gain(value):
8891
8992
9093def cleanup ():
91- GPIO .output (MICS6814_HEATER_PIN , 0 )
94+ if _heater is None :
95+ return
96+ lines , offset = _heater
97+ lines .set_value (offset , Value .INACTIVE )
9298
9399
94100def read_all ():
@@ -98,9 +104,9 @@ def read_all():
98104 if not _is_available :
99105 raise RuntimeError ("Gas sensor not connected." )
100106
101- ox = adc .get_voltage (' in0/gnd' )
102- red = adc .get_voltage (' in1/gnd' )
103- nh3 = adc .get_voltage (' in2/gnd' )
107+ ox = adc .get_voltage (" in0/gnd" )
108+ red = adc .get_voltage (" in1/gnd" )
109+ nh3 = adc .get_voltage (" in2/gnd" )
104110
105111 try :
106112 ox = (ox * 56000 ) / (3.3 - ox )
@@ -121,11 +127,11 @@ def read_all():
121127
122128 if _adc_enabled :
123129 if _adc_gain == MICS6814_GAIN :
124- analog = adc .get_voltage (' ref/gnd' )
130+ analog = adc .get_voltage (" ref/gnd" )
125131 else :
126132 adc .set_programmable_gain (_adc_gain )
127133 time .sleep (0.05 )
128- analog = adc .get_voltage (' ref/gnd' )
134+ analog = adc .get_voltage (" ref/gnd" )
129135 adc .set_programmable_gain (MICS6814_GAIN )
130136
131137 return Mics6814Reading (ox , red , nh3 , analog )
0 commit comments