Skip to content

Commit 13558f3

Browse files
committed
Merge branch 'develop-pandian' into develop
2 parents 6b34561 + 9f298e9 commit 13558f3

File tree

6 files changed

+213
-11
lines changed

6 files changed

+213
-11
lines changed

m5stack/libs/unit/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
"OPUnit": "op",
3838
"RFIDUnit": "rfid",
3939
"LoRaE220JPUnit": "lora_e220_jp",
40+
"WEIGHTUnit": "weight",
41+
"SCALESUnit": "scales",
4042
}
4143

4244
def __getattr__(attr):

m5stack/libs/unit/limit.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1+
from machine import Pin
12
from hardware import Button
23

4+
SWITCH = 1
5+
COUNTER = 2
36

4-
def LIMITUnit(port):
5-
return Button(port[0])
7+
class LIMITUnit(Button):
8+
def __init__(self, port, active_low=True, type=SWITCH):
9+
self.count_value = 0
10+
self.curr_status = 0
11+
self.prev_status = 0 if active_low else 1
12+
self.pin = Pin(port[0], Pin.IN, Pin.PULL_UP)
13+
super().__init__(port[0], active_low=active_low)
14+
if type != SWITCH:
15+
self.pin.irq(handler=self._cb_irq, trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING))
16+
17+
def _cb_irq(self, arg):
18+
self.curr_status = self.pin.value()
19+
if self.curr_status != self.prev_status:
20+
self.count_value += 1
21+
self.prev_status = self.curr_status
22+
23+
def count_reset(self):
24+
self.count_value = 0

m5stack/libs/unit/manifest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
"op.py",
4040
'lora_e220_jp.py',
4141
"rfid.py",
42+
"weight.py",
43+
"scales.py",
4244
),
4345
base_path="..",
4446
opt=0,

m5stack/libs/unit/op.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
from machine import Pin
2+
from hardware import Button
23

34
SWITCH = 1
45
COUNTER = 2
56

6-
7-
class OPUnit:
8-
def __init__(self, port, type=SWITCH):
7+
class OPUnit(Button):
8+
def __init__(self, port, active_low=True, type=SWITCH):
99
self.count_value = 0
10+
self.curr_status = 0
11+
self.prev_status = 0 if active_low else 1
1012
self.pin = Pin(port[0], Pin.IN, Pin.PULL_UP)
13+
super().__init__(port[0], active_low=active_low)
1114
if type != SWITCH:
12-
self.pin.irq(handler=self._cb_irq, trigger=Pin.IRQ_RISING)
13-
14-
@property
15-
def get_value(self):
16-
return self.pin.value()
15+
self.pin.irq(handler=self._cb_irq, trigger=(Pin.IRQ_FALLING | Pin.IRQ_RISING))
1716

1817
def _cb_irq(self, arg):
19-
self.count_value += 1
18+
self.curr_status = self.pin.value()
19+
if self.curr_status != self.prev_status:
20+
self.count_value += 1
21+
self.prev_status = self.curr_status
2022

2123
def count_reset(self):
2224
self.count_value = 0

