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@5:6566725c8835, 2017-06-30 (annotated)
- Committer:
- jurica238814
- Date:
- Fri Jun 30 11:45:56 2017 +0000
- Revision:
- 5:6566725c8835
- Parent:
- 4:f6f94ef38e6a
- Child:
- 6:e848c82b5248
Images added.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jurica238814 | 0:dc96b5c8e3f6 | 1 | /* Copyright (c) 2017 Aconno. All Rights Reserved. |
jurica238814 | 0:dc96b5c8e3f6 | 2 | * |
jurica238814 | 0:dc96b5c8e3f6 | 3 | * Licensees are granted free, non-transferable use of the information. NO |
jurica238814 | 0:dc96b5c8e3f6 | 4 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
jurica238814 | 0:dc96b5c8e3f6 | 5 | * the file. |
jurica238814 | 0:dc96b5c8e3f6 | 6 | */ |
jurica238814 | 0:dc96b5c8e3f6 | 7 | |
jurica238814 | 0:dc96b5c8e3f6 | 8 | #include "mbed.h" |
jurica238814 | 0:dc96b5c8e3f6 | 9 | #include "acd52832_bsp.h" |
jurica238814 | 0:dc96b5c8e3f6 | 10 | #include "GDEP015OC1.h" |
jurica238814 | 5:6566725c8835 | 11 | #include "pictures.h" |
jurica238814 | 0:dc96b5c8e3f6 | 12 | |
jurica238814 | 5:6566725c8835 | 13 | #define DEBUG (1) |
jurica238814 | 5:6566725c8835 | 14 | #define ADC_MAX_VALUE (4092) |
jurica238814 | 5:6566725c8835 | 15 | #define ADC_REF_VOLTAGE (3.6) |
jurica238814 | 5:6566725c8835 | 16 | #define VOLTAGE_DIVIDER_RATION (130.0/30) |
jurica238814 | 5:6566725c8835 | 17 | #define CURRENT_FACTOR (36.0) |
jurica238814 | 5:6566725c8835 | 18 | #define BATTERY_MAX_V (14.0) |
jurica238814 | 5:6566725c8835 | 19 | #define BATTERY_MIN_V (12.0) |
jurica238814 | 5:6566725c8835 | 20 | #define BATTERY_STEP (0.4) |
jurica238814 | 5:6566725c8835 | 21 | #define LOW_QUICK_CURRENT (0.5) |
jurica238814 | 5:6566725c8835 | 22 | |
jurica238814 | 5:6566725c8835 | 23 | |
jurica238814 | 0:dc96b5c8e3f6 | 24 | |
jurica238814 | 0:dc96b5c8e3f6 | 25 | SPI spi(PIN_EPD_MOSI, NC, PIN_EPD_SCK, NC); |
jurica238814 | 0:dc96b5c8e3f6 | 26 | GDEP015OC1 epd = GDEP015OC1(spi, PIN_EPD_CS, PIN_EPD_DC, PIN_EPD_RST, PIN_EPD_BUSY); |
jurica238814 | 0:dc96b5c8e3f6 | 27 | |
jurica238814 | 0:dc96b5c8e3f6 | 28 | AnalogIn battery (p28); |
jurica238814 | 0:dc96b5c8e3f6 | 29 | AnalogIn usb1 (p29); |
jurica238814 | 0:dc96b5c8e3f6 | 30 | AnalogIn usb2 (p30); |
jurica238814 | 0:dc96b5c8e3f6 | 31 | |
jurica238814 | 0:dc96b5c8e3f6 | 32 | int main(){ |
jurica238814 | 0:dc96b5c8e3f6 | 33 | char buffer[25] = {0}; |
jurica238814 | 5:6566725c8835 | 34 | char low_string[25] = "LOW"; |
jurica238814 | 5:6566725c8835 | 35 | char quick_string[25] = "QUICK"; |
jurica238814 | 4:f6f94ef38e6a | 36 | float adc1_mean=0, adc2_mean=0, adc3_mean=0; |
jurica238814 | 5:6566725c8835 | 37 | float battery_voltage = 0; |
jurica238814 | 5:6566725c8835 | 38 | float usb1_current = 0, usb2_current = 0; |
jurica238814 | 4:f6f94ef38e6a | 39 | int count = 0; |
jurica238814 | 4:f6f94ef38e6a | 40 | |
jurica238814 | 4:f6f94ef38e6a | 41 | NRF_SAADC->RESOLUTION = 0x00000002; // Set 12b resolution |
jurica238814 | 5:6566725c8835 | 42 | |
jurica238814 | 5:6566725c8835 | 43 | epd.empty(); |
jurica238814 | 5:6566725c8835 | 44 | epd.writeFull(); |
jurica238814 | 5:6566725c8835 | 45 | |
jurica238814 | 0:dc96b5c8e3f6 | 46 | while(true){ |
jurica238814 | 0:dc96b5c8e3f6 | 47 | |
jurica238814 | 4:f6f94ef38e6a | 48 | adc1_mean += battery.read_u16(); |
jurica238814 | 4:f6f94ef38e6a | 49 | adc2_mean += usb1.read_u16(); |
jurica238814 | 4:f6f94ef38e6a | 50 | adc3_mean += usb2.read_u16(); |
jurica238814 | 4:f6f94ef38e6a | 51 | count ++; |
jurica238814 | 0:dc96b5c8e3f6 | 52 | |
jurica238814 | 4:f6f94ef38e6a | 53 | if (count == 10){ |
jurica238814 | 4:f6f94ef38e6a | 54 | |
jurica238814 | 4:f6f94ef38e6a | 55 | adc1_mean /= 10; |
jurica238814 | 4:f6f94ef38e6a | 56 | adc2_mean /= 10; |
jurica238814 | 4:f6f94ef38e6a | 57 | adc3_mean /= 10; |
jurica238814 | 5:6566725c8835 | 58 | count = 0; |
jurica238814 | 4:f6f94ef38e6a | 59 | |
jurica238814 | 5:6566725c8835 | 60 | battery_voltage = adc1_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)*VOLTAGE_DIVIDER_RATION; |
jurica238814 | 5:6566725c8835 | 61 | usb1_current = (CURRENT_FACTOR/ADC_MAX_VALUE)*adc2_mean; |
jurica238814 | 5:6566725c8835 | 62 | usb2_current = (CURRENT_FACTOR/ADC_MAX_VALUE)*adc3_mean; |
jurica238814 | 4:f6f94ef38e6a | 63 | |
jurica238814 | 5:6566725c8835 | 64 | if(battery_voltage > BATTERY_MAX_V - BATTERY_STEP){ |
jurica238814 | 5:6566725c8835 | 65 | //Load image |
jurica238814 | 5:6566725c8835 | 66 | for(uint16_t x=0;x<5000;x++) |
jurica238814 | 5:6566725c8835 | 67 | epd.fill(BS_5[x], x); |
jurica238814 | 5:6566725c8835 | 68 | epd.write(); |
jurica238814 | 5:6566725c8835 | 69 | } |
jurica238814 | 5:6566725c8835 | 70 | else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 2) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 1)){ |
jurica238814 | 5:6566725c8835 | 71 | //Load image |
jurica238814 | 5:6566725c8835 | 72 | for(uint16_t x=0;x<5000;x++) |
jurica238814 | 5:6566725c8835 | 73 | epd.fill(BS_4[x], x); |
jurica238814 | 5:6566725c8835 | 74 | epd.write(); |
jurica238814 | 5:6566725c8835 | 75 | } |
jurica238814 | 5:6566725c8835 | 76 | else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 3) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 2)){ |
jurica238814 | 5:6566725c8835 | 77 | //Load image |
jurica238814 | 5:6566725c8835 | 78 | for(uint16_t x=0;x<5000;x++) |
jurica238814 | 5:6566725c8835 | 79 | epd.fill(BS_3[x], x); |
jurica238814 | 5:6566725c8835 | 80 | epd.write(); |
jurica238814 | 5:6566725c8835 | 81 | } |
jurica238814 | 5:6566725c8835 | 82 | else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 4) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 3)){ |
jurica238814 | 5:6566725c8835 | 83 | //Load image |
jurica238814 | 5:6566725c8835 | 84 | for(uint16_t x=0;x<5000;x++) |
jurica238814 | 5:6566725c8835 | 85 | epd.fill(BS_2[x], x); |
jurica238814 | 5:6566725c8835 | 86 | epd.write(); |
jurica238814 | 5:6566725c8835 | 87 | } |
jurica238814 | 5:6566725c8835 | 88 | else if((battery_voltage > BATTERY_MAX_V - BATTERY_STEP * 5) && (battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 4)){ |
jurica238814 | 5:6566725c8835 | 89 | //Load image |
jurica238814 | 5:6566725c8835 | 90 | for(uint16_t x=0;x<5000;x++) |
jurica238814 | 5:6566725c8835 | 91 | epd.fill(BS_1[x], x); |
jurica238814 | 5:6566725c8835 | 92 | epd.write(); |
jurica238814 | 5:6566725c8835 | 93 | } |
jurica238814 | 5:6566725c8835 | 94 | else if(battery_voltage < BATTERY_MAX_V - BATTERY_STEP * 5){ |
jurica238814 | 5:6566725c8835 | 95 | //Load image |
jurica238814 | 5:6566725c8835 | 96 | for(uint16_t x=0;x<5000;x++) |
jurica238814 | 5:6566725c8835 | 97 | epd.fill(BS_E[x], x); |
jurica238814 | 5:6566725c8835 | 98 | epd.write(); |
jurica238814 | 5:6566725c8835 | 99 | } |
jurica238814 | 5:6566725c8835 | 100 | |
jurica238814 | 5:6566725c8835 | 101 | if(usb1_current < (float)LOW_QUICK_CURRENT){ |
jurica238814 | 5:6566725c8835 | 102 | epd.writeString(low_string, 25, 180, 0); |
jurica238814 | 5:6566725c8835 | 103 | epd.write(); |
jurica238814 | 5:6566725c8835 | 104 | } |
jurica238814 | 5:6566725c8835 | 105 | else{ |
jurica238814 | 5:6566725c8835 | 106 | epd.writeString(quick_string, 25, 180, 0); |
jurica238814 | 5:6566725c8835 | 107 | epd.write(); |
jurica238814 | 5:6566725c8835 | 108 | } |
jurica238814 | 5:6566725c8835 | 109 | |
jurica238814 | 5:6566725c8835 | 110 | if(usb2_current < (float)LOW_QUICK_CURRENT){ |
jurica238814 | 5:6566725c8835 | 111 | epd.writeString(low_string, 135, 180, 0); |
jurica238814 | 5:6566725c8835 | 112 | epd.write(); |
jurica238814 | 5:6566725c8835 | 113 | } |
jurica238814 | 5:6566725c8835 | 114 | else{ |
jurica238814 | 5:6566725c8835 | 115 | epd.writeString(quick_string, 135, 180, 0); |
jurica238814 | 5:6566725c8835 | 116 | epd.write(); |
jurica238814 | 5:6566725c8835 | 117 | } |
jurica238814 | 4:f6f94ef38e6a | 118 | |
jurica238814 | 4:f6f94ef38e6a | 119 | |
jurica238814 | 4:f6f94ef38e6a | 120 | |
jurica238814 | 5:6566725c8835 | 121 | #if DEBUG |
jurica238814 | 5:6566725c8835 | 122 | /* |
jurica238814 | 5:6566725c8835 | 123 | // Print voltage and current values in debug mode |
jurica238814 | 5:6566725c8835 | 124 | sprintf(buffer, "Battery: %5.5fV", adc1_mean*(ADC_REF_VOLTAGE/ADC_MAX_VALUE)*VOLTAGE_DIVIDER_RATION); // Create a string |
jurica238814 | 5:6566725c8835 | 125 | epd.writeString(buffer,25,95,0); // Write new data to the buffer |
jurica238814 | 5:6566725c8835 | 126 | epd.write(); // Write string to the EPD |
jurica238814 | 5:6566725c8835 | 127 | */ |
jurica238814 | 5:6566725c8835 | 128 | sprintf(buffer, "USB1: %5.5fA", (CURRENT_FACTOR/ADC_MAX_VALUE)*adc2_mean); // Create a string |
jurica238814 | 5:6566725c8835 | 129 | epd.writeString(buffer,5,190,0); // Write new data to the buffer |
jurica238814 | 5:6566725c8835 | 130 | epd.write(); // Write string to the EPD |
jurica238814 | 5:6566725c8835 | 131 | |
jurica238814 | 5:6566725c8835 | 132 | sprintf(buffer, "USB1: %5.5fA", (CURRENT_FACTOR/ADC_MAX_VALUE)*adc3_mean); // Create a string |
jurica238814 | 5:6566725c8835 | 133 | epd.writeString(buffer,105,190,0); // Write new data to the buffer |
jurica238814 | 5:6566725c8835 | 134 | epd.write(); // Write string to the EPD |
jurica238814 | 5:6566725c8835 | 135 | #endif |
jurica238814 | 4:f6f94ef38e6a | 136 | |
jurica238814 | 4:f6f94ef38e6a | 137 | adc1_mean = 0; |
jurica238814 | 4:f6f94ef38e6a | 138 | adc2_mean = 0; |
jurica238814 | 4:f6f94ef38e6a | 139 | adc3_mean = 0; |
jurica238814 | 4:f6f94ef38e6a | 140 | } |
jurica238814 | 0:dc96b5c8e3f6 | 141 | } |
jurica238814 | 0:dc96b5c8e3f6 | 142 | } |