upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
bh1792.h
Go to the documentation of this file.
1 /*
2 * The MIT License (MIT)
3 *
4 * Author: Assam Boudjelthia
5 * Copyright (c) 2018 Rohm Semiconductor.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24 
25 #pragma once
26 
27 #ifdef __cplusplus
28 extern "C"
29 {
30 #endif
31 
32 #include <mraa/i2c.h>
33 #include <mraa/gpio.h>
34 #include <unistd.h>
35 #include <upm_types.h>
36 #include <pthread.h>
37 #include "bh1792glc_registers.h"
38 
47 #define ONE_SEC_IN_MIRCO_SEC 1000000
48 #define FIFO_WATERMARK 32
49 #define LED_CURRENT_MAX 63
50 
55 typedef enum {
56  MSR_64Hz = BH1792_MEAS_CONTROL1_MSR_64HZ,
57  MSR_32Hz = BH1792_MEAS_CONTROL1_MSR_32HZ,
58  MSR_128Hz = BH1792_MEAS_CONTROL1_MSR_128HZ,
59  MSR_256Hz = BH1792_MEAS_CONTROL1_MSR_256HZ,
60  MSR_1024Hz = BH1792_MEAS_CONTROL1_MSR_1024HZ,
61  MSR_NON_SYNC = BH1792_MEAS_CONTROL1_MSR_NON_SYNCH_MODE,
62  MSR_SINGLE = BH1792_MEAS_CONTROL1_MSR_SINGLE_MEAS_MODE
63 } MEAS_MODES;
64 
68 typedef enum {
69  GREEN,
70  IR
71 } LED_TYPES;
72 
76 typedef enum {
77  WATER_MARK = BH1792_MEAS_CONTROL5_INT_SEL_FIFO_WATERMARK,
78  IR_THRESHOLD_JUDGE = BH1792_MEAS_CONTROL5_INT_SEL_IR_THRESHOLD,
79  ON_COMPLETE = BH1792_MEAS_CONTROL5_INT_SEL_ON_COMPLETE
81 
85 typedef enum {
86  SYNCHRONIZED,
87  NON_SYNCHRONIZED,
88  SINGLE_GREEN,
89  SINGLE_IR
90 } OP_MODES;
91 
95 typedef struct _bh1792_context
96 {
97  mraa_i2c_context i2c;
98  mraa_gpio_context interrupt;
99  bool enabled;
100  bool isrEnabled;
101  OP_MODES op_mode;
102  pthread_t sync_thread;
103  bool sync_thread_alive;
104  LED_TYPES led_type;
105  INTERRUPT_MODES interrupt_mode;
106  uint16_t meas_freq;
107  uint8_t green_current;
108  uint8_t ir_current;
109  uint16_t threshold;
110 } *bh1792_context;
111 
121 bh1792_context bh1792_init(int bus, int addr);
122 
128 void bh1792_close(bh1792_context dev);
129 
136 upm_result_t bh1792_check_who_am_i(bh1792_context dev);
137 
145 
154 upm_result_t bh1792_soft_reset(bh1792_context dev);
155 
163 upm_result_t bh1792_get_meas_time_ms(bh1792_context dev, float *meas_time);
164 
172 upm_result_t bh1792_set_green_leds_current(bh1792_context dev, uint8_t current);
173 
181 upm_result_t bh1792_get_green_leds_current(bh1792_context dev, uint8_t *current);
182 
190 upm_result_t bh1792_set_ir_led_current(bh1792_context dev, uint8_t current);
191 
199 upm_result_t bh1792_get_ir_led_current(bh1792_context dev, uint8_t *current);
200 
208 upm_result_t bh1792_set_ir_threshold(bh1792_context dev, uint16_t threshold);
209 
217 upm_result_t bh1792_get_ir_threshold(bh1792_context dev, uint16_t *threshold);
218 
226 upm_result_t bh1792_get_fifo_size(bh1792_context dev, uint8_t *count);
227 
236 upm_result_t bh1792_disable_interrupt(bh1792_context dev);
237 
244 upm_result_t bh1792_clear_interrupt(bh1792_context dev);
245 
252 upm_result_t bh1792_start_measurement(bh1792_context dev);
253 
262 upm_result_t bh1792_stop_measurement(bh1792_context dev);
263 
273 
283 upm_result_t bh1792_get_fifo_data(bh1792_context dev, uint16_t fifo_led_off[],
284  uint16_t fifo_led_on[]);
285 
293 upm_result_t bh1792_discard_fifo_data(bh1792_context dev);
294 
303 upm_result_t bh1792_get_green_data(bh1792_context dev, uint16_t *green_led_off,
304  uint16_t *green_led_on);
305 
314 upm_result_t bh1792_get_ir_data(bh1792_context dev, uint16_t *ir_led_off,
315  uint16_t *ir_led_on);
316 
331 upm_result_t bh1792_enable_sync_mode(bh1792_context dev, uint16_t meas_freq,
332  uint8_t green_current);
333 
347 upm_result_t bh1792_enable_non_sync_mode(bh1792_context dev, uint8_t ir_current,
348  uint16_t threshold);
349 
365 upm_result_t bh1792_enable_single_mode(bh1792_context dev, LED_TYPES led_type,
366  uint8_t current);
367 
378 upm_result_t bh1792_install_isr(bh1792_context dev, mraa_gpio_edge_t edge,
379  int pin, void (*isr)(void *), void *isr_args);
380 
390 upm_result_t bh1792_install_isr_falling_edge(bh1792_context dev, int pin,
391  void (*isr)(void *), void *isr_args);
392 
399 
407 upm_result_t bh1792_registers_dump(bh1792_context dev, char *dump);
408 
409 #ifdef __cplusplus
410 }
411 #endif
upm_result_t bh1792_clear_interrupt(bh1792_context dev)
Clears the interrupt flag allowing more interrupts to be raised.
Definition: bh1792.c:580
upm_result_t bh1792_install_isr_falling_edge(bh1792_context dev, int pin, void(*isr)(void *), void *isr_args)
Installs the ISR to a given GPIO pin.
Definition: bh1792.c:849
upm_result_t bh1792_soft_reset(bh1792_context dev)
Initiates a software reset to the sensor. All register values will be written to their defaults...
Definition: bh1792.c:340
upm_result_t bh1792_enable_non_sync_mode(bh1792_context dev, uint8_t ir_current, uint16_t threshold)
Enabled and configures the sensor to use non-synchronized mode. This mode uses IR_THRESHOLD interrupt...
Definition: bh1792.c:767
upm_result_t bh1792_get_ir_threshold(bh1792_context dev, uint16_t *threshold)
Gets interrupt ir threshold value.
Definition: bh1792.c:532
upm_result_t bh1792_get_fifo_size(bh1792_context dev, uint8_t *count)
Gets the current size of built-in FIFO.
Definition: bh1792.c:547
LED_TYPES
LED light types.
Definition: bh1792.h:68
bh1792 sensor context
Definition: bh1792.h:95
upm_result_t bh1792_get_ir_data(bh1792_context dev, uint16_t *ir_led_off, uint16_t *ir_led_on)
Gets the values from using IR LED light.
Definition: bh1792.c:722
upm_result_t bh1792_get_green_leds_current(bh1792_context dev, uint8_t *current)
Gets the green LED current value.
Definition: bh1792.c:468
upm_result_t bh1792_set_green_leds_current(bh1792_context dev, uint8_t current)
Sets the green LED Current value.
Definition: bh1792.c:448
upm_result_t bh1792_install_isr(bh1792_context dev, mraa_gpio_edge_t edge, int pin, void(*isr)(void *), void *isr_args)
Installs the ISR to a given GPIO pin.
Definition: bh1792.c:819
upm_result_t bh1792_disable_interrupt(bh1792_context dev)
Disables interrupt function. Interrupt is enabled by default on all operation modes, so this have to be called after enabling a specific operation mode.
Definition: bh1792.c:574
upm_result_t bh1792_stop_measurement(bh1792_context dev)
Definition: bh1792.c:620
upm_result_t bh1792_get_ir_led_current(bh1792_context dev, uint8_t *current)
Gets the IR LED current value.
Definition: bh1792.c:502
upm_result_t bh1792_check_who_am_i(bh1792_context dev)
Check "who am I" register value to identify the sensor.
Definition: bh1792.c:302
void bh1792_remove_isr(bh1792_context dev)
Removes the ISR if it is installed.
Definition: bh1792.c:855
upm_result_t bh1792_enable_sync_mode(bh1792_context dev, uint16_t meas_freq, uint8_t green_current)
Enables and configures the sensor to use synchronized mode. This will use the built-in FIFO and raise...
Definition: bh1792.c:742
upm_result_t bh1792_restart_measurement(bh1792_context dev)
Restart the measurement, This function stops measurement, then re-writes the configuration registers ...
Definition: bh1792.c:633
upm_result_t bh1792_enable_single_mode(bh1792_context dev, LED_TYPES led_type, uint8_t current)
Enables and configures the sensor to use single mode. This sets the sensor to take only one measureme...
Definition: bh1792.c:788
struct _bh1792_context * bh1792_context
bh1792 sensor context
upm_result_t bh1792_start_measurement(bh1792_context dev)
Starts the measuremnt on sensor or enable the sensor.
Definition: bh1792.c:599
upm_result_t bh1792_discard_fifo_data(bh1792_context dev)
Reads and discard the 32 FIFO entries to reset the WATERMARK interrupt flag.
Definition: bh1792.c:689
OP_MODES
Operations modes available with the sensor.
Definition: bh1792.h:85
upm_result_t bh1792_set_ir_led_current(bh1792_context dev, uint8_t current)
Sets the IR LED Current value.
Definition: bh1792.c:482
upm_result_t bh1792_get_fifo_data(bh1792_context dev, uint16_t fifo_led_off[], uint16_t fifo_led_on[])
Gets the fifo data of 32 entries and resets the WATERMARK interrupt flag.
Definition: bh1792.c:668
upm_result_t bh1792_get_meas_time_ms(bh1792_context dev, float *meas_time)
Gets the value of measurement time is milli-seconds.
Definition: bh1792.c:412
bool bh1792_is_enabled(bh1792_context dev)
Checks if the measuremnt on sensor is enabled.
Definition: bh1792.c:332
upm_result_t bh1792_get_green_data(bh1792_context dev, uint16_t *green_led_off, uint16_t *green_led_on)
Gets the values from using green LED light.
Definition: bh1792.c:702
bh1792_context bh1792_init(int bus, int addr)
Init the sensor with specific bus and address. Before starting measurement, one of (bh1792_enable_syn...
Definition: bh1792.c:246
void bh1792_close(bh1792_context dev)
Close and free sensor context.
Definition: bh1792.c:283
MEAS_MODES
Measurement modes, for synchronized time modes, non synchronized and single modes.
Definition: bh1792.h:55
INTERRUPT_MODES
Interrupt modes of the sensor.
Definition: bh1792.h:76
upm_result_t bh1792_registers_dump(bh1792_context dev, char *dump)
Gets a dump of configuration registers as a string.
Definition: bh1792.c:865
upm_result_t bh1792_set_ir_threshold(bh1792_context dev, uint16_t threshold)
Sets interrupt ir threshold value for non-synchronized mode.
Definition: bh1792.c:516