m5stack/libs/unit/scales.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
from machine import I2C
2+
from .pahub import PAHUBUnit
3+
from .unit_helper import UnitError
4+
import time
5+
import struct
6+
7+
try:
8+
from typing import Union
9+
except ImportError:
10+
pass
11+
12+
13+
SCALES_ADDR = 0x26
14+
15+
############# BUTTON STATUS ##############
16+
BUTTON_SHORT = 0x20
17+
BUTTON_LONG = 0x21
18+
BUTTON_STATUS = 0x22
19+
BUTTON_OFFSET = 0x23
20+
21+
############## LED CONTROL ###############
22+
LED_SYNC = 0x25
23+
LED_RED = 0x50
24+
LED_GREEN = 0x51
25+
LED_BLUE = 0x52
26+
27+
######## SCALES STATUS & CONTROL #########
28+
RAW_ADC = 0x10
29+
GET_WEIGHT = 0x14
30+
OFFSET_ADC = 0x18
31+
SOFT_OFFSET = 0x24
32+
33+
############## CALIBRATION ###############
34+
CALI_ZERO = 0x30
35+
CALI_LOAD = 0x32
36+
37+
############# DEVICE STATUS ##############
38+
FW_VER_REG = 0xFE
39+
I2C_ADDR_REG = 0xFF
40+
41+
42+
class SCALESUnit:
43+
def __init__(self, i2c: Union[I2C, PAHUBUnit], addr = SCALES_ADDR) -> None:
44+
self.i2c = i2c
45+
self.i2c_addr = addr
46+
self._available()
47+
48+
def _available(self) -> None:
49+
if not (self.i2c_addr in self.i2c.scan()):
50+
raise UnitError("Scales unit maybe not connect")
51+
52+
def get_button_status(self, status) -> int:
53+
status += BUTTON_SHORT
54+
if status >= BUTTON_SHORT and status <= BUTTON_OFFSET:
55+
return self.i2c.readfrom_mem(self.i2c_addr, status, 1)[0]
56+
57+
def set_button_offset(self, enable) -> None:
58+
self.i2c.writeto_mem(self.i2c_addr, BUTTON_OFFSET, bytes([enable]))
59+
60+
def set_rgbled_sync(self, control) -> None:
61+
self.i2c.writeto_mem(self.i2c_addr, LED_SYNC, bytes([control]))
62+
63+
def get_rgbled_sync(self) -> int:
64+
return self.i2c.readfrom_mem(self.i2c_addr, LED_SYNC, 1)[0]
65+
66+
def set_rgb_led(self, rgb) -> None:
67+
self.i2c.writeto_mem(self.i2c_addr, LED_RED, rgb.to_bytes(3, 'big'))
68+
69+
def get_rgb_led(self) -> list:
70+
return list(self.i2c.readfrom_mem(self.i2c_addr, LED_RED, 3))
71+
72+
def get_scale_value(self, scale) -> int:
73+
scale = RAW_ADC + (scale * 4)
74+
if scale >= RAW_ADC and scale <= OFFSET_ADC:
75+
byte_val = self.i2c.readfrom_mem(self.i2c_addr, scale, 4)
76+
if scale == GET_WEIGHT:
77+
return int((struct.unpack('>i', byte_val)[0]) /100)
78+
return struct.unpack('>i', byte_val)[0]
79+
80+
def set_raw_offset(self, value) -> None:
81+
self.i2c.writeto_mem(self.i2c_addr, OFFSET_ADC, value.to_bytes(4, 'big'))
82+
83+
def set_current_raw_offset(self) -> None:
84+
self.i2c.writeto_mem(self.i2c_addr, SOFT_OFFSET, bytes([1]))
85+
86+
def set_calibration_zero(self) -> None:
87+
self.i2c.writeto_mem(self.i2c_addr, CALI_ZERO, bytes([0,0]))
88+
time.sleep_ms(200)
89+
90+
def set_calibration_load(self, gram) -> None:
91+
self.i2c.writeto_mem(self.i2c_addr, CALI_LOAD, gram.to_bytes(2, 'little'))
92+
time.sleep_ms(200)
93+
94+
def get_device_inform(self, mode) -> int:
95+
if mode >= FW_VER_REG and mode <= I2C_ADDR_REG:
96+
return self.i2c.readfrom_mem(self.i2c_addr, mode, 1)[0]
97+
98+
def set_i2c_address(self, addr):
99+
if addr >= 1 and addr <= 127:
100+
if addr != self.i2c_addr:
101+
self.i2c.writeto_mem(self.i2c_addr, I2C_ADDR_REG, bytes([addr]))
102+
self.i2c_addr = addr
103+
time.sleep_ms(200)
104+

m5stack/libs/unit/weight.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from machine import Pin
2+
import time
3+
from micropython import const
4+
5+
CHANNEL_A_128 = const(1)
6+
CHANNEL_A_64 = const(3)
7+
CHANNEL_B_32 = const(2)
8+
9+
DATA_BITS = const(24)
10+
MAX_VALUE = const(0x7fffff)
11+
MIN_VALUE = const(0x800000)
12+
13+
class WEIGHTUnit:
14+
def __init__(self, port) -> None:
15+
self.hx711data = Pin(port[0], Pin.IN)
16+
self.hx711clk = Pin(port[1], Pin.OUT)
17+
self.zero_value = 0
18+
self.hx711clk.value(0)
19+
self._channel=1
20+
self._scale=1.0
21+
22+
@property
23+
def get_raw_weight(self) -> int:
24+
count = 0
25+
if self.is_ready_wait():
26+
for i in range(24):
27+
self.hx711clk.value(1)
28+
time.sleep_us(1)
29+
self.hx711clk.value(0)
30+
time.sleep_us(1)
31+
if self.hx711data.value():
32+
count += 1
33+
count = count << 1
34+
else:
35+
return 0
36+
37+
gain_m = self._channel
38+
while gain_m:
39+
self.hx711clk.value(1)
40+
time.sleep_us(1)
41+
self.hx711clk.value(0)
42+
time.sleep_us(1)
43+
gain_m -= 1
44+
count = count ^ 0x800000
45+
return count
46+
47+
@property
48+
def get_scale_weight(self) -> int:
49+
return int((self.get_raw_weight - self.zero_value) * self._scale)
50+
51+
def set_tare(self) -> None:
52+
self.zero_value = self.get_raw_weight
53+
54+
def set_calibrate_scale(self, weight):
55+
self._scale = (1.0 * weight) / (self.get_raw_weight - self.zero_value)
56+
57+
def is_ready_wait(self) -> bool:
58+
times = 0
59+
while self.hx711data.value():
60+
times += 1
61+
time.sleep_ms(10)
62+
if times > 25:
63+
break
64+
return not self.hx711data.value()
65+
66+
def set_channel(self, chan: int) -> None:
67+
self._channel = chan
68+
for i in range(self._channel):
69+
self.hx711clk.value(1)
70+
time.sleep_us(1)
71+
self.hx711clk.value(0)
72+
time.sleep_us(1)
73+

0 commit comments

Comments
 (0)