Skip to content

Commit 744bd55

Browse files
committed
fix/unit/miniscale: code format
Signed-off-by: lbuque <[email protected]>
1 parent 5464f29 commit 744bd55

File tree

2 files changed

+39
-48
lines changed

2 files changed

+39
-48
lines changed

m5stack/libs/unit/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"LoRaE220JPUnit": "lora_e220_jp",
4040
"WEIGHTUnit": "weight",
4141
"SCALESUnit": "scales",
42-
"MiniScale": "miniscale",
42+
"MiniScaleUnit": "miniscale",
4343
}
4444

4545

m5stack/libs/unit/miniscale.py

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,59 @@
11
# -*- encoding: utf-8 -*-
2-
'''
2+
"""
33
@File : _miniscale.py
44
@Time : 2023/12/19
55
@Author : TONG YIHAN
66
77
@License : (C)Copyright 2015-2023, M5STACK
88
@Desc : This sensor is capable of measuring weight and also includes additional functionalities like LED control and various filters.
9-
'''
9+
"""
1010

1111
# Import necessary libraries
1212
from machine import I2C
1313
from .pahub import PAHUBUnit
1414
from .unit_helper import UnitError
15-
import time
1615
import struct
17-
try:
16+
import sys
17+
18+
if sys.platform != "esp32":
1819
from typing import Union
19-
except ImportError:
20-
pass
21-
22-
class Miniscale:
23-
"""! MiniScale is a weight sensor, includes a hx711 22bit ADC.
24-
25-
"""
26-
def __init__(self, i2c: Union[I2C, PAHUBUnit], addr=0x26):
20+
21+
22+
class MiniScaleUnit:
23+
"""! MiniScale is a weight sensor, includes a hx711 22bit ADC."""
24+
25+
def __init__(self, i2c: Union[I2C, PAHUBUnit], addr=0x26):
2726
self.i2c = i2c
2827
self.addr = addr
2928
self._available()
3029

3130
def _available(self):
3231
"""! Check if sensor is available on the I2C bus.
33-
32+
3433
Raises:
3534
UnitError: If the sensor is not found.
3635
"""
3736
if not (self.i2c_addr in self.i2c.scan()):
3837
raise UnitError("MiniScale Unit not found.")
39-
38+
4039
@property
4140
def adc(self) -> int:
4241
"""! Get the ADC value."""
4342
data = self.i2c.readfrom_mem(self.addr, 0x00, 4)
44-
return struct.unpack('<I', data)[0]
45-
43+
return struct.unpack("<I", data)[0]
44+
4645
@property
4746
def weight(self) -> float:
4847
"""! Get the weight in grams."""
4948
data = self.i2c.readfrom_mem(self.addr, 0x10, 4)
50-
return struct.unpack('<f', data)[0]
51-
49+
return struct.unpack("<f", data)[0]
50+
5251
@property
5352
def button(self) -> bool:
5453
"""! Get the button state."""
5554
data = self.i2c.readfrom_mem(self.addr, 0x20, 1)
56-
return struct.unpack('b', data)[0] == 0
57-
55+
return struct.unpack("b", data)[0] == 0
56+
5857
def setLed(self, r: int, g: int, b: int):
5958
"""! Set the RGB LED color.
6059
@@ -65,10 +64,8 @@ def setLed(self, r: int, g: int, b: int):
6564
self.i2c.writeto_mem(self.addr, 0x30, bytes([r, g, b]))
6665

6766
def reset(self):
68-
"""! Reset weight value to zero
69-
70-
"""
71-
self.i2c.writeto_mem(self.addr, 0x50, b'\x01')
67+
"""! Reset weight value to zero"""
68+
self.i2c.writeto_mem(self.addr, 0x50, b"\x01")
7269

7370
def calibration(self, weight1_g: float, weight1_adc: int, weight2_g: float, weight2_adc: int):
7471
"""! Calibration the MiniScale sensor.
@@ -78,63 +75,57 @@ def calibration(self, weight1_g: float, weight1_adc: int, weight2_g: float, weig
7875
(3) step 3: Put 100g weight on it, and get RawADC, this is RawADC_100g
7976
(4) step 4: Calculate the value of GAP = (RawADC_100g-RawADC0g) / 100
8077
(5) step 5: Write GAP value to the unit Via I2C
81-
78+
8279
@param weight1_g Weight1 in grams.
8380
@param weight1_adc Weight1 in ADC.
8481
@param weight2_g Weight2 in grams.
8582
@param weight2_adc Weight2 in ADC.
8683
"""
87-
84+
8885
gap = (weight2_adc - weight1_adc) / (weight2_g - weight1_g)
89-
self.i2c.writeto_mem(self.addr, 0x40, struct.pack('<f', gap))
86+
self.i2c.writeto_mem(self.addr, 0x40, struct.pack("<f", gap))
9087

9188
def setLowPassFilter(self, enabled: bool):
9289
"""! Set low pass filter enabled or not
93-
90+
9491
@param enabled Enable filter or not
9592
"""
9693
if enabled:
97-
self.i2c.writeto_mem(self.addr, 0x80, b'\x01')
94+
self.i2c.writeto_mem(self.addr, 0x80, b"\x01")
9895
else:
99-
self.i2c.writeto_mem(self.addr, 0x80, b'\x00')
96+
self.i2c.writeto_mem(self.addr, 0x80, b"\x00")
10097

10198
def getLowPassFilter(self) -> bool:
102-
"""! Get low pass filter enabled or not
103-
104-
"""
99+
"""! Get low pass filter enabled or not"""
105100
data = self.i2c.readfrom_mem(self.addr, 0x80, 1)
106-
return data == b'\x01'
101+
return data == b"\x01"
107102

108103
def setAverageFilterLevel(self, level: int):
109104
"""! Set average filter level
110-
105+
111106
@param level level of average filter, larger value for smoother result but more latency
112107
"""
113108
if level > 50 or level < 0:
114109
raise Exception("Level for average filter must between 0 ~ 50")
115110

116-
self.i2c.writeto_mem(self.addr, 0x81, struct.pack('b', level))
117-
111+
self.i2c.writeto_mem(self.addr, 0x81, struct.pack("b", level))
112+
118113
def getAverageFilterLevel(self) -> int:
119-
"""! Get average filter level
120-
121-
"""
114+
"""! Get average filter level"""
122115
data = self.i2c.readfrom_mem(self.addr, 0x81, 1)
123-
return struct.unpack('b', data)[0]
116+
return struct.unpack("b", data)[0]
124117

125118
def setEMAFilterAlpha(self, alpha: int):
126119
"""! Set ema filter alpha
127-
120+
128121
@param alpha alpha of ema filter, smaller value for smoother result but more latency
129122
"""
130123
if alpha > 99 or alpha < 0:
131124
raise Exception("Alpha for EMA filter must between 0 ~ 99")
132125

133-
self.i2c.writeto_mem(self.addr, 0x82, struct.pack('b', alpha))
126+
self.i2c.writeto_mem(self.addr, 0x82, struct.pack("b", alpha))
134127

135128
def getEMAFilterAlpha(self) -> int:
136-
"""! Get ema filter alpha
137-
138-
"""
129+
"""! Get ema filter alpha"""
139130
data = self.i2c.readfrom_mem(self.addr, 0x82, 1)
140-
return struct.unpack('b', data)[0]
131+
return struct.unpack("b", data)[0]

0 commit comments

Comments
 (0)