LibDriver PCA9685  1.0.0
PCA9685 full-featured driver
driver_pca9685_basic.c
Go to the documentation of this file.
1 
37 #include "driver_pca9685_basic.h"
38 
39 static pca9685_handle_t gs_handle;
50 uint8_t pca9685_basic_init(pca9685_address_t addr, uint16_t hz)
51 {
52  uint8_t res;
53  uint8_t reg;
54 
55  /* link interface function */
66 
67  /* set addr pin */
68  res = pca9685_set_addr_pin(&gs_handle, addr);
69  if (res != 0)
70  {
71  pca9685_interface_debug_print("pca9685: set addr pin failed.\n");
72 
73  return 1;
74  }
75 
76  /* pca9685 init */
77  res = pca9685_init(&gs_handle);
78  if (res != 0)
79  {
80  pca9685_interface_debug_print("pca9685: init failed.\n");
81 
82  return 1;
83  }
84 
85  /* inactive */
86  res = pca9685_set_active(&gs_handle, PCA9685_BOOL_FALSE);
87  if (res != 0)
88  {
89  pca9685_interface_debug_print("pca9685: set active failed.\n");
90  (void)pca9685_deinit(&gs_handle);
91 
92  return 1;
93  }
94 
95  /* set sleep mode */
96  res = pca9685_set_sleep_mode(&gs_handle, PCA9685_BOOL_TRUE);
97  if (res != 0)
98  {
99  pca9685_interface_debug_print("pca9685: set sleep mode failed.\n");
100  (void)pca9685_deinit(&gs_handle);
101 
102  return 1;
103  }
104 
105  /* set frequency */
107  if (res != 0)
108  {
109  pca9685_interface_debug_print("pca9685: output frequency convert to register failed.\n");
110  (void)pca9685_deinit(&gs_handle);
111 
112  return 1;
113  }
114 
115  /* set pre scale */
116  res = pca9685_set_prescaler(&gs_handle, reg);
117  if (res != 0)
118  {
119  pca9685_interface_debug_print("pca9685: set pre scale failed.\n");
120  (void)pca9685_deinit(&gs_handle);
121 
122  return 1;
123  }
124 
125  /* disable external clock pin */
127  if (res != 0)
128  {
129  pca9685_interface_debug_print("pca9685: set external clock pin failed.\n");
130  (void)pca9685_deinit(&gs_handle);
131 
132  return 1;
133  }
134 
135  /* enable auto increment */
137  if (res != 0)
138  {
139  pca9685_interface_debug_print("pca9685: set register auto increment failed.\n");
140  (void)pca9685_deinit(&gs_handle);
141 
142  return 1;
143  }
144 
145  /* set respond sub address 1 */
147  if (res != 0)
148  {
149  pca9685_interface_debug_print("pca9685: set respond sub address 1 failed.\n");
150  (void)pca9685_deinit(&gs_handle);
151 
152  return 1;
153  }
154 
155  /* set respond sub address 2 */
157  if (res != 0)
158  {
159  pca9685_interface_debug_print("pca9685: set respond sub address 2 failed.\n");
160  (void)pca9685_deinit(&gs_handle);
161 
162  return 1;
163  }
164 
165  /* set respond sub address 3 */
167  if (res != 0)
168  {
169  pca9685_interface_debug_print("pca9685: set respond sub address 3 failed.\n");
170  (void)pca9685_deinit(&gs_handle);
171 
172  return 1;
173  }
174 
175  /* set respond all call */
177  if (res != 0)
178  {
179  pca9685_interface_debug_print("pca9685: set respond all call failed.\n");
180  (void)pca9685_deinit(&gs_handle);
181 
182  return 1;
183  }
184 
185  /* set output invert */
187  if (res != 0)
188  {
189  pca9685_interface_debug_print("pca9685: set output invert failed.\n");
190  (void)pca9685_deinit(&gs_handle);
191 
192  return 1;
193  }
194 
195  /* stop output change */
197  if (res != 0)
198  {
199  pca9685_interface_debug_print("pca9685: set output change failed.\n");
200  (void)pca9685_deinit(&gs_handle);
201 
202  return 1;
203  }
204 
205  /* set output driver */
207  if (res != 0)
208  {
209  pca9685_interface_debug_print("pca9685: set output driver failed.\n");
210  (void)pca9685_deinit(&gs_handle);
211 
212  return 1;
213  }
214 
215  /* set output disable type */
217  if (res != 0)
218  {
219  pca9685_interface_debug_print("pca9685: set output disable type failed.\n");
220  (void)pca9685_deinit(&gs_handle);
221 
222  return 1;
223  }
224 
225  /* set sub address 1 */
227  if (res != 0)
228  {
229  pca9685_interface_debug_print("pca9685: set sub address 1 failed.\n");
230  (void)pca9685_deinit(&gs_handle);
231 
232  return 1;
233  }
234 
235  /* set sub address 2 */
237  if (res != 0)
238  {
239  pca9685_interface_debug_print("pca9685: set sub address 2 failed.\n");
240  (void)pca9685_deinit(&gs_handle);
241 
242  return 1;
243  }
244 
245  /* set sub address 3 */
247  if (res != 0)
248  {
249  pca9685_interface_debug_print("pca9685: set sub address 3 failed.\n");
250  (void)pca9685_deinit(&gs_handle);
251 
252  return 1;
253  }
254 
255  /* set all call address */
257  if (res != 0)
258  {
259  pca9685_interface_debug_print("pca9685: set all call address failed.\n");
260  (void)pca9685_deinit(&gs_handle);
261 
262  return 1;
263  }
264 
265  /* set sleep mode */
266  res = pca9685_set_sleep_mode(&gs_handle, PCA9685_BOOL_FALSE);
267  if (res != 0)
268  {
269  pca9685_interface_debug_print("pca9685: set sleep mode failed.\n");
270  (void)pca9685_deinit(&gs_handle);
271 
272  return 1;
273  }
274 
275  /* active */
276  res = pca9685_set_active(&gs_handle, PCA9685_BOOL_TRUE);
277  if (res != 0)
278  {
279  pca9685_interface_debug_print("pca9685: set active failed.\n");
280  (void)pca9685_deinit(&gs_handle);
281 
282  return 1;
283  }
284 
285  return 0;
286 }
287 
295 uint8_t pca9685_basic_deinit(void)
296 {
297  uint8_t res;
298 
299  /* inactive */
300  res = pca9685_set_active(&gs_handle, PCA9685_BOOL_FALSE);
301  if (res != 0)
302  {
303  return 1;
304  }
305 
306  /* deinit */
307  res = pca9685_deinit(&gs_handle);
308  if (res != 0)
309  {
310  return 1;
311  }
312  else
313  {
314  return 0;
315  }
316 }
317 
330 uint8_t pca9685_basic_write(pca9685_channel_t channel, float delay_percent, float high_duty_cycle_percent)
331 {
332  uint8_t res;
333  uint16_t on_count, off_count;
334 
335  /* convert data */
336  res = pca9685_pwm_convert_to_register(&gs_handle, delay_percent, high_duty_cycle_percent, (uint16_t *)&on_count, (uint16_t *)&off_count);
337  if (res != 0)
338  {
339  return 1;
340  }
341 
342  /* write channel */
343  res = pca9685_write_channel(&gs_handle, channel, on_count, off_count);
344  if (res != 0)
345  {
346  return 1;
347  }
348 
349  return 0;
350 }
driver pca9685 basic header file
uint8_t pca9685_pwm_convert_to_register(pca9685_handle_t *handle, float delay_percent, float high_duty_cycle_percent, uint16_t *on_count, uint16_t *off_count)
convert the pwm to the register raw data
uint8_t pca9685_set_prescaler(pca9685_handle_t *handle, uint8_t prescaler)
set the clock pres cale
uint8_t pca9685_write_channel(pca9685_handle_t *handle, pca9685_channel_t channel, uint16_t on_count, uint16_t off_count)
write led channels
pca9685_address_t
pca9685 address enumeration definition
uint8_t pca9685_set_respond_all_call(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable respond all call
uint8_t pca9685_init(pca9685_handle_t *handle)
initialize the chip
uint8_t pca9685_set_active(pca9685_handle_t *handle, pca9685_bool_t enable)
set the chip active
uint8_t pca9685_set_addr_pin(pca9685_handle_t *handle, pca9685_address_t addr_pin)
set the address pin
uint8_t pca9685_set_output_driver(pca9685_handle_t *handle, pca9685_output_driver_t driver)
set the output driver type
uint8_t pca9685_set_respond_subaddress_2(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable respond sub address 2
uint8_t pca9685_set_output_disable_type(pca9685_handle_t *handle, pca9685_output_disable_type_t type)
set the output disable type
uint8_t pca9685_set_subaddress_2(pca9685_handle_t *handle, uint8_t addr)
set the sub address 2
uint8_t pca9685_output_frequency_convert_to_register(pca9685_handle_t *handle, uint32_t oscillator, uint16_t output_freq, uint8_t *reg)
convert the output frequency to the register raw data
uint8_t pca9685_set_register_auto_increment(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable the register auto increment
uint8_t pca9685_set_respond_subaddress_3(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable respond sub address 3
uint8_t pca9685_set_output_invert(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable output invert
pca9685_channel_t
pca9685 channel enumeration definition
uint8_t pca9685_set_subaddress_1(pca9685_handle_t *handle, uint8_t addr)
set the sub address 1
#define PCA9685_OSCILLATOR_INTERNAL_FREQUENCY
pca9685 internal oscillator frequency
uint8_t pca9685_set_sleep_mode(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable the sleep mode
uint8_t pca9685_set_external_clock_pin(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable the external clock pin
uint8_t pca9685_deinit(pca9685_handle_t *handle)
close the chip
uint8_t pca9685_set_respond_subaddress_1(pca9685_handle_t *handle, pca9685_bool_t enable)
enable or disable respond sub address 1
uint8_t pca9685_set_subaddress_3(pca9685_handle_t *handle, uint8_t addr)
set the sub address 3
uint8_t pca9685_set_all_call_address(pca9685_handle_t *handle, uint8_t addr)
set the all call address
uint8_t pca9685_set_output_change(pca9685_handle_t *handle, pca9685_output_change_t change)
set the output change type
@ PCA9685_BOOL_FALSE
@ PCA9685_BOOL_TRUE
@ PCA9685_OUTPUT_DISABLE_TYPE_HIGH_IMPEDANCE
#define PCA9685_BASIC_DEFAULT_SUBADDRESS_1
#define PCA9685_BASIC_DEFAULT_ALL_CALL_ADDRESS
#define PCA9685_BASIC_DEFAULT_RESPOND_ALL_CALL
#define PCA9685_BASIC_DEFAULT_OUTPUT_DRIVER
#define PCA9685_BASIC_DEFAULT_RESPOND_SUBADDRESS_1
pca9685 basic example default definition
#define PCA9685_BASIC_DEFAULT_OUTPUT_INVERT
uint8_t pca9685_basic_deinit(void)
basic example deinit
#define PCA9685_BASIC_DEFAULT_SUBADDRESS_3
#define PCA9685_BASIC_DEFAULT_SUBADDRESS_2
#define PCA9685_BASIC_DEFAULT_OUTPUT_CHANGE
uint8_t pca9685_basic_init(pca9685_address_t addr, uint16_t hz)
basic example init
uint8_t pca9685_basic_write(pca9685_channel_t channel, float delay_percent, float high_duty_cycle_percent)
basic example write
#define PCA9685_BASIC_DEFAULT_RESPOND_SUBADDRESS_2
void pca9685_interface_delay_ms(uint32_t ms)
interface delay ms
uint8_t pca9685_interface_oe_deinit(void)
interface oe deinit
uint8_t pca9685_interface_oe_init(void)
interface oe init
uint8_t pca9685_interface_iic_deinit(void)
interface iic bus deinit
uint8_t pca9685_interface_iic_read(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus read
uint8_t pca9685_interface_iic_init(void)
interface iic bus init
void pca9685_interface_debug_print(const char *const fmt,...)
interface print format data
uint8_t pca9685_interface_oe_write(uint8_t value)
interface oe write
uint8_t pca9685_interface_iic_write(uint8_t addr, uint8_t reg, uint8_t *buf, uint16_t len)
interface iic bus write
pca9685 handle structure definition