1+ from machine import I2C
2+ from .pahub import PAHUBUnit
3+ from .unit_helper import UnitError
4+ import struct
5+ import sys
6+
7+ if sys .platform != "esp32" :
8+ from typing import Union
9+
10+ import time
11+ import struct
12+
13+ KMETER_ADDR = 0x66
14+
15+ TEMP_CELSIUS_REG = 0x00
16+ TEMP_FAHREN_REG = 0x04
17+ TEMP_INT_CELSIUS_REG = 0x10
18+ TEMP_INT_FAHREN_REG = 0x14
19+ TEMP_READY_REG = 0x20
20+ TEMP_STR_CELSIUS_REG = 0x30
21+ TEMP_STR_FAHREN_REG = 0x40
22+ TEMP_INTSTR_CELSIUS_REG = 0x50
23+ TEMP_INTSTR_FAHREN_REG = 0x60
24+ FIRM_VER_REG = 0xFE
25+ I2C_ADDR_REG = 0xFF
26+
27+
28+ class KMETER_ISOUnit :
29+ def __init__ (self , i2c : Union [I2C , PAHUBUnit ], addr = KMETER_ADDR ):
30+ """! Kmeter Initialize Function
31+ addr: 1 ~ 127
32+ """
33+ self .kmeter_i2c = i2c
34+ if addr >= 0x01 and addr <= 0x7F :
35+ self .unit_addr = addr
36+ if not (self .unit_addr in self .kmeter_i2c .scan ()):
37+ raise UnitError ("Kmeter iso unit maybe not connect" )
38+
39+ def get_kmeter_thermo (self , temp = 0 ):
40+ """! get thermocouple temperature value."""
41+ buff = self .kmeter_i2c .readfrom_mem (self .unit_addr , TEMP_CELSIUS_REG + (4 * temp ), 4 )
42+ return round ((self .int_convert (buff )/ 100 ), 2 )
43+
44+ def get_kmeter_internal (self , temp = 0 ):
45+ """! get internal temperature value."""
46+ buff = self .kmeter_i2c .readfrom_mem (self .unit_addr , TEMP_INT_CELSIUS_REG + (4 * temp ), 4 )
47+ return round (self .int_convert (buff )/ 100 , 2 )
48+
49+ @property
50+ def get_data_available_status (self ):
51+ """! get temperature measurement value is ready? """
52+ status = self .kmeter_i2c .readfrom_mem (self .unit_addr , TEMP_READY_REG , 1 )[0 ]
53+ return False if status else True
54+
55+ def get_kmeter_thermo_string (self , temp = 0 ):
56+ """! get thermocouple temperature string value."""
57+ return self .kmeter_i2c .readfrom_mem (self .unit_addr , TEMP_STR_CELSIUS_REG + (temp * 0x10 ), 1 ).decode () + str (float (self .kmeter_i2c .readfrom_mem (self .unit_addr , TEMP_STR_CELSIUS_REG + (temp * 0x10 ), 8 ).decode ()))
58+
59+ def get_kmeter_internal_string (self , temp = 0 ):
60+ """! get internal temperature string value."""
61+ return self .kmeter_i2c .readfrom_mem (self .unit_addr , TEMP_INTSTR_CELSIUS_REG + (temp * 0x10 ), 1 ).decode ()+ str (float (self .kmeter_i2c .readfrom_mem (self .unit_addr , TEMP_INTSTR_CELSIUS_REG + (temp * 0x10 ), 8 ).decode ()))
62+
63+ def get_device_spec (self , mode ) -> int :
64+ """! Get device firmware version and i2c address.
65+ mode: 0xFE and 0xFF
66+ """
67+ if mode >= FIRM_VER_REG and mode <= I2C_ADDR_REG :
68+ return self .kmeter_i2c .readfrom_mem (self .unit_addr , mode , 1 )[0 ]
69+
70+ def set_i2c_address (self , addr ) -> None :
71+ """! Set i2c address.
72+ addr: 1 to 127
73+ """
74+ if addr >= 0x01 and addr <= 0x7F :
75+ if addr != self .unit_addr :
76+ self .kmeter_i2c .writeto_mem (self .unit_addr , I2C_ADDR_REG , struct .pack ('b' , addr ))
77+ self .unit_addr = addr
78+ time .sleep_ms (200 )
79+
80+ def int_convert (self , value ):
81+ return struct .unpack ('<i' , value )[0 ]
82+
0 commit comments