AskSin++
Max44009.h
1 //
2 // Main Parts are from
3 // AUTHOR: Rob dot Tillaart at gmail dot com
4 // VERSION: 0.1.9
5 // PURPOSE: library for MAX44009 lux sensor Arduino
6 // HISTORY: See Max440099.cpp
7 //
8 // Released to the public domain
9 //
10 //- -----------------------------------------------------------------------------------------------------------------------
11 // AskSin++
12 // 2018-04-03 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
13 // 2018-07-04 jp112sdl Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
14 //- -----------------------------------------------------------------------------------------------------------------------
15 
16 #ifndef __SENSORS_MAX44009_h__
17 #define __SENSORS_MAX44009_h__
18 
19 #include <Sensors.h>
20 #include <Wire.h>
21 
22 #if defined(ARDUINO) && ARDUINO >= 100
23 #include "Arduino.h"
24 #else
25 #include "WProgram.h"
26 #endif
27 
28 // REGISTERS
29 #define MAX44009_CONFIGURATION 0x02
30 #define MAX44009_LUX_READING_HIGH 0x03
31 #define MAX44009_LUX_READING_LOW 0x04
32 
33 // CONFIGURATION MASKS
34 #define MAX44009_CFG_CONTINUOUS 0x80
35 #define MAX44009_CFG_MANUAL 0x40
36 #define MAX44009_CFG_CDR 0x08
37 #define MAX44009_CFG_TIMER 0x07
38 
39 namespace as {
40 
41 template <byte ADDRESS=0x4a, uint8_t CDR=0x01, uint8_t TIM=0x03>
42 class MAX44009 : public Brightness {
43 public:
44  void init () {
45  Wire.begin();
46  getLux();
47  uint8_t err = getError();
48  if (err == 0) {
49  _present = true;
50  setContinuousMode();
51  setManualMode(CDR, TIM);
52  DPRINTLN(F("MAX44009 Sensor OK"));
53  } else {
54  DPRINT(F("MAX44009 Sensor Error "));DDECLN(err);
55  }
56  }
57  void measure (__attribute__((unused)) bool async=false) {
58  if( present() == true ) {
59  _brightness = getLux() / 100;
60  uint8_t err = getError();
61  if (err != 0) {
62  //DPRINT("MAX44009 Sensor Error ");DDECLN(err);
63  }
64  }
65  }
66 private:
67  uint8_t _error;
68  uint8_t _data;
69  uint32_t getLux(void) {
70  uint8_t dhi = read(MAX44009_LUX_READING_HIGH);
71  uint8_t dlo = read(MAX44009_LUX_READING_LOW);
72  uint8_t e = dhi >> 4;
73  uint32_t m = ((dhi & 0x0F) << 4) + (dlo & 0x0F);
74  m <<= e;
75  uint32_t val = m * 45;
76  return val;
77  }
78  uint8_t getError() {
79  uint8_t e = _error;
80  _error = 0;
81  return e;
82  }
83  void setConfiguration(const uint8_t value) {
84  write(MAX44009_CONFIGURATION, value);
85  }
86 
87  uint8_t getConfiguration() {
88  return read(MAX44009_CONFIGURATION);
89  }
90 
91  void setAutomaticMode() {
92  uint8_t config = read(MAX44009_CONFIGURATION);
93  config &= ~MAX44009_CFG_CONTINUOUS; // off
94  config &= ~MAX44009_CFG_MANUAL; // off
95  write(MAX44009_CONFIGURATION, config);
96  }
97 
98  void setContinuousMode() {
99  uint8_t config = read(MAX44009_CONFIGURATION);
100  config |= MAX44009_CFG_CONTINUOUS; // on
101  config &= ~MAX44009_CFG_MANUAL; // off
102  write(MAX44009_CONFIGURATION, config);
103  }
104 
105  void setManualMode(uint8_t _CDR, uint8_t _TIM) {
106  if (_CDR != 0) _CDR = 1;
107  if (_TIM > 7) _TIM = 7;
108  uint8_t config = read(MAX44009_CONFIGURATION);
109  config &= ~MAX44009_CFG_CONTINUOUS; // off
110  config |= MAX44009_CFG_MANUAL; // on
111  config &= 0xF0; // clear CDR & TIM bits
112  config |= _CDR << 3 | TIM;
113  write(MAX44009_CONFIGURATION, config);
114  }
115 
116  uint8_t read(uint8_t reg) {
117  Wire.beginTransmission(ADDRESS);
118  Wire.write(reg);
119  _error = Wire.endTransmission();
120  if (_error != 0) {
121  return _data; // last value
122  }
123  if (Wire.requestFrom(ADDRESS, (uint8_t) 1) != 1) {
124  _error = 10;
125  return _data; // last value
126  }
127 #if (ARDUINO < 100)
128  _data = Wire.receive();
129 #else
130  _data = Wire.read();
131 #endif
132  return _data;
133  }
134 
135  void write(uint8_t reg, uint8_t value) {
136  Wire.beginTransmission(ADDRESS);
137  Wire.write(reg);
138  Wire.write(value);
139  _error = Wire.endTransmission();
140  }
141 };
142 
143 }
144 
145 #endif
as::Brightness
Definition: Sensors.h:21
as::MAX44009
Definition: Max44009.h:42