implemented basic functions for VEML6040

This commit is contained in:
hhaupt 2024-05-02 01:27:43 +02:00
parent dbe97dc94c
commit a5572b20b7
5 changed files with 137 additions and 76 deletions

View File

@ -1,7 +1,6 @@
#include <Adafruit_NeoPixel.h>
#include <Dezibot.h> #include <Dezibot.h>
#define GPIO_LED 48
Dezibot dezibot = Dezibot(); Dezibot dezibot = Dezibot();

View File

@ -1,5 +0,0 @@
#include "Dezibot.h"
int main(){
return 0;
}

View File

@ -10,73 +10,6 @@
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define GPIO_LED 48
void Dezibot::begin(void) { void Dezibot::begin(void) {
Adafruit_NeoPixel ledStrip = Adafruit_NeoPixel(3, GPIO_LED, NEO_GRB + NEO_KHZ800); colorDetection = ColorDetection();
Wire.begin(1, 2);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println("SSD1306 allocation failed");
for (;;); // Don't proceed, loop forever
}
display.display();
vTaskDelay(2000);
display.clearDisplay();
vTaskDelay(2000);
// Draw a single pixel in white
display.drawPixel(10, 10, SSD1306_WHITE);
// Show the display buffer on the screen. You MUST call display() after
// drawing commands to make them visible on screen!
display.display();
vTaskDelay(2000);
Serial.begin(9600);
Serial.println("start");
vTaskDelay(1000);
while (1) {
/* Blink off (output low) */
ledStrip.setPixelColor(1, ledStrip.Color(100, 100, 100));
ledStrip.show(); // Aktualisiere die Farbe des Pixels
vTaskDelay(1000);
/* Blink on (output high) */
ledStrip.setPixelColor(1, ledStrip.Color(0, 0, 0));
ledStrip.show(); // Aktualisiere die Farbe des Pixels
vTaskDelay(1000);
struct timeval tv_now;
gettimeofday(&tv_now, NULL);
int64_t time_us = (int64_t) tv_now.tv_sec * 1000000L + (int64_t) tv_now.tv_usec;
Serial.println(time_us);
display.clearDisplay();
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 0);
display.println(F("scroll"));
display.display(); // Show initial text
vTaskDelay(1000);
}
} }

View File

@ -0,0 +1,84 @@
#include "ColorDetection.h"
void ColorDetection::begin(void){
Wire.begin(I2C_MASTER_SDA_IO,I2C_MASTER_SCL_IO);
ColorDetection::configure(VEML_CONFIG{.mode = AUTO,.enabled = true,.exposureTime=MS40});
};
void ColorDetection::configure(VEML_CONFIG config){
uint8_t configRegister = 0;
switch(config.exposureTime)
{
case MS40:
configRegister = 0x00;break;
case MS80:
configRegister = 0x01;break;
case MS160:
configRegister = 0x02;break;
case MS320:
configRegister = 0x03;break;
case MS640:
configRegister = 0x04;break;
case MS1280:
configRegister = 0x05;break;
}
configRegister = configRegister << 4;
if(config.mode == MANUAL)
{
configRegister = configRegister | (0x01<<1);
}
if(!config.enabled)
{
configRegister = configRegister | 1;
}
ColorDetection::writeDoubleRegister(CMD_CONFIG,(uint16_t)configRegister);
};
uint16_t ColorDetection::getColorValue(color color){
switch(color)
{
case RED:
return readDoubleRegister(REG_RED);
break;
case GREEN:
return readDoubleRegister(REG_GREEN);
break;
case BLUE:
return readDoubleRegister(REG_BLUE);
break;
case WHITE:
return readDoubleRegister(REG_WHITE);
break;
default:
Serial.println("Color is not supported by the sensor");
return 0;
}
};
uint16_t ColorDetection::readDoubleRegister(uint8_t regAddr){
uint16_t result = 0;
Wire.beginTransmission(VEML_ADDR);
Wire.write(regAddr);
if(Wire.endTransmission() != 0){
Serial.printf("Reading Register %d failed",regAddr);
}
Wire.requestFrom(VEML_ADDR,2);
uint8_t offset = 0;
while(Wire.available()){
result = result << 8;
result = result | (Wire.read()<<offset);
offset = offset + 8;
}
return result;
};
void ColorDetection::writeDoubleRegister(uint8_t regAddr, uint16_t data){
//erst low dann high
Wire.beginTransmission(VEML_ADDR);
Wire.write(regAddr);
Wire.write((uint8_t)(data&0x00FF));
Wire.write((uint8_t)((data>>8)&0x00FF));
if(Wire.endTransmission() != 0){
Serial.printf("Reading Register %d failed",regAddr);
}
};

View File

@ -1,7 +1,57 @@
#ifndef ColorDetection_h #ifndef ColorDetection_h
#define ColorDetection_h #define ColorDetection_h
#include <stdint.h>
#include <Wire.h>
#include <Arduino.h>
//Definitions for I2c
#define I2C_MASTER_SCL_IO 2 /*!< GPIO number used for I2C master clock */
#define I2C_MASTER_SDA_IO 1 /*!< GPIO number used for I2C master data */
//Chipadress of the VEML6040
#define VEML_ADDR 0x10 /*!< Slave address of the MPU9250 sensor */
//CMDCodes for communicate with the VEML6040
#define CMD_CONFIG 0x00
#define REG_RED 0x08
#define REG_GREEN 0x09
#define REG_BLUE 0x0A
#define REG_WHITE 0x0B
enum duration{
MS40,
MS80,
MS160,
MS320,
MS640,
MS1280
};
enum vemlMode{
AUTO,
MANUAL
};
struct VEML_CONFIG{
vemlMode mode;
bool enabled;
duration exposureTime;
};
enum color{
RED,
GREEN,
BLUE,
WHITE
};
class ColorDetection{ class ColorDetection{
public:
void begin(void);
void configure(VEML_CONFIG config);
uint16_t getColorValue(color color);
protected:
uint16_t readDoubleRegister(uint8_t regAddr);
void writeDoubleRegister(uint8_t regAddr, uint16_t data);
}; };
#endif //ColorDetection_h #endif //ColorDetection_h