Add support and sample example of use of the veml6070 uv sensor

master
Schoumi 7 months ago
parent fce420da48
commit f13349dfc5

@ -0,0 +1,52 @@
/*
The MIT License (MIT)
Copyright (c) 2016 British Broadcasting Corporation.
This software is provided by Lancaster University by arrangement with the BBC.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include "MicroBit.h"
#include "veml6070.h"
MicroBit uBit;
MicroBitI2C i2c(I2C_SDA0,I2C_SCL0);
int main()
{
// Initialise the micro:bit runtime.
uBit.init();
veml6070 veml(&uBit,&i2c);
uint16_t uv = 0;
while(true)
{
veml.sensor_read(&uv);
ManagedString display = "UV:" + ManagedString(uv);
uBit.display.scroll(display.toCharArray());
uBit.sleep(1000);
}
release_fiber();
}

@ -0,0 +1,105 @@
/****************************************************************************
* extdrv/veml6070_uv_sensor.c
*
* VEML6070 I2C UV sensor driver
*
* Copyright 2016 Nathael Pajani <nathael.pajani@ed3l.fr>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************** */
#include "veml6070.h"
/* Sensor config
* Performs default configuration of the UV sensor.
*/
#define CONF_BUF_SIZE 1
veml6070::veml6070(MicroBit* uB, MicroBitI2C* uBi2c, uint8_t addr): uBit(uB), i2c(uBi2c), address(addr)
{
int ret = 0;
char cmd_buf[CONF_BUF_SIZE] = { (VEML6070_NO_ACK | VEML6070_INTEG_1T | VEML6070_ENABLE), };
if (probe_sensor() != 1) {
uBit->display.scroll("VEML6070: No Device");
}
uBit->sleep(1);
ret = i2c->write(address,cmd_buf, CONF_BUF_SIZE);
if (ret != MICROBIT_OK) {
probe_ok = 0;
uBit->display.scroll("VEML6070: Conf Error");
}
}
/* Check the sensor presence, return 1 if found */
int veml6070::probe_sensor()
{
char dropped;
/* Did we already probe the sensor ? */
if (probe_ok != 1) {
int status = i2c->read(address, &dropped, 1);
if (status == MICROBIT_OK)
probe_ok = 1;
}
return probe_ok;
}
/* UV Read
* Performs a read of the uv data from the sensor.
* 'uv_raw': integer addresses for conversion result.
* Return value(s):
* Upon successfull completion, returns 0 and the luminosity read is placed in the
* provided integer(s). On error, returns a negative integer (-1 NO Device, -2 Invalid uv_raw input value, -3 read error)
*
*/
int veml6070::sensor_read(uint16_t* uv_raw)
{
int ret = 0;
uint8_t data = 0;
if (probe_ok != 1) {
if (probe_sensor() != 1) {
uBit->display.scroll("VEML6070: No Device");
return -1;
}
uBit->sleep(1);
}
if (uv_raw == NULL) {
return -2;
}
/* Start by reading MSB */
ret = i2c->read(address+2, (char*)&data, 1);
if (ret != MICROBIT_OK) {
probe_ok = 0;
return ret;
}
*uv_raw = ((uint16_t)data << 8) & 0xFF00;
uBit->sleep(1);
/* And read LSB */
ret = i2c->read(address, (char*)&data, 1);
if (ret != MICROBIT_OK) {
return ret;
}
*uv_raw |= (uint16_t)data & 0x00FF;
return 0;
}

@ -0,0 +1,85 @@
/****************************************************************************
* veml6070.h
*
* VEML6070 I2C UV sensor driver
*
* Copyright 2016 Nathael Pajani <nathael.pajani@ed3l.fr>
* Copyright 2022 Anthony Chomienne <anthony@mob-dev.fr>
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************** */
/**
* This code is mainly an adaptation of code provided by Techno-Innov (Nathael Pajani <nathael.pajani@ed3l.fr>)
* www.techno-innov.fr
* http://git.techno-innov.fr
*/
#ifndef VEML6070_H
#define VEML6070_H
#include <cstdint>
#include "MicroBit.h"
#define VEML6070_ADDR 0x70
/* Defines for command byte */
#define VEML6070_ACK (1 << 5)
#define VEML6070_ACK_THD_102 (0)
#define VEML6070_ACK_THD_145 (1 << 4)
#define VEML6070_NO_ACK (0)
#define VEML6070_INTEG_05T (0x00 << 2)
#define VEML6070_INTEG_1T (0x01 << 2)
#define VEML6070_INTEG_2T (0x02 << 2)
#define VEML6070_INTEG_4T (0x03 << 2)
#define VEML6070_DISABLE (1 << 0)
#define VEML6070_ENABLE (0)
class veml6070 {
public:
/* Sensor config
* Performs default configuration of the UV sensor.
*/
veml6070(MicroBit* uB, MicroBitI2C* i2c, uint8_t address = VEML6070_ADDR);
/* Check the sensor presence, return 1 if found */
int probe_sensor();
/* UV Read
* Performs a read of the uv data from the sensor.
* 'uv_raw': integer addresses for conversion result.
* Return value(s):
* Upon successfull completion, returns 0 and the value read is placed in the provided integer.
* On error, returns a negative integer equivalent to errors from glibc.
*/
int sensor_read(uint16_t* uv_raw);
private:
MicroBit* uBit;
MicroBitI2C* i2c;
uint8_t address;
uint8_t probe_ok;
};
#endif /* VEML6070_H */
Loading…
Cancel
Save