1
0
Fork 0

Part 5: Battery

This commit is contained in:
Austin Morlan 2020-05-21 21:44:29 -07:00
parent d86013c90c
commit e525a823dc
Signed by: austin
GPG Key ID: FD6B27654AF5E348
4 changed files with 100 additions and 0 deletions

View File

@ -1,6 +1,7 @@
idf_component_register(
SRCS
"main.c"
"odroid/battery.c"
"odroid/display.c"
"odroid/input.c"
"odroid/sdcard.c"

View File

@ -1,3 +1,4 @@
#include "odroid/battery.h"
#include "odroid/display.h"
#include "odroid/input.h"
#include "odroid/sdcard.h"
@ -16,6 +17,7 @@ void app_main(void)
Odroid_InitializeInput();
Odroid_InitializeDisplay();
Odroid_InitializeSdcard();
Odroid_InitializeBatteryReader();
// Load sprite
uint16_t* sprite = (uint16_t*)malloc(64 * 64 * sizeof(uint16_t));
@ -95,6 +97,20 @@ void app_main(void)
fclose(snapFile);
}
uint32_t batteryLevel = Odroid_ReadBatteryLevel();
ESP_LOGI(LOG_TAG, "Battery level: %u\n", batteryLevel);
if (batteryLevel < 3600)
{
Odroid_EnableBatteryLight();
}
else
{
Odroid_DisableBatteryLight();
}
Odroid_DrawFrame(gFramebuffer);
}

73
src/odroid/battery.c Normal file
View File

@ -0,0 +1,73 @@
#include "battery.h"
#include <driver/adc.h>
#include <esp_adc_cal.h>
#include <esp_log.h>
#include <soc/adc_channel.h>
static const char* LOG_TAG = "OdroidBattery";
static const adc1_channel_t BATTERY_READ_PIN = ADC1_GPIO36_CHANNEL;
static const gpio_num_t BATTERY_LED_PIN = GPIO_NUM_2;
static esp_adc_cal_characteristics_t gCharacteristics;
void Odroid_InitializeBatteryReader()
{
// Configure LED
{
gpio_config_t gpioConfig = {};
gpioConfig.mode = GPIO_MODE_OUTPUT;
gpioConfig.pin_bit_mask = 1ULL << BATTERY_LED_PIN;
ESP_ERROR_CHECK(gpio_config(&gpioConfig));
}
// Configure ADC
{
adc1_config_width(ADC_WIDTH_BIT_12);
adc1_config_channel_atten(BATTERY_READ_PIN, ADC_ATTEN_DB_11);
adc1_config_channel_atten(BATTERY_READ_PIN, ADC_ATTEN_DB_11);
esp_adc_cal_value_t type = esp_adc_cal_characterize(
ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 1100, &gCharacteristics);
// The ESP32 in the Odroid Go should have its fuse set with a pre-calibrated vref
assert(type == ESP_ADC_CAL_VAL_EFUSE_VREF);
}
ESP_LOGI(LOG_TAG, "Battery reader initialized");
}
uint32_t Odroid_ReadBatteryLevel(void)
{
const int SAMPLE_COUNT = 20;
uint32_t raw = 0;
for (int sampleIndex = 0; sampleIndex < SAMPLE_COUNT; ++sampleIndex)
{
raw += adc1_get_raw(BATTERY_READ_PIN);
}
raw /= SAMPLE_COUNT;
// Voltage divider reports half actual voltage
uint32_t voltage = 2 * esp_adc_cal_raw_to_voltage(raw, &gCharacteristics);
return voltage;
}
void Odroid_EnableBatteryLight(void)
{
gpio_set_level(BATTERY_LED_PIN, 1);
}
void Odroid_DisableBatteryLight(void)
{
gpio_set_level(BATTERY_LED_PIN, 0);
}

10
src/odroid/battery.h Normal file
View File

@ -0,0 +1,10 @@
#pragma once
#include <stdint.h>
void Odroid_InitializeBatteryReader(void);
uint32_t Odroid_ReadBatteryLevel(void);
void Odroid_EnableBatteryLight(void);
void Odroid_DisableBatteryLight(void);