Skip to content

Commit

Permalink
max44009 library draft
Browse files Browse the repository at this point in the history
  • Loading branch information
luk6xff committed Sep 3, 2020
1 parent df239af commit 6603221
Show file tree
Hide file tree
Showing 19 changed files with 474 additions and 21 deletions.
9 changes: 9 additions & 0 deletions esp32-app/lib/MAX44009/lib_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Import('env')
from os.path import join, realpath

# private library flags
for item in env.get("CPPDEFINES", []):
if isinstance(item, tuple) and item[0] == "PLATFORM":
env.Append(CPPPATH=[realpath(join("platform", item[1]))])
env.Replace(SRC_FILTER=["+<*>", "-<platform>", "+<%s>" % join("platform", item[1])])
break
25 changes: 25 additions & 0 deletions esp32-app/lib/MAX44009/library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "MAX44009",
"description": "MAX44009 ambient light sensor library",
"platforms": "*",
"frameworks": [
"mbed",
"arduino"
],
"keywords": [
"MAX44009",
"Light Sensor"
],
"version": "0.1.0",
"authors": {
"name": "Lukasz Uszko",
"url": "https://github.com/luk6xff/DevLibs/tree/master/MAX44009"
},
"repository": {
"type": "git",
"url": "https://github.com/luk6xff/DevLibs/tree/master/MAX44009"
},
"build": {
"extraScript": "lib_build.py"
}
}
12 changes: 12 additions & 0 deletions esp32-app/lib/MAX44009/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name=MAX44009
version=0.1.0
author=Lukasz Uszko
maintainer=luk6xff <[email protected]>
sentence=MAX44009 ambient light sensor library
paragraph=
category=LIGHT
url=https://github.com/luk6xff/DevLibs/tree/master/MAX44009
architectures=*
includes=max44009.h,max44009.c,platform/arduino/max44009-arduino.h,platform/arduino/max44009-arduino.cpp
depends=
license=MIT
34 changes: 34 additions & 0 deletions esp32-app/lib/MAX44009/max44009.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "max44009.h"


//------------------------------------------------------------------------------
bool max44009_init(max44009* const dev)
{
if (!dev)
{
return false;
}
return true;
}

//------------------------------------------------------------------------------
bool max44009_read_lux_intenity(const max44009* const dev, float* lux)
{
uint8_t exponent;
uint8_t mantissa;

uint8_t high_and_low_byte[2];
uint8_t reg_addr = 0x03 ;
if (!max44009_read(dev, reg_addr, &high_and_low_byte, 2))
{
lux = -1;
return false;
}

mantissa = (((high_and_low_byte[0] & 0x0F) <<4) | (high_and_low_byte[1] & 0x0F));
exponent = (high_and_low_byte[0] & 0xF0) >> 4;
*lux = (float)(pow(2, (float)exponent) * mantissa * 0.045);
return true;
}

//-----------------------------------------------------------------------------
84 changes: 84 additions & 0 deletions esp32-app/lib/MAX44009/max44009.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* @brief: MAX44009 ambient light sensor library
* @author: luk6xff
* @email: [email protected]
* @date: 2020-09-02
* @license: MIT
*/

#ifndef __MAX44009_H__
#define __MAX44009_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>
#include <string.h>


#define MAX44009_DEFUALT_I2C_ADDRESS 0x97 //0b10011011 - A0 PIN is conected to VDD

/**
* @brief max44009 dev object
*/
typedef struct
{
uint8_t i2c_addr; // I2C memory address
void* platform_dev;
} max44009;

/**
* @brief Initialize eeprom.
*
* @param dev max44009 device object
* @return True on success, false otherwise
*/
bool max44009_init(max44009* const dev);

/**
* @brief Read ambient light luminance.
*
* @param dev max44009 device object
* @param lux Ambient light luminance in lux [lx]
* @return True on success, false otherwise
*/
bool max44009_read_lux_intenity(const max44009* const dev, float* lux);


//-----------------------------------------------------------------------------
// @brief HW dependent functions - must be defined for each platform
//-----------------------------------------------------------------------------

/**
* @brief Write bytes into max44009 device
*
* @param dev max44009 device object
* @param reg_addr Register address.
* @param buf Data to be written
* @param buf_size Number of bytes to be written.
* @return True on success, false otherwise
*/
extern bool max44009_write(const max44009* const dev, uint8_t reg_addr,
const uint8_t* buf, size_t buf_size);

/**
* @brief Read bytes from max44009 device
*
* @param dev max44009 device object
* @param reg_addr Register address.
* @param[in] buf Buffer to fill with read bytes.
* @param buf_size Number of bytes to read (32 bytes max).
* @retval Status value
*/
extern bool max44009_read(const max44009* const dev, uint8_t reg_addr,
uint8_t* buf, size_t buf_size);


#ifdef __cplusplus
}
#endif

#endif /* __MAX44009_H__ */

53 changes: 53 additions & 0 deletions esp32-app/lib/MAX44009/platform/arduino/max44009-arduino.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @brief: MAX44009 ambient light sensor library
* @author: luk6xff
* @email: [email protected]
* @date: 2020-09-02
* @license: MIT
*/

#include "max44009-arduino.h"

//------------------------------------------------------------------------------
bool max44009_arduino_init(max44009* const dev, max44009_arduino* const arduino_dev)
{
arduino_dev->i2c->begin(dev->i2c_addr);
arduino_dev->i2c->setClock(400000);
dev->platform_dev = arduino_dev;
return max44009_init(dev);
}
//-----------------------------------------------------------------------------
bool max44009_write(const max44009* const dev, uint8_t reg_addr,
const uint8_t* buf, size_t buf_size)
{
max44009_arduino* const pd = (max44009_arduino*)dev->platform_dev;
pd->i2c->beginTransmission(dev->i2c_addr);
pd->i2c->write(reg_addr);
for (uint8_t i = 0; i < buf_size; i++)
{
pd->i2c->write((uint8_t) buf[i]);
}
return pd->i2c->endTransmission() == 0; // true on success
}

//-----------------------------------------------------------------------------
bool max44009_read(const max44009* const dev, uint8_t reg_addr,
uint8_t* buf, size_t buf_size)
{
const size_t timeout_ms = 1000; // 1 second timeout
size_t recv_data_cntr = 0;
max44009_arduino* const pd = (max44009_arduino*)dev->platform_dev;

pd->i2c->beginTransmission(dev->i2c_addr);
// Start transmission by write to a given register
pd->i2c->write(reg_addr);
// Read data
pd->i2c->requestFrom(dev->i2c_addr, buf_size);
const size_t t1 = millis();
for (; pd->i2c->available() && (timeout_ms == 0 || (millis() - t1) < timeout_ms); recv_data_cntr++)
{
buf[recv_data_cntr ] = pd->i2c->read();
}
return pd->i2c->endTransmission() == 0 && recv_data_cntr == buf_size;
}
//-----------------------------------------------------------------------------
30 changes: 30 additions & 0 deletions esp32-app/lib/MAX44009/platform/arduino/max44009-arduino.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @brief: MAX44009 ambient light sensor library
* @author: luk6xff
* @email: [email protected]
* @date: 2020-09-02
* @license: MIT
*/

#ifndef __MAX44009_ARDUINO_H__
#define __MAX44009_ARDUINO_H__

#include "../../max44009.h"
#include <Arduino.h>
#include <Wire.h>

/**
* @brief MAX44009 arduino specific dev object
*/
typedef struct
{
TwoWire* i2c;
} max44009_arduino;

/**
* @brief Initialize MAX44009 device with ARDUINO dependent parameters.
*/
bool max44009_arduino_init(max44009* const dev, max44009_arduino* const arduino_dev);


#endif // __MAX44009_ARDUINO_H__
49 changes: 49 additions & 0 deletions esp32-app/lib/MAX44009/platform/mbed/max44009-mbed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "max44009-mbed.h"




//-----------------------------------------------------------------------------
bool max44009_mbed_init(max44009* const dev, max44009_mbed* const mbed_dev)
{
dev->platform_dev = mbed_dev;
return max44009_init(dev);
}

//-----------------------------------------------------------------------------
bool max44009_write(const max44009* const dev, uint8_t reg_addr,
const uint8_t* buf, size_t buf_size)
{
const max44009_mbed* const pd = (max44009_mbed*)dev->platform_dev;

int ack = pd->i2c->write((int)dev->addr, (char*)&reg_addr, 1, true);
ack = pd->i2c->write((int)dev->addr, (char*)buf, buf_size);
if (ack != 0)
{
return false;
}
return true;
}

//-----------------------------------------------------------------------------
bool max44009_read(const max44009* const dev, uint8_t reg_addr,
uint8_t* buf, size_t buf_size)
{
const max44009_mbed* const pd = (max44009_mbed*)dev->platform_dev;

// Write addr
int ack = pd->i2c->write((int)dev->addr, (char*)&reg_addr, 1, true);
if (ack != 0)
{
return false;
}
// Sequential Read
ack = pd->i2c->read(dev->addr, (char*)buf, buf_size);
if (ack != 0)
{
return false;
}
return true;
}

//-----------------------------------------------------------------------------
30 changes: 30 additions & 0 deletions esp32-app/lib/MAX44009/platform/mbed/max44009-mbed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @brief: MAX44009 ambient light sensor library
* @author: luk6xff
* @email: [email protected]
* @date: 2020-09-02
* @license: MIT
*/

#ifndef __MAX44009_MBED_H__
#define __MAX44009_MBED_H__

#include "mbed.h"
#include "../../max44009.h"


typedef struct
{
I2C* i2c; // I2C Interface
DigitalOut* wp; // AT24Cxx Write protection pin
} max44009_mbed;


/**
* @brief Initialize MAX44009 device with MBED dependent parameters.
*/
bool max44009_mbed_init(max44009* const dev, max44009_mbed* const mbed_dev);


#endif /*__MAX44009_MBED_H__ */

Loading

0 comments on commit 6603221

Please sign in to comment.