upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
bh1749.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 "bh1749_registers.h"
37 
49 typedef enum {
50  INT_JUDGE_0 = BH1749_PERSISTENCE_MODE_STATUS_ACTIVE_AFTER_MEASUREMENT,
51  INT_JUDGE_1 = BH1749_PERSISTENCE_MODE_STATUS_UPDATE_AFTER_MEASUREMENT,
52  INT_JUDGE_4 = BH1749_PERSISTENCE_MODE_STATUS_UPDATE_AFTER_4_SAME,
53  INT_JUDGE_8 = BH1749_PERSISTENCE_MODE_STATUS_UPDATE_AFTER_8_SAME,
55 
59 typedef enum {
60  MEAS_35MS = BH1749_MODE_CONTROL1_ODR_28P6,
61  MEAS_120MS = BH1749_MODE_CONTROL1_ODR_8P333,
62  MEAS_240MS = BH1749_MODE_CONTROL1_ODR_4P167
63 } MEAS_TIMES;
64 
68 typedef enum {
69  RGB_GAIN_1X = BH1749_MODE_CONTROL1_RGB_GAIN_1X,
70  RGB_GAIN_32X = BH1749_MODE_CONTROL1_RGB_GAIN_32X
71 } RGB_GAINS;
72 
76 typedef enum {
77  IR_GAIN_1X = BH1749_MODE_CONTROL1_IR_GAIN_1X,
78  IR_GAIN_32X = BH1749_MODE_CONTROL1_IR_GAIN_32X
79 } IR_GAINS;
80 
84 typedef enum {
85  RED = BH1749_INTERRUPT_SOURCE_SELECT_RED,
86  GREEN = BH1749_INTERRUPT_SOURCE_SELECT_GREEN,
87  BLUE = BH1749_INTERRUPT_SOURCE_SELECT_BLUE
88 } INT_SOURCES;
89 
93 typedef struct _bh1749_context
94 {
95  mraa_i2c_context i2c;
96  mraa_gpio_context interrupt;
97  bool enabled;
98  bool isrEnabled;
99  uint16_t int_thh;
100  uint16_t int_thl;
101  INT_SOURCES int_src;
102  IR_GAINS ir_gain;
103  RGB_GAINS rgb_gain;
104  MEAS_TIMES meas_time;
105  OPERATING_MODES operating_mode;
106 } *bh1749_context;
107 
114 upm_result_t bh1749_check_who_am_i(bh1749_context dev);
115 
125 bh1749_context bh1749_init(int bus, int addr);
126 
132 void bh1749_close(bh1749_context dev);
133 
140 upm_result_t bh1749_enable(bh1749_context dev);
141 
148 upm_result_t bh1749_disable(bh1749_context dev);
149 
161 upm_result_t bh1749_sensor_init(bh1749_context dev, OPERATING_MODES opMode,
162  MEAS_TIMES measTime,
163  RGB_GAINS rgbGain,
164  IR_GAINS irGain,
165  INT_SOURCES intSource);
166 
174 upm_result_t bh1749_set_operating_mode(bh1749_context dev, OPERATING_MODES opMode);
175 
183 upm_result_t bh1749_get_operating_mode(bh1749_context dev, uint8_t *opMode);
184 
192 upm_result_t bh1749_set_measurement_time(bh1749_context dev, MEAS_TIMES measTime);
193 
201 upm_result_t bh1749_get_measurement_time(bh1749_context dev, uint8_t *meas_time);
202 
210 upm_result_t bh1749_set_rgb_gain(bh1749_context dev, RGB_GAINS rgbGain);
211 
219 upm_result_t bh1749_get_rgb_gain(bh1749_context dev, uint8_t *gain);
220 
228 upm_result_t bh1749_set_ir_gain(bh1749_context dev, IR_GAINS irGain);
229 
237 upm_result_t bh1749_get_ir_gain(bh1749_context dev, uint8_t *gain);
238 
246 upm_result_t bh1749_set_int_source(bh1749_context dev, INT_SOURCES intSource);
247 
255 
262 upm_result_t bh1749_enable_interrupt(bh1749_context dev);
263 
270 upm_result_t bh1749_disable_interrupt(bh1749_context dev);
271 
278 upm_result_t bh1749_reset_interrupt(bh1749_context dev);
279 
287 
295 
304 upm_result_t bh1749_soft_reset(bh1749_context dev);
305 
313 upm_result_t bh1749_set_threshold_high(bh1749_context dev, uint16_t threshold);
314 
322 upm_result_t bh1749_get_threshold_high(bh1749_context dev, uint16_t *threshold);
323 
331 upm_result_t bh1749_set_threshold_low(bh1749_context dev, uint16_t threshold);
332 
340 upm_result_t bh1749_get_threshold_low(bh1749_context dev, uint16_t *threshold);
341 
349 upm_result_t bh1749_get_red(bh1749_context dev, uint16_t *red);
350 
358 upm_result_t bh1749_get_green(bh1749_context dev, uint16_t *green);
359 
367 upm_result_t bh1749_get_blue(bh1749_context dev, uint16_t *blue);
368 
376 upm_result_t bh1749_get_ir(bh1749_context dev, uint16_t *ir);
377 
385 upm_result_t bh1749_get_green2(bh1749_context dev, uint16_t *green2);
386 
396 upm_result_t bh1749_get_measurements(bh1749_context dev, uint16_t *result);
397 
408 upm_result_t bh1749_install_isr(bh1749_context dev, mraa_gpio_edge_t edge, int pin,
409  void (*isr)(void *), void *isr_args);
410 
417 
425 upm_result_t bh1749_registers_dump(bh1749_context dev, char *dump);
426 
427 #ifdef __cplusplus
428 }
429 #endif
upm_result_t bh1749_get_threshold_high(bh1749_context dev, uint16_t *threshold)
Gets interrupt threshold high value.
Definition: bh1749.c:519
upm_result_t bh1749_enable_interrupt(bh1749_context dev)
Enables interrupt mode and resets the interrupt status (clear)
Definition: bh1749.c:453
upm_result_t bh1749_set_measurement_time(bh1749_context dev, MEAS_TIMES measTime)
Sets measurement time (ODR)
Definition: bh1749.c:312
char bh1749_get_interrupt_source_char(bh1749_context dev)
Gets interrupt source value.
Definition: bh1749.c:428
void bh1749_close(bh1749_context dev)
Close and free sensor context.
Definition: bh1749.c:222
bool bh1749_is_interrupted(bh1749_context dev)
Checks the status of the interrupt.
Definition: bh1749.c:470
upm_result_t bh1749_get_threshold_low(bh1749_context dev, uint16_t *threshold)
Gets interrupt threshold low value.
Definition: bh1749.c:543
The full sensor context.
Definition: bh1749.h:93
upm_result_t bh1749_get_ir_gain(bh1749_context dev, uint8_t *gain)
Gets IR gain value.
Definition: bh1749.c:394
upm_result_t bh1749_get_blue(bh1749_context dev, uint16_t *blue)
Gets value of Blue color channel.
Definition: bh1749.c:583
upm_result_t bh1749_get_red(bh1749_context dev, uint16_t *red)
Gets value of Red color channel.
Definition: bh1749.c:556
RGB_GAINS
RGB gain choices.
Definition: bh1749.h:68
upm_result_t bh1749_get_measurement_time(bh1749_context dev, uint8_t *meas_time)
Sets measurement time (ODR)
Definition: bh1749.c:324
upm_result_t bh1749_disable_interrupt(bh1749_context dev)
Disables interrupt mode.
Definition: bh1749.c:460
upm_result_t bh1749_disable(bh1749_context dev)
Disables RGB color measurement on the sensor.
Definition: bh1749.c:248
upm_result_t bh1749_sensor_init(bh1749_context dev, OPERATING_MODES opMode, MEAS_TIMES measTime, RGB_GAINS rgbGain, IR_GAINS irGain, INT_SOURCES intSource)
Initializes (writes) configuration values to sensor.
Definition: bh1749.c:263
struct _bh1749_context * bh1749_context
The full sensor context.
upm_result_t bh1749_set_threshold_high(bh1749_context dev, uint16_t threshold)
Sets interrupt threshold high value.
Definition: bh1749.c:507
upm_result_t bh1749_set_ir_gain(bh1749_context dev, IR_GAINS irGain)
Sets IR gain values.
Definition: bh1749.c:382
upm_result_t bh1749_install_isr(bh1749_context dev, mraa_gpio_edge_t edge, int pin, void(*isr)(void *), void *isr_args)
Installs the ISR to a given GPIO pin.
Definition: bh1749.c:646
upm_result_t bh1749_reset_interrupt(bh1749_context dev)
Resets interrupt status (clear) to allow new interrupts.
Definition: bh1749.c:465
upm_result_t bh1749_registers_dump(bh1749_context dev, char *dump)
Gets a dump of configuration registers as a string.
Definition: bh1749.c:681
upm_result_t bh1749_get_operating_mode(bh1749_context dev, uint8_t *opMode)
Gets operating mode (interrupt persistance) value.
Definition: bh1749.c:291
upm_result_t bh1749_enable(bh1749_context dev)
Enables RGB color measurement on the sensor.
Definition: bh1749.c:233
OPERATING_MODES
Operation modes enum for interrupt modes (persistance)
Definition: bh1749.h:49
void bh1749_remove_isr(bh1749_context dev)
Removes the ISR if it is installed.
Definition: bh1749.c:673
upm_result_t bh1749_get_ir(bh1749_context dev, uint16_t *ir)
Gets value of IR color channel.
Definition: bh1749.c:596
upm_result_t bh1749_get_measurements(bh1749_context dev, uint16_t *result)
Gets all channels measurements values.
Definition: bh1749.c:622
upm_result_t bh1749_set_rgb_gain(bh1749_context dev, RGB_GAINS rgbGain)
Sets RGB gain values.
Definition: bh1749.c:348
upm_result_t bh1749_soft_reset(bh1749_context dev)
Initiates a software reset to the sensor. All register values will be written to their defaults...
Definition: bh1749.c:502
IR_GAINS
IR gain choices.
Definition: bh1749.h:76
upm_result_t bh1749_get_green(bh1749_context dev, uint16_t *green)
Gets value of Green color channel.
Definition: bh1749.c:570
bh1749_context bh1749_init(int bus, int addr)
Init the sensor with specific bus and address. This function calls the sensor_init() function to set ...
Definition: bh1749.c:182
upm_result_t bh1749_get_green2(bh1749_context dev, uint16_t *green2)
Gets value of Green2 color channel.
Definition: bh1749.c:609
upm_result_t bh1749_set_int_source(bh1749_context dev, INT_SOURCES intSource)
Sets interrupt source value.
Definition: bh1749.c:416
MEAS_TIMES
Measuremnt time choices.
Definition: bh1749.h:59
INT_SOURCES
Interrupt source choices.
Definition: bh1749.h:84
upm_result_t bh1749_get_rgb_gain(bh1749_context dev, uint8_t *gain)
Gets RGB gain value.
Definition: bh1749.c:360
upm_result_t bh1749_check_who_am_i(bh1749_context dev)
Check "who am I" register value to identify the sensor.
Definition: bh1749.c:167
upm_result_t bh1749_set_threshold_low(bh1749_context dev, uint16_t threshold)
Sets interrupt threshold low value.
Definition: bh1749.c:531
upm_result_t bh1749_set_operating_mode(bh1749_context dev, OPERATING_MODES opMode)
Sets operating mode (interrupt persistance)
Definition: bh1749.c:280
bool bh1749_is_interrupt_enabled(bh1749_context dev)
Checks whether interrupt mode is enabled.
Definition: bh1749.c:486