A simple example for analog input and EPD usage.
Dependencies: GDEP015OC1 acn_nrf52_saadc aconno_bsp
Fork of acd52832_3_Analog_In by
main.cpp
- Committer:
- jurica238814
- Date:
- 2017-06-30
- Revision:
- 5:6566725c8835
- Parent:
- 4:f6f94ef38e6a
- Child:
- 6:e848c82b5248
File content as of revision 5:6566725c8835:
/* Copyright (c) 2017 Aconno. All Rights Reserved.
*
* Licensees are granted free, non-transferable use of the information. NO
* WARRANTY of ANY KIND is provided. This heading must NOT be removed from
* the file.
*/
#include "mbed.h"
#include "acd52832_bsp.h"
#include "GDEP015OC1.h"
#include "pictures.h"
#define DEBUG (1)
#define ADC_MAX_VALUE (4092)
#define ADC_REF_VOLTAGE (3.6)
#define VOLTAGE_DIVIDER_RATION (130.0/30)
#define CURRENT_FACTOR (36.0)
#define BATTERY_MAX_V (14.0)
#define BATTERY_MIN_V (12.0)
#define BATTERY_STEP (0.4)
#define LOW_QUICK_CURRENT (0.5)
SPI spi(PIN_EPD_MOSI, NC, PIN_EPD_SCK, NC);
GDEP015OC1 epd = GDEP015OC1(spi, PIN_EPD_CS, PIN_EPD_DC, PIN_EPD_RST, PIN_EPD_BUSY);
AnalogIn battery (p28);
AnalogIn usb1 (p29);
AnalogIn usb2 (p30);
int main(){
char buffer[25] = {0};
char low_string[25] = "LOW";
char quick_string[25] = "QUICK";
float adc1_mean=0, adc2_mean=0, adc3_mean=0;
float battery_voltage = 0;
float usb1_current = 0, usb2_current = 0;
int count = 0;
NRF_SAADC->RESOLUTION = 0x00000002; // Set 12b resolution
epd.empty();
epd.writeFull();
while(true){
adc1_mean += battery.read_u16();
adc2_mean += usb1.read_u16();
adc3_mean += usb2.read_u16();
count ++;
if (count == 10){
adc1_mean /= 10;
adc2_mean /= 10;
adc3_mean /= 10;
count = 0;
battery_voltage = adc1_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)*VOLTAGE_DIVIDER_RATION;
usb1_current = (CURRENT_FACTOR/ADC_MAX_VALUE)*adc2_mean;
usb2_current = (CURRENT_FACTOR/ADC_MAX_VALUE)*adc3_mean;
if(battery_voltage > BATTERY_MAX_V - BATTERY_STEP){
//Load image
for(uint16_t x=0;x<5000;x++)
epd.fill(BS_5[x], x);
epd.write();
}
else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 2) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 1)){
//Load image
for(uint16_t x=0;x<5000;x++)
epd.fill(BS_4[x], x);
epd.write();
}
else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 3) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 2)){
//Load image
for(uint16_t x=0;x<5000;x++)
epd.fill(BS_3[x], x);
epd.write();
}
else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 4) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 3)){
//Load image
for(uint16_t x=0;x<5000;x++)
epd.fill(BS_2[x], x);
epd.write();
}
else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 5) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 4)){
//Load image
for(uint16_t x=0;x<5000;x++)
epd.fill(BS_1[x], x);
epd.write();
}
else if(battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 5){
//Load image
for(uint16_t x=0;x<5000;x++)
epd.fill(BS_E[x], x);
epd.write();
}
if(usb1_current < (float)LOW_QUICK_CURRENT){
epd.writeString(low_string, 25, 180, 0);
epd.write();
}
else{
epd.writeString(quick_string, 25, 180, 0);
epd.write();
}
if(usb2_current < (float)LOW_QUICK_CURRENT){
epd.writeString(low_string, 135, 180, 0);
epd.write();
}
else{
epd.writeString(quick_string, 135, 180, 0);
epd.write();
}
#if DEBUG
/*
// Print voltage and current values in debug mode
sprintf(buffer, "Battery: %5.5fV", adc1_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)*VOLTAGE_DIVIDER_RATION); // Create a string
epd.writeString(buffer,25,95,0); // Write new data to the buffer
epd.write(); // Write string to the EPD
*/
sprintf(buffer, "USB1: %5.5fA", (CURRENT_FACTOR/ADC_MAX_VALUE)*adc2_mean); // Create a string
epd.writeString(buffer,5,190,0); // Write new data to the buffer
epd.write(); // Write string to the EPD
sprintf(buffer, "USB1: %5.5fA", (CURRENT_FACTOR/ADC_MAX_VALUE)*adc3_mean); // Create a string
epd.writeString(buffer,105,190,0); // Write new data to the buffer
epd.write(); // Write string to the EPD
#endif
adc1_mean = 0;
adc2_mean = 0;
adc3_mean = 0;
}
}
}
