1
+ from machine import I2C
2
+ from micropython import const
3
+ import struct
4
+ from .pahub import PAHUB
5
+ from .unit_helper import UnitError
6
+ import time
7
+
8
+ try :
9
+ from typing import Union
10
+ except ImportError :
11
+ pass
12
+
13
+ HBRIDGE_ADDR = 0x20
14
+
15
+ DIRECTION_REG = 0x00
16
+ PWM8BIT_REG = 0x01
17
+ PWM16BIT_REG = 0x02
18
+ PWMFREQ_REG = 0x04
19
+ ADC8BIT_REG = 0x10
20
+ ADC16BIT_REG = 0x20
21
+ VIN_CURRENT_REG = 0x30
22
+ I2C_ADDR_REG = 0xFF
23
+ FW_VER_REG = 0xFE
24
+
25
+ class HBRIDGE :
26
+ def __init__ (self , i2c : Union [I2C , PAHUB ], slave_addr = HBRIDGE_ADDR ):
27
+ """
28
+ Hbridge Initialize Function
29
+ Set I2C port, Hbridge Slave Address
30
+ """
31
+ self .hbridge_i2c = i2c
32
+ self .init_i2c_address (slave_addr )
33
+
34
+ def init_i2c_address (self , slave_addr = HBRIDGE_ADDR ):
35
+ """
36
+ init the i2c address
37
+ slave_addr : 0x20 to 0x2F
38
+ """
39
+ if slave_addr >= 0x20 and slave_addr <= 0x2F :
40
+ self .i2c_addr = slave_addr
41
+ if not (self .i2c_addr in self .hbridge_i2c .scan ()):
42
+ raise UnitError ("Hbridge unit maybe not connect" )
43
+
44
+ def get_driver_config (self , reg = 0 ):
45
+ """
46
+ get driver config value
47
+ """
48
+ leng = 1
49
+ if reg > 1 :
50
+ leng = 2
51
+ buf = self .read_reg (reg , leng )
52
+ return struct .unpack ('<H' , buf )[0 ]
53
+ else :
54
+ return self .read_reg (reg , leng )[0 ]
55
+
56
+ def set_direction (self , dir = 0 ):
57
+ """
58
+ set direction
59
+ dir : 0 stop, 1 forward, 2 reverse
60
+ """
61
+ self .write_mem_list (DIRECTION_REG , [dir ])
62
+
63
+ def set_8bit_pwm (self , duty = 0 ):
64
+ """
65
+ set 8bit pwm dutycycle
66
+ duty : 0 to 255
67
+ """
68
+ self .write_mem_list (PWM8BIT_REG , [duty ])
69
+
70
+ def set_16bit_pwm (self , duty = 0 ):
71
+ """
72
+ set 16bit pwm dutycycle
73
+ duty : 0 to 65535
74
+ """
75
+ self .write_mem_list (PWM16BIT_REG , [(duty & 0xff ), ((duty >> 8 ) & 0xff )])
76
+
77
+ def set_pwm_freq (self , freq = 0 ):
78
+ """
79
+ set direction
80
+ duty : 0 to 65535
81
+ """
82
+ freq = max (min (freq , 10000 ),100 )
83
+ self .write_mem_list (PWMFREQ_REG , [(freq & 0xff ), ((freq >> 8 ) & 0xff )])
84
+
85
+ def get_adc_value (self , raw = 0 , res = 8 ):
86
+ """
87
+ get adc value
88
+ """
89
+ leng = 1
90
+ if res > 8 :
91
+ leng = 2
92
+ buf = self .read_reg (ADC16BIT_REG , leng )
93
+ val = struct .unpack ('<H' , buf )[0 ]
94
+ res = 4095
95
+ else :
96
+ val = self .read_reg (ADC8BIT_REG , leng )[0 ]
97
+ res = 255
98
+ if raw :
99
+ return val
100
+ else :
101
+ val = (3.3 / res ) * val * 11
102
+ return round (val , 2 )
103
+
104
+ #############################support v1.1################################
105
+ def get_vin_current (self ):
106
+ """
107
+ get vin current.
108
+ """
109
+ buf = self .read_reg (VIN_CURRENT_REG , 4 )
110
+ return struct .unpack ('<f' , buf )[0 ]
111
+ #############################support v1.1################################
112
+
113
+ def get_device_status (self , mode ):
114
+ """
115
+ get firmware version and i2c address.
116
+ mode : 0xFE and 0xFF
117
+ """
118
+ if mode >= FW_VER_REG and mode <= I2C_ADDR_REG :
119
+ return self .read_reg (mode , 1 )[0 ]
120
+
121
+ def write_mem_list (self , reg , data ):
122
+ buf = bytearray (data )
123
+ self .hbridge_i2c .writeto_mem (self .i2c_addr , reg , buf )
124
+
125
+ def read_reg (self , reg , num ):
126
+ return self .hbridge_i2c .readfrom_mem (self .i2c_addr , reg , num )
127
+
128
+ def deinit (self ):
129
+ pass
130
+
131
+ '''
132
+ if __name__ == "__main__":
133
+ import unit
134
+ hbridge = unit.get(unit.HBRIDGE, unit.PORTA)
135
+ hbridge.get_driver_config(0)
136
+ hbridge.get_driver_config(1)
137
+ '''
0 commit comments