Can someone help me on this low power library thing? I cant seems to understand which and where to put the correct code. I try follow the example sketch but it just wonk work, here is my full code without lowpower. I want it to sleep if no activity and wake after pin 2 is press.
static const unsigned char PROGMEM FLM [] = {
0x00, 0x00, 0x00, 0x07, 0xFF, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xFF,
0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x06, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x03, 0xFE, 0x0E, 0x00, 0x00, 0x70, 0x03,
0xFE, 0x00, 0x00, 0x03, 0xFE, 0x0E, 0x00, 0x00, 0x70, 0x03, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x0C,
0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xDE, 0xFF, 0xFF, 0x7B, 0xF8, 0x00, 0xFC,
0x00, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0x1F, 0xF0, 0x00, 0x00, 0x3F, 0xF8, 0x00, 0x00, 0x00, 0x03, 0x1F, 0xF0,
0x00, 0x01, 0xFF, 0xFB, 0x80, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xF9, 0xE0, 0x00,
0x00, 0x03, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xF9, 0xF0, 0x00, 0x00, 0x03, 0xFF, 0xFF, 0xFE, 0x13,
0xFF, 0xF9, 0xF8, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x3C, 0xFF, 0xFD, 0xFE, 0x00, 0x00, 0x03,
0x80, 0x00, 0x00, 0x7E, 0x3F, 0xFD, 0xFF, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0xFF, 0x9F, 0xFD,
0xFF, 0x00, 0x00, 0x03, 0x80, 0x00, 0x01, 0xFF, 0xC7, 0xFD, 0xFF, 0x80, 0x00, 0x03, 0x80, 0x00,
0x03, 0xFF, 0xF3, 0xFD, 0xFF, 0xC0, 0x00, 0x03, 0x80, 0x00, 0x03, 0xFF, 0xF8, 0xFC, 0xFF, 0xC0,
0x00, 0x03, 0x80, 0x00, 0x07, 0xFF, 0xFE, 0x7C, 0xFF, 0xE0, 0x00, 0x03, 0x80, 0x00, 0x07, 0xFF,
0xFE, 0x1C, 0xFF, 0xC0, 0x00, 0x03, 0x80, 0x00, 0x0F, 0xFF, 0xE0, 0x0C, 0xFF, 0x90, 0x00, 0x03,
0x80, 0x00, 0x0F, 0xFF, 0x00, 0x06, 0xFF, 0x30, 0x00, 0x03, 0x80, 0x00, 0x0F, 0xF8, 0x00, 0x00,
0xFE, 0x70, 0x00, 0x03, 0x80, 0x00, 0x0F, 0xC3, 0x80, 0x00, 0xFC, 0xF0, 0x00, 0x03, 0x80, 0x00,
0x1E, 0x1F, 0x00, 0x00, 0xF9, 0xF0, 0x00, 0x03, 0x80, 0x00, 0x10, 0xFF, 0x00, 0x00, 0xF3, 0xF8,
0x00, 0x03, 0x80, 0x00, 0x03, 0xFF, 0x00, 0x00, 0xE7, 0xF8, 0x00, 0x03, 0x80, 0x00, 0x1F, 0xFE,
0x00, 0x00, 0xCF, 0xF8, 0x00, 0x03, 0x80, 0x00, 0x1F, 0xFE, 0x00, 0x00, 0x9F, 0xF8, 0x00, 0x03,
0x80, 0x00, 0x1F, 0xFC, 0x00, 0x00, 0x3F, 0xF8, 0x00, 0x03, 0x80, 0x00, 0x1F, 0xFD, 0x00, 0x00,
0x7F, 0xF0, 0x00, 0x03, 0x80, 0x00, 0x1F, 0xFD, 0x80, 0x00, 0xFF, 0xF0, 0x00, 0x03, 0x80, 0x00,
0x0F, 0xF9, 0x80, 0x01, 0xFF, 0xF0, 0x00, 0x03, 0x80, 0x00, 0x0F, 0xFB, 0xC0, 0x03, 0xFF, 0xF0,
0x00, 0x03, 0x80, 0x00, 0x0F, 0xF3, 0xC0, 0x0F, 0xFF, 0xF0, 0x00, 0x03, 0x80, 0x00, 0x07, 0xF3,
0xE0, 0x03, 0xFF, 0xE0, 0x00, 0x03, 0x80, 0x00, 0x07, 0xF7, 0xF7, 0xE0, 0x01, 0xE0, 0x00, 0x03,
0x80, 0x00, 0x07, 0xE7, 0xF3, 0xFF, 0xE0, 0x00, 0x00, 0x03, 0x80, 0x00, 0x03, 0xE7, 0xFB, 0xFF,
0xFF, 0xC0, 0x5C, 0x03, 0x80, 0x00, 0x01, 0xEF, 0xF9, 0xFF, 0xFF, 0x80, 0x3E, 0x03, 0x80, 0x00,
0x01, 0xCF, 0xFC, 0xFF, 0xFF, 0x80, 0x3E, 0x03, 0x80, 0x00, 0x00, 0xCF, 0xFE, 0xFF, 0xFF, 0x00,
0x3C, 0x03, 0x80, 0x00, 0x00, 0x5F, 0xFE, 0x7F, 0xFE, 0x00, 0x18, 0x03, 0x80, 0x00, 0x00, 0x1F,
0xFF, 0x3F, 0xFC, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x1F, 0xFF, 0x3F, 0xF8, 0x00, 0x00, 0x03,
0x80, 0x00, 0x00, 0x07, 0xFF, 0x9F, 0xE0, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x01, 0xFF, 0xCF,
0x80, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x7F, 0xCE, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00,
0x00, 0x00, 0x03, 0xE0, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x03, 0x80, 0x07, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x3E, 0xCC, 0xF9,
0x99, 0xF0, 0x20, 0xE7, 0x80, 0x00, 0x00, 0x30, 0x58, 0x89, 0x9B, 0x10, 0x20, 0xE7, 0xFF, 0xFF,
0xFE, 0x30, 0x70, 0x99, 0x9B, 0x00, 0x20, 0xEF, 0xFF, 0xFF, 0xFE, 0x3C, 0x30, 0xF1, 0x99, 0xF0,
0x20, 0xBB, 0x00, 0x00, 0x00, 0x30, 0x30, 0xB1, 0x98, 0x10, 0x20, 0x9B, 0x00, 0x00, 0x00, 0x30,
0x30, 0x99, 0x9B, 0x30, 0x30, 0xDB, 0x00, 0x00, 0x00, 0x30, 0x30, 0x98, 0xF1, 0xE0, 0x3C, 0xD3 };
static const unsigned char PROGMEM _full [] =
{ 0x7F, 0xE0, 0x40, 0x20, 0x5F, 0xB8, 0x5F, 0xB8, 0x5F, 0xB8, 0x5F, 0xB8, 0x40, 0x20, 0x7F, 0xE0 };
static const unsigned char PROGMEM _threequarters [] =
{ 0x7F, 0xE0, 0x40, 0x20, 0x5C, 0x38, 0x5E, 0x38, 0x5E, 0x38, 0x5F, 0x38, 0x40, 0x20, 0x7F, 0xE0 };
static const unsigned char PROGMEM _half [] =
{ 0x7F, 0xE0, 0x40, 0x20, 0x58, 0x38, 0x58, 0x38, 0x5C, 0x38, 0x5C, 0x38, 0x40, 0x20, 0x7F, 0xE0 };
static const unsigned char PROGMEM _low [] =
{ 0x7F, 0xE0, 0x40, 0x20, 0x50, 0x38, 0x50, 0x38, 0x50, 0x38, 0x50, 0x38, 0x40, 0x20, 0x7F, 0xE0 };
static const unsigned char PROGMEM _empty [] =
{ 0x7F, 0xE0, 0x40, 0x20, 0x40, 0x38, 0x40, 0x38, 0x40, 0x38, 0x40, 0x38, 0x40, 0x20, 0x7F, 0xE0 };
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <BH1750.h>
#include <Adafruit_TCS34725.h>
#include <EEPROM.h>
#include <Battery.h>
// REPLACE 1 //////////////////////////////
/* 4 Pin Oled display
*
* VCC -> 3.3V
* GND -> GND
* SDA -> pin A4
* SCL -> pin A5
*/
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
// END REPLACE 1 //////////////////////////
BH1750 lightMeter;
uint8_t Rp = 2; // Metering button pin
uint8_t Bn1p = 3; // + increment button pin
uint8_t Bn2p = 4; // - increment button pin
#define DomeMultiplier 2.17 // define 1.0 if no white translucide Dome on the sensor.
#define MaxISOIndex 57
#define MaxApertureIndex 27 // max 70 (default f1.0 ~ f22.0) editable
#define MaxTimeIndex 80
#define MaxFlashMeteringTime 3000 // flash metering in milisecond (default 3 seconds)
/* CONSTANT FOR WHITE BALANCE /////// */
#define TCS34725_R_Coef 0.136
#define TCS34725_G_Coef 1.000
#define TCS34725_B_Coef -0.444
#define TCS34725_GA 1.0
#define TCS34725_DF 310.0
#define TCS34725_CT_Coef 3810.0
#define TCS34725_CT_Offset 1391.0
//#define C_const 129
float lux; // Lux value from BH1750
float ct; // Color Temp value from TCS34725
boolean Overflow = 0; // Sensor got Saturated and Display "Overflow"
float ISOND;
boolean ISOmode = 0;
boolean Bn1; // + increment button state
boolean Bn2; // - increment button state
boolean R; // Metering button state
boolean mainScreen = false;
boolean ISOset = false;
// EEPROM for memory recording
#define ISOIndexAddr 1
#define apertureIndexAddr 2
#define modeIndexAddr 3
#define T_expIndexAddr 4
#define meteringModeAddr 5
#define ndIndexAddr 6
#define defaultApertureIndex 12
#define defaultISOIndex 11
#define defaultModeIndex 0
#define defaultT_expIndex 19
uint8_t ISOIndex = EEPROM.read(ISOIndexAddr);
uint8_t apertureIndex = EEPROM.read(apertureIndexAddr);
uint8_t T_expIndex = EEPROM.read(T_expIndexAddr);
uint8_t modeIndex = EEPROM.read(modeIndexAddr);
uint8_t meteringMode = EEPROM.read(meteringModeAddr);
uint8_t ndIndex = EEPROM.read(ndIndexAddr);
Battery battery(3000, 4200, A0); // MinVoltage / MaxVoltage / BattPin
class ColorSensor {
public:
void getColorTemp();
uint16_t r, g, b, c, ir;
uint16_t ct, lux, r_comp, g_comp, b_comp;
float cpl;
} rgb_sensor;
void ColorSensor::getColorTemp() {
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_101MS, TCS34725_GAIN_1X);
tcs.getRawData(&r, &g, &b, &c);
//lux = tcs.calculateLux(r, g, b);
// DN40 calculations
ir = (r + g + b > c) ? (r + g + b - c) / 2 : 0;
r_comp = r - ir;
g_comp = g - ir;
b_comp = b - ir;
//c_comp = c - ir;
cpl = (700) / (TCS34725_GA * TCS34725_DF);
lux = (TCS34725_R_Coef * float(r_comp) + TCS34725_G_Coef * float(g_comp) + TCS34725_B_Coef * float(b_comp)) / cpl;
ct = TCS34725_CT_Coef * float(b_comp) / float(r_comp) + TCS34725_CT_Offset;
//ct = ct*DomeSum;
}
void setup() {
pinMode(Bn1p, INPUT_PULLUP);
pinMode(Bn2p, INPUT_PULLUP);
pinMode(Rp, INPUT_PULLUP);
battery.begin(5000, 1.0);
Wire.begin();
lightMeter.begin(BH1750::ONE_TIME_HIGH_RES_MODE_2);
//lightMeter.begin(BH1750::ONE_TIME_LOW_RES_MODE); // for low resolution but 16ms light measurement time.
// REPLACE 2 ////////////////////////////////
/* 4 pin 0.96 Oled Display */
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
// END REPLACE 2 ////////////////////////////
display.clearDisplay();
display.drawBitmap(24, 0, FLM, 80, 64, 1);
display.display();
display.clearDisplay();
delay(3000);
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(40, 1);
display.println("FYRUS LM");
display.drawLine(31, 12, 95, 12, WHITE);
display.drawLine(31, 51, 95, 51, WHITE);
if (battery.level() > 80 && battery.level() < 100) {
display.drawBitmap(44, 22, _full, 16, 8, WHITE);
} else if (battery.level() > 60 && battery.level() < 80) {
display.drawBitmap(44, 22, _threequarters, 16, 8, WHITE);
} else if (battery.level() > 40 && battery.level() < 60) {
display.drawBitmap(44, 22, _half, 16, 8, WHITE);
} else if (battery.level() > 20 && battery.level() < 40) {
display.drawBitmap(44, 22, _low, 16, 8, WHITE);
} else {
display.setCursor(29, 29);
display.print(F("Low Battery!"));
display.setCursor(31 ,57);
display.println("NEED CHARGE");
}
if (battery.level() > 20 && battery.level() < 100) {
display.setCursor(61, 22);
display.print(battery.level());
display.println(F("%"));
display.setCursor(45, 34);
display.print(battery.voltage());
display.print(F("mV"));
display.setCursor(25, 57);
display.println("<<< READY >>>");
}
display.display();
delay(2000);
// IF NO MEMORY WAS RECORDED BEFORE, START WITH THIS VALUES otherwise it will read "255"
if (apertureIndex > MaxApertureIndex) {
apertureIndex = defaultApertureIndex;
}
if (ISOIndex > MaxISOIndex) {
ISOIndex = defaultISOIndex;
}
if (T_expIndex > MaxTimeIndex) {
T_expIndex = defaultT_expIndex;
}
if (modeIndex < 0 || modeIndex > 1) {
// Aperture priority. Calculating shutter speed.
modeIndex = 0;
}
if (meteringMode > 1) {
meteringMode = 0;
}
lux = getLux(); // get Lux reading from BH1750
rgb_sensor.getColorTemp(); // get White Balance Value from TCS34725
}
void loop() {
readAll();
menu();
if (!R) {
// Save setting if Metering button pressed.
SaveSettings();
lux = 0;
refresh();
if (meteringMode == 0) {
// Ambient light meter mode.
lightMeter.configure(BH1750::ONE_TIME_HIGH_RES_MODE_2);
lux = getLux();
rgb_sensor.getColorTemp();
if (Overflow == 1) {
delay(10);
getLux();
}
refresh();
delay(200);
} else if (meteringMode == 1) {
// Flash light metering
lightMeter.configure(BH1750::CONTINUOUS_LOW_RES_MODE);
unsigned long startTime = millis();
uint16_t currentLux = 0;
lux = 0;
while (true) {
// check max flash metering time
if (startTime + MaxFlashMeteringTime < millis()) {
break;
}
currentLux = getLux();
delay(16);
if (currentLux > lux) {
lux = currentLux;
}
}
refresh();
}
}
}
void outOfrange() {
display.println(F("--"));
}
void SaveSettings() {
// Save lightmeter setting into EEPROM.
EEPROM.write(ndIndexAddr, ndIndex);
EEPROM.write(ISOIndexAddr, ISOIndex);
EEPROM.write(modeIndexAddr, modeIndex);
EEPROM.write(apertureIndexAddr, apertureIndex);
EEPROM.write(T_expIndexAddr, T_expIndex);
EEPROM.write(meteringModeAddr, meteringMode);
}
void footer() {
}
/*
Get light value
*/
float getLux() {
uint16_t lux = lightMeter.readLightLevel();
if (lux >= 65534) {
// light sensor is overloaded.
Overflow = 1;
lux = 65535;
} else {
Overflow = 0;
}
return lux * DomeMultiplier; // DomeMultiplier = 2.17 (calibration)*/
}
float log2(float x) {
return log(x) / log(2);
}
float lux2ev(float lux) {
return log2(lux / 2.5);
}
// return aperture value (1.4, 1.8, 2.0) by index in sequence (0, 1, 2, 3, ...).
float getApertureByIndex(uint8_t indx) {
float roundIndx = 10.0;
if (indx > 39) {
roundIndx = 1;
}
float f = round(pow(2, indx / 3.0 * 0.5) * roundIndx) / roundIndx;
// the formula returns exact value, but photographers uses more memorable values.
// convert it.
if (f >= 1.1 && f < 1.2) {
f = 1.1;
} else if (f >= 1.2 && f < 1.4) {
f = 1.2;
} else if (f > 3.2 && f < 4) {
f = 3.5;
} else if (f > 5 && f < 6.3) {
f = 5.6;
} else if (f > 10 && f < 11) {
f = 10;
} else if (f >= 11 && f < 12) {
f = 11;
} else if (f >= 12 && f < 14) {
f = 13;
} else if (f >= 14 && f < 16) {
f = 14;
} else if (f >= 20 && f < 22) {
f = 20;
} else if (f >= 22 && f < 25) {
f = 22;
} else if (f >= 24 && f < 28) {
f = 25;
} else if (f >= 28 && f < 40) {
f = 36;
} else if (f >= 40 && f < 45) {
f = 40;
} else if (f >= 45 && f < 50) {
f = 45;
} else if (f >= 50 && f < 57) {
f = 51;
} else if (f >= 71 && f < 80) {
f = 72;
} else if (f >= 80 && f < 90) {
f = 80;
} else if (f >= 90 && f < 101) {
f = 90;
}
return f;
}
// Return ISO value (100, 200, 400, ...) by index in sequence (0, 1, 2, 3, ...).
long getISOByIndex(uint8_t indx) {
if (indx < 0 || indx > MaxISOIndex) {
indx = 0;
}
indx += 10;
// float iso = pow(10, (indx - 1) / 10.0);
// iso = (long)(round(iso / 10.0) * 10);
long int factor = 1;
float iso = 0;
if (indx > 60) {
indx -= 50;
factor = 100000;
} else if (indx > 50) {
indx -= 40;
factor = 10000;
} else if (indx > 40) {
indx -= 30;
factor = 1000;
} else if (indx > 30) {
indx -= 20;
factor = 100;
} else if (indx > 20) {
indx -= 10;
factor = 10;
}
if (indx == 10) {
iso = 8;
} else if (indx == 11) {
iso = 10;
} else if (indx == 12) {
iso = 12.5;
} else if (indx == 13) {
iso = 16;
} else if (indx == 14) {
iso = 20;
} else if (indx == 15) {
iso = 25;
} else if (indx == 16) {
iso = 32;
} else if (indx == 17) {
iso = 40;
} else if (indx == 18) {
iso = 50;
} else if (indx == 19) {
iso = 64;
} else if (indx == 20) {
iso = 80;
}
iso = (long)floor(iso * factor);
return iso;
}
float getMinDistance(float x, float v1, float v2) {
if (x - v1 > v2 - x) {
return v2;
}
return v1;
}
float getTimeByIndex(uint8_t indx) {
if (indx < 0 || indx >= MaxTimeIndex) {
indx = 0;
}
float factor = 0;
float t = 0;
if (indx < 10) {
factor = 100.0;
} else if (indx < 20) {
indx -= 10;
factor = 10.0;
} else if (indx < 30) {
indx -= 20;
factor = 1.0;
} else if (indx < 40) {
indx -= 30;
factor = 0.1;
} else if (indx < 50) {
indx -= 40;
factor = 0.01;
} else if (indx < 60) {
indx -= 50;
factor = 0.001;
} else if (indx < 70) {
indx -= 60;
factor = 0.0001;
} else if (indx < 80) {
indx -= 70;
factor = 0.00001;
}
if (indx == 0) {
t = 100;
} else if (indx == 1) {
t = 80;
} else if (indx == 2) {
t = 64;
} else if (indx == 3) {
t = 50;
} else if (indx == 4) {
t = 40;
} else if (indx == 5) {
t = 32;
} else if (indx == 6) {
t = 25;
} else if (indx == 7) {
t = 20;
} else if (indx == 8) {
t = 16;
} else if (indx == 9) {
t = 12.5;
}
t = 1 / (t * factor);
return t;
}
// Convert calculated time (in seconds) to photograpy style shutter speed.
double fixTime(double t) {
double divider = 1;
float maxTime = getTimeByIndex(MaxTimeIndex);
if (t < maxTime) {
return maxTime;
}
t = 1 / t;
if (t > 99999) {
divider = 10000;
} else if (t > 9999) {
divider = 1000;
} else if (t > 999) {
divider = 100;
} else if (t > 99) {
divider = 10;
}
t = t / divider;
if (t >= 10 && t <= 12.5) {
t = getMinDistance(t, 10, 12.5);
} else if (t >= 12.5 && t <= 16) {
t = getMinDistance(t, 12.5, 16);
} else if (t >= 16 && t <= 20) {
t = getMinDistance(t, 16, 20);
} else if (t >= 20 && t <= 25) {
t = getMinDistance(t, 20, 25);
} else if (t >= 25 && t <= 32) {
t = getMinDistance(t, 25, 32);
} else if (t >= 32 && t <= 40) {
t = getMinDistance(t, 32, 40);
} else if (t >= 40 && t <= 50) {
t = getMinDistance(t, 40, 50);
} else if (t >= 50 && t <= 64) {
t = getMinDistance(t, 50, 64);
} else if (t >= 64 && t <= 80) {
t = getMinDistance(t, 64, 80);
} else if (t >= 80 && t <= 100) {
t = getMinDistance(t, 80, 100);
}
t = t * divider;
if (t == 32) {
t = 30;
}
if (t == 16) {
t = 15;
}
t = 1 / t;
return t;
}
// Convert calculated aperture value to photograpy style aperture value.
float fixAperture(float a) {
for (int i = 0; i < MaxApertureIndex; i++) {
float a1 = getApertureByIndex(i);
float a2 = getApertureByIndex(i + 1);
if (a1 < a && a2 >= a) {
return getMinDistance(a, a1, a2);
}
}
return 0;
}
/*
Return ND from ndIndex
int ND[] = {0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48}; // eg.: 1) 0.3 ND = -1 stop = 2^2 = 4; 2) 0.9 ND = -3 stop = 2^3 = 16;
*/
uint8_t getND(uint8_t ndIndex) {
if (ndIndex == 0) {
return 0;
}
return 3 + (ndIndex - 1) * 3;
}
// Calculate new exposure value and display it.
void refresh() {
ISOset = false;
mainScreen = true;
float EV = lux2ev(lux);
float T = getTimeByIndex(T_expIndex);
float A = getApertureByIndex(apertureIndex);
long iso = getISOByIndex(ISOIndex);
uint8_t ndStop = getND(ndIndex);
// if ND filter is configured then make corrections.
// As ISO is a main operand in all EV calculations we can adjust ISO by ND filter factor.
// if ND4 (ND 0.6) filter is configured then we need to adjust ISO to -2 full stops. Ex. 800 to 200
if (ndIndex > 0) {
ISOND = iso / (pow(2, ndIndex));
} else {
ISOND = iso;
}
if (lux > 0) {
if (modeIndex == 0) {
// Aperture priority. Calculating time.
T = fixTime(100 * pow(A, 2) / ISOND / pow(2, EV)); //T = exposure time, in seconds
// Calculating shutter speed index for correct menu navigation.
for (int i = 0; i <= MaxTimeIndex; i++) {
if (T == getTimeByIndex(i)) {
T_expIndex = i;
break;
}
}
} else if (modeIndex == 1) {
// Shutter speed priority. Calculating aperture.
A = fixAperture(sqrt(pow(2, EV) * ISOND * T / 100));
// Calculating aperture index for correct menu navigation.
if (A > 0) {
for (int i = 0; i <= MaxApertureIndex; i++) {
if (A == getApertureByIndex(i)) {
apertureIndex = i;
break;
}
}
}
}
} else {
if (modeIndex == 0) {
T = 0;
} else {
A = 0;
}
}
uint8_t Tdisplay = 0; // Flag for shutter speed display style (fractional, seconds, minutes)
double Tfr = 0;
float Tmin = 0;
if (T >= 60) {
Tdisplay = 0; // Exposure is in minutes
Tmin = T / 60;
} else if (T < 60 && T >= 0.5) {
Tdisplay = 2; // Exposure in in seconds
} else if (T < 0.5) {
Tdisplay = 1; // Exposure is in fractional form
Tfr = round(1 / T);
}
/* /////// DISPLAY /////// */
uint8_t linePos[] = {17, 37};
display.clearDisplay();
display.setTextColor(WHITE);
/* APERTURE DISPLAY /////// */
display.setTextSize(1);
display.setCursor(0, 1);
display.print(F("f/"));
display.setCursor(13, 1);
display.setTextSize(3);
if (A > 0) {
if (A >= 100) {
display.print(A, 0);
} else {
display.print(A, 1);
}
} else {
outOfrange();
}
/* SHUTTER DISPLAY /////// */
display.setTextSize(2);
display.setCursor(0, 38);
if (Tdisplay == 0) {
display.print(Tmin, 1);
display.print(F("m"));
} else if (Tdisplay == 1) {
if (T > 0) {
display.print(F("1/"));
display.print(Tfr, 0);
} else {
outOfrange();
}
} else if (Tdisplay == 2) {
display.print(T, 1);
display.print(F("s"));
} else if (Tdisplay == 3) {
outOfrange();
}
/* DEFINE SAME TEXT SIZE & COLOR BELOW THIS RANGE /////// */
display.setTextColor(WHITE);
display.setTextSize(1);
/* PRIORITY MARKING (Aperture/Shutter) /////// */
if (modeIndex == 0) {
// Aperture mode
display.setTextColor(WHITE);
display.setCursor(36, 26);
display.print(F("Aperture"));
display.drawLine(0, 26, 26, 26, WHITE);
display.drawLine(26, 26, 32, 32, WHITE);
} else if (modeIndex == 1) {
// Shutter mode
display.setCursor(0, 26);
display.print(F("Shutter"));
display.drawLine(50, 32, 82, 32, WHITE);
display.drawLine(44, 26, 50, 32, WHITE);
}
/* METERING MODE DISPLAY (SWITCHING AMBIENT/FLASH) /////// */
if (meteringMode == 0) {
} else if (meteringMode == 1) {
display.setCursor(50, 57);
display.print(F("+FLASH"));
}
/* LINE DIVISOR /////// */
display.drawLine(91, 28, 91, 52, WHITE);
/* BATTERY PERCENTAGE /////// */
if (battery.level() > 80 && battery.level() < 100) {
display.drawBitmap(89, 56, _full, 16, 8, WHITE);
} else if (battery.level() > 60 && battery.level() < 80) {
display.drawBitmap(89, 56, _threequarters, 16, 8, WHITE);
} else if (battery.level() > 40 && battery.level() < 60) {
display.drawBitmap(89, 56, _half, 16, 8, WHITE);
} else if (battery.level() > 20 && battery.level() < 40) {
display.drawBitmap(89, 56, _low, 16, 8, WHITE);
} else {
display.drawBitmap(89, 56, _empty, 16, 8, WHITE);
}
display.setCursor(104, 57);
display.print(battery.level());
display.println(F("%"));
display.setCursor(96, 47);
display.print(battery.voltage() / 1000.0, 1);
display.print(F("v"));
/* WHITE BALANCE DISPLAY /////// */
display.drawRect(91, 1, 37, 25, WHITE);
display.setCursor(98, 5);
display.print(F("TEMP"));
if (rgb_sensor.ct > 9999) {
display.setCursor(101, 15);
display.print(rgb_sensor.ct / 1000.0, 0);
} else {
display.setCursor(98, 15);
display.print(rgb_sensor.ct / 1000.0, 1);
} display.print(F("K"));
/* LUX DISPLAY ///////
display.setCursor(90, 27);
display.print(lux, 0);
display.print(F("lx")); */
/* EV DISPLAY /////// */
display.setCursor(96, 33);
display.print(F("EV:"));
if (lux > 0) {
display.println(EV, 0);
} else {
display.println(0, 0);
}
/* ISO DISPLAY /////// */
display.setCursor(0, 57);
display.print(F("ISO:"));
if (iso > 999999) {
display.print(iso / 1000000.0, 2);
display.print(F("M"));
} else if (iso > 9999) {
display.print(iso / 1000.0, 0);
display.print(F("K"));
} else {
display.print(iso);
}
display.display();
}
void showISOset() {
ISOset = true;
mainScreen = false;
/* ISO MENU /////// */
display.clearDisplay();
display.setTextSize(2);
display.setCursor(47, 1);
display.println(F("ISO"));
display.setTextSize(1);
display.setCursor(44, 18);
display.println(F("SETTING"));
display.setTextSize(3);
long iso = getISOByIndex(ISOIndex);
if (iso > 999999) {
display.setCursor(0, 35);
} else if (iso > 99999) {
display.setCursor(10, 35);
} else if (iso > 9999) {
display.setCursor(20, 35);
} else if (iso > 999) {
display.setCursor(30, 35);
} else if (iso > 99) {
display.setCursor(40, 35);
} else {
display.setCursor(50, 35);
}
display.print(iso);
display.display();
delay(200);
}
/* /////// FUNCTION /////// */
void menu() {
if (!Bn1 && !Bn2) {
if (mainScreen) {
showISOset();
} else {
refresh();
delay(200);
}
}
if (ISOset) {
// ISO change mode
if (!Bn1) {
// increase ISO
ISOIndex++;
if (ISOIndex > MaxISOIndex) {
ISOIndex = 0;
}
} else if (!Bn2) {
if (ISOIndex > 0) {
ISOIndex--;
} else {
ISOIndex = MaxISOIndex;
}
}
if (!Bn1 || !Bn2) {
showISOset();
}
}
if (!R && !Bn1) {
// switching between Aperture priority and Shutter Speed priority.
if (mainScreen) {
modeIndex++;
if (modeIndex > 1) {
modeIndex = 0;
}
}
refresh();
delay(200);
}
if (mainScreen && !R && !Bn2) {
// Switch between Ambient light and Flash light metering
if (meteringMode == 0) {
meteringMode = 1;
} else {
meteringMode = 0;
}
refresh();
delay(200);
}
if (mainScreen && (!Bn1 || !Bn2)) {
if (modeIndex == 0) {
// Aperture priority mode
if (!Bn1) {
// Increase aperture.
apertureIndex++;
if (apertureIndex > MaxApertureIndex) {
apertureIndex = 0;
}
} else if (!Bn2) {
// Decrease aperture
if (apertureIndex > 0) {
apertureIndex--;
} else {
apertureIndex = MaxApertureIndex;
}
}
} else if (modeIndex == 1) {
// Time priority mode
if (!Bn1) {
// increase time
T_expIndex--;
if (T_expIndex > MaxTimeIndex) {
T_expIndex = 0;
}
} else if (!Bn2) {
// decrease time
if (T_expIndex > 0) {
T_expIndex++;
} else {
T_expIndex = MaxTimeIndex;
}
}
}
delay(200);
refresh();
}
}
void readAll() {
Bn1 = digitalRead(Bn1p);
Bn2 = digitalRead(Bn2p);
R = digitalRead(Rp);
}