@@ -599,6 +599,305 @@ def irq(self, *args, **kws):
599599 self .__pin .irq (* args , ** kws )
600600
601601
602+ # 202108018 更换SHT20 SPL06传感器
603+ class WEATHER (object ):
604+ def __init__ (self ):
605+ self .i2c = i2c
606+ addr = self .i2c .scan ()
607+ if 118 in addr :
608+ WEATHER .chip = 1 # SPL06-001
609+ WEATHER .IIC_ADDR = 118
610+ elif 119 in addr :
611+ WEATHER .chip = 2 # BME280
612+ WEATHER .IIC_ADDR = 119
613+ else :
614+ raise OSError ("WEATHER init error" )
615+ if (WEATHER .chip == 1 ):
616+ # SPL06
617+ WEATHER ._writeReg (0x06 , 0x01 ) # 压力配置寄存器
618+ WEATHER ._writeReg (0x07 , 0x80 ) # 温度配置寄存器
619+ WEATHER ._writeReg (0x08 , 0x07 ) # 设置连续的压力和温度测量
620+ WEATHER ._writeReg (0x09 , 0x00 )
621+ sleep_ms (50 )
622+ try :
623+ id = WEATHER ._readReg (0x0D )
624+ if (id [0 ]!= 0x10 ):
625+ print ('SPL06 ID error:' ,id )
626+ except OSError as e :
627+ print (e )
628+ # SHT20
629+ self .sht20 = SHT20 ()
630+ elif (WEATHER .chip == 2 ):
631+ self .bme280 = BME280 ()
632+
633+ def _readReg (reg , nbytes = 1 ):
634+ return i2c .readfrom_mem (WEATHER .IIC_ADDR , reg , nbytes )
635+
636+ def _writeReg (reg , value ):
637+ i2c .writeto_mem (WEATHER .IIC_ADDR , reg , value .to_bytes (1 , 'little' ))
638+
639+ def get_traw (self ):
640+ tmp_MSB = WEATHER ._readReg (0x03 )
641+ tmp_LSB = WEATHER ._readReg (0x04 )
642+ tmp_XLSB = WEATHER ._readReg (0x05 )
643+
644+ tmp = (tmp_MSB [0 ] << 16 ) | (tmp_LSB [0 ] << 8 ) | tmp_XLSB [0 ]
645+ if (tmp & (1 << 23 )):
646+ tmp = - ((tmp ^ 0xffffff ) + 1 )
647+
648+ return tmp
649+
650+ def get_temperature_scale_factor (self ):
651+ tmp_Byte = WEATHER ._readReg (0x07 )
652+ tmp_Byte = tmp_Byte [0 ] & 0B111
653+
654+ if (tmp_Byte == 0B000 ):
655+ k = 524288.0
656+ elif (tmp_Byte == 0B001 ):
657+ k = 1572864.0
658+ elif (tmp_Byte == 0B010 ):
659+ k = 3670016.0
660+ elif (tmp_Byte == 0B011 ):
661+ k = 7864320.0
662+ elif (tmp_Byte == 0B100 ):
663+ k = 253952.0
664+ elif (tmp_Byte == 0B101 ):
665+ k = 516096.0
666+ elif (tmp_Byte == 0B110 ):
667+ k = 1040384.0
668+ elif (tmp_Byte == 0B111 ):
669+ k = 2088960.0
670+
671+ return k
672+
673+ def get_pressure_scale_factor (self ):
674+ tmp_Byte = WEATHER ._readReg (0x06 )
675+ tmp_Byte = tmp_Byte [0 ] & 0B111
676+
677+ if (tmp_Byte == 0B000 ):
678+ k = 524288.0
679+ elif (tmp_Byte == 0B001 ):
680+ k = 1572864.0
681+ elif (tmp_Byte == 0B010 ):
682+ k = 3670016.0
683+ elif (tmp_Byte == 0B011 ):
684+ k = 7864320.0
685+ elif (tmp_Byte == 0B100 ):
686+ k = 253952.0
687+ elif (tmp_Byte == 0B101 ):
688+ k = 516096.0
689+ elif (tmp_Byte == 0B110 ):
690+ k = 1040384.0
691+ elif (tmp_Byte == 0B111 ):
692+ k = 2088960.0
693+
694+ return k
695+
696+ def get_praw (self ):
697+ tmp_MSB = WEATHER ._readReg (0x00 )
698+ tmp_LSB = WEATHER ._readReg (0x01 )
699+ tmp_XLSB = WEATHER ._readReg (0x02 )
700+
701+ tmp = ((tmp_MSB [0 ] << 16 ) | (tmp_LSB [0 ] << 8 ) | tmp_XLSB [0 ] )& 0x00ffffff
702+
703+ if (tmp & (1 << 23 )):
704+ tmp = - ((tmp ^ 0xffffff ) + 1 )
705+
706+ return tmp
707+
708+ def get_c0 (self ):
709+ tmp_MSB = WEATHER ._readReg (0x10 )
710+ tmp_LSB = WEATHER ._readReg (0x11 )
711+
712+ tmp_LSB = tmp_LSB [0 ] >> 4
713+ tmp = tmp_MSB [0 ] << 4 | tmp_LSB
714+
715+ if (tmp & (1 << 11 )):
716+ tmp = - ((tmp ^ 0xfff ) + 1 )
717+
718+ return tmp
719+
720+ def get_c1 (self ):
721+ tmp_MSB = WEATHER ._readReg (0x11 )
722+ tmp_LSB = WEATHER ._readReg (0x12 )
723+
724+ tmp = ((tmp_MSB [0 ] & 0xF ) << 8 ) | tmp_LSB [0 ]
725+
726+ if (tmp & (1 << 11 )):
727+ tmp = - ((tmp ^ 0xfff ) + 1 )
728+
729+ return tmp
730+
731+ def get_c00 (self ):
732+ tmp_MSB = WEATHER ._readReg (0x13 )
733+ tmp_LSB = WEATHER ._readReg (0x14 )
734+ tmp_XLSB = WEATHER ._readReg (0x15 )
735+
736+ tmp = ((tmp_MSB [0 ] << 12 ) | (tmp_LSB [0 ] << 4 ) | (tmp_XLSB [0 ] >> 4 ))
737+
738+ if (tmp & (1 << 19 )):
739+ tmp = - ((tmp ^ 0xfffff ) + 1 )
740+
741+ return tmp
742+
743+ def get_c10 (self ):
744+ tmp_MSB = WEATHER ._readReg (0x15 )
745+ tmp_LSB = WEATHER ._readReg (0x16 )
746+ tmp_XLSB = WEATHER ._readReg (0x17 )
747+
748+ tmp_MSB = tmp_MSB [0 ] & 0x0F
749+
750+ tmp = (tmp_MSB << 16 ) | (tmp_LSB [0 ] << 8 ) | tmp_XLSB [0 ]
751+
752+ if (tmp & (1 << 19 )):
753+ tmp = - ((tmp ^ 0xFFFFF ) + 1 )
754+
755+ return tmp
756+
757+ def get_c01 (self ):
758+ tmp_MSB = WEATHER ._readReg (0x18 )
759+ tmp_LSB = WEATHER ._readReg (0x19 )
760+
761+ tmp = (tmp_MSB [0 ] << 8 ) | tmp_LSB [0 ]
762+
763+ if (tmp & (1 << 15 )):
764+ tmp = - ((tmp ^ 0xFFFF ) + 1 )
765+
766+ return tmp
767+
768+ def get_c11 (self ):
769+ tmp_MSB = WEATHER ._readReg (0x1A )
770+ tmp_LSB = WEATHER ._readReg (0x1B )
771+
772+ tmp = (tmp_MSB [0 ] << 8 ) | tmp_LSB [0 ]
773+
774+ if (tmp & (1 << 15 )):
775+ tmp = - ((tmp ^ 0xFFFF ) + 1 )
776+
777+ return tmp
778+
779+ def get_c20 (self ):
780+ tmp_MSB = WEATHER ._readReg (0x1C )
781+ tmp_LSB = WEATHER ._readReg (0x1D )
782+
783+ tmp = (tmp_MSB [0 ] << 8 ) | tmp_LSB [0 ]
784+
785+ if (tmp & (1 << 15 )):
786+ tmp = - ((tmp ^ 0xFFFF ) + 1 )
787+
788+ return tmp
789+
790+ def get_c21 (self ):
791+ tmp_MSB = WEATHER ._readReg (0x1E )
792+ tmp_LSB = WEATHER ._readReg (0x1F )
793+ tmp = (tmp_MSB [0 ] << 8 ) | tmp_LSB [0 ]
794+
795+ if (tmp & (1 << 15 )):
796+ tmp = - ((tmp ^ 0xFFFF ) + 1 )
797+
798+ return tmp
799+
800+ def get_c30 (self ):
801+ tmp_MSB = WEATHER ._readReg (0x20 )
802+ tmp_LSB = WEATHER ._readReg (0x21 )
803+
804+ tmp = (tmp_MSB [0 ] << 8 ) | tmp_LSB [0 ]
805+
806+ if (tmp & (1 << 15 )):
807+ tmp = - ((tmp ^ 0xFFFF ) + 1 )
808+
809+ return tmp
810+
811+ def get_temperature (self ):
812+ """
813+ 获取温度
814+ :return: 温度,单位摄氏度
815+ """
816+ c0 = self .get_c0 ()
817+ c1 = self .get_c1 ()
818+ traw = self .get_traw ()
819+ t_scale = self .get_temperature_scale_factor ()
820+
821+ traw_sc = traw / t_scale
822+ temp_c = ((c0 ) * 0.5 ) + ((c1 ) * traw_sc )
823+ temp_f = (temp_c * 9 / 5 ) + 32
824+
825+ return temp_c
826+
827+ def pressure (self ):
828+ """
829+ 获取气压
830+ :return: 气压,单位Pa
831+ """
832+ if (WEATHER .chip == 1 ):
833+ traw = self .get_traw ()
834+ t_scale = self .get_temperature_scale_factor ()
835+ traw_sc = traw / t_scale
836+
837+ praw = self .get_praw ()
838+ p_scale = self .get_pressure_scale_factor ()
839+ praw_sc = praw / p_scale
840+
841+ pcomp = self .get_c00 ()+ praw_sc * (self .get_c10 ()+ praw_sc * (self .get_c20 ()+ praw_sc * self .get_c30 ())) + traw_sc * self .get_c01 () + traw_sc * praw_sc * (self .get_c11 ()+ praw_sc * self .get_c21 ())
842+ # print("pcomp", "{:.2f}".format(pcomp))
843+ elif (WEATHER .chip == 2 ):
844+ pcomp = self .bme280 .pressure ()
845+
846+ return pcomp
847+
848+ def temperature (self ):
849+ """
850+ 获取温度
851+ :return: 温度,单位摄氏度
852+ """
853+ if (WEATHER .chip == 1 ):
854+ return self .sht20 .temperature ()
855+ elif (WEATHER .chip == 2 ):
856+ return self .bme280 .temperature ()
857+
858+ def humidity (self ):
859+ """
860+ 获取湿度
861+ :return: 湿度,单位%
862+ """
863+ if (WEATHER .chip == 1 ):
864+ return self .sht20 .humidity ()
865+ elif (WEATHER .chip == 2 ):
866+ return self .bme280 .humidity ()
867+
868+
869+ class SHT20 (object ):
870+ """
871+ 温湿度模块SHT20控制类
872+ :param i2c: I2C实例对象,默认i2c=i2c.
873+ """
874+
875+ def __init__ (self , i2c = i2c ):
876+ self .i2c = i2c
877+
878+ def temperature (self ):
879+ """
880+ 获取温度
881+
882+ :return: 温度,单位摄氏度
883+ """
884+ self .i2c .writeto (0x40 , b'\xf3 ' )
885+ sleep_ms (70 )
886+ t = i2c .readfrom (0x40 , 2 )
887+ return - 46.86 + 175.72 * (t [0 ] * 256 + t [1 ]) / 65535
888+
889+ def humidity (self ):
890+ """
891+ 获取湿度
892+
893+ :return: 湿度,单位%
894+ """
895+ self .i2c .writeto (0x40 , b'\xf5 ' )
896+ sleep_ms (25 )
897+ t = i2c .readfrom (0x40 , 2 )
898+ return - 6 + 125 * (t [0 ] * 256 + t [1 ]) / 65535
899+
900+
602901from gui import *
603902
604903def numberMap (inputNum , bMin , bMax , cMin , cMax ):
@@ -617,8 +916,9 @@ def numberMap(inputNum, bMin, bMax, cMin, cMax):
617916accelerometer = motion .accelerometer
618917gyroscope = motion .gyroscope
619918
620- # bm280
621- bme280 = BME280 ()
919+ #气象传感器
920+ #SHT20 SPL06 bme280
921+ weather = WEATHER ()
622922
623923# rgb matrix
624924rgb = NeoPixel (Pin (25 , Pin .OUT ), 25 , 3 , 1 )
0 commit comments