Hello, I recently acquited the Flora Lux Sensor I need to integrate in a wearable project. In fairness, I can make it work by re-using the provided example code, but I believe I could write a better, leaner code if I would understand better the syntax. I did read carefully the "Adafruit Unified Sensor Driver" README but would appreciate a clarification on two points.
The "Unified Driver Abstraction Layer in Practice" says (quote):
Using the unified sensor abstraction layer is relatively easy once a compliant driver has been created.
Every compliant sensor can now be read using a single, well-known 'type' (sensors_event_t), and there is a standardised way of interrogating a sensor about its specific capabilities (via sensor_t).
An example of reading the TSL2561 light sensor can be seen below:
Adafruit_TSL2561 tsl = Adafruit_TSL2561(TSL2561_ADDR_FLOAT, 12345);
...
/* Get a new sensor event */
sensors_event_t event;
tsl.getEvent(&event);
/* Display the results (light is measured in lux) */
if (event.light)
{
Serial.print(event.light); Serial.println(" lux");
}
else
{
/* If event.light = 0 lux the sensor is probably saturated
and no reliable data could be generated! */
Serial.println("Sensor overload");
}
Question #1 : why is it event is prefixed with a "&" in the "getEvent" instruction ? tsl.getEvent(&event);
I am not good at all at C or C++ but guess the & is some sort of pointer. But why do we need it here?
Question#2: what is the exact nature (type ?) of event.light in this line of code if (event.light)
I mean:
"event" is where the result of the sensing is stored. I'd say it is a variable. Does ".light" adds an information telling the .. system that the value of this variable is exepressed in Lux (SI reference).
In other words, if I was using an accelerometer iso a lux sensor, would we write here event. acceleration iso event.light?
Thank you in adavnce for you patience with a newbie.
Pierre
Adafruit's Unified Sensor Driver- Lux sensor question
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 39
- Joined: Fri Feb 14, 2014 5:07 am
- adafruit_support_bill
- Posts: 88096
- Joined: Sat Feb 07, 2009 10:11 am
Re: Adafruit's Unified Sensor Driver- Lux sensor question
The & in this case is the "address of" operator. Since we want the function to update the value of "event", we need to pass-by-reference instead of by value.Question #1 : why is it event is prefixed with a "&" in the "getEvent" instruction ? tsl.getEvent(&event);
I am not good at all at C or C++ but guess the & is some sort of pointer. But why do we need it here?
"event" is defined as a union. In other words, it is a variable that is capable of storing data of different types. "event.light" specifies that we are interpreting the value as a light reading in SI units.Question#2: what is the exact nature (type ?) of event.light in this line of code if (event.light)
https://github.com/adafruit/Adafruit_Se ... t_Sensor.h
-
- Posts: 39
- Joined: Fri Feb 14, 2014 5:07 am
Re: Adafruit's Unified Sensor Driver- Lux sensor question
Dear Bill,
Thank you so much for answering so quickly, on a Sunday, and in such a clear manner.
Regards,
Pierre
Thank you so much for answering so quickly, on a Sunday, and in such a clear manner.
Regards,
Pierre
Please be positive and constructive with your questions and comments.