77 lines
3.5 KiB
C
77 lines
3.5 KiB
C
#include "device_power.h"
|
|||
|
|
#include "call_client.h"
|
||
|
|
#include "audio_processor.h"
|
||
|
|
#include "volc_hal.h"
|
||
|
|
#include "nvs.h"
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
static int active_brightness=80;
|
||
|
|
|
||
|
|
esp_err_t device_power_boot(void) {
|
||
|
|
nvs_handle_t n;uint8_t standby=0;
|
||
|
|
esp_err_t e=nvs_open("control",NVS_READONLY,&n);
|
||
|
|
if(e==ESP_OK) {
|
||
|
|
e=nvs_get_u8(n,"standby",&standby);nvs_close(n);
|
||
|
|
if(e!=ESP_OK && e!=ESP_ERR_NVS_NOT_FOUND)return e;
|
||
|
|
} else if(e!=ESP_ERR_NVS_NOT_FOUND)return e;
|
||
|
|
if(standby>1)return ESP_ERR_INVALID_STATE;
|
||
|
|
e=call_client_set_standby(standby!=0);
|
||
|
|
if(e==ESP_OK)e=audio_recorder_set_standby(standby!=0);
|
||
|
|
volc_hal_context_t *ctx=volc_get_global_hal_context();
|
||
|
|
if(e==ESP_OK && standby && (!ctx || volc_hal_display_set_brightness(ctx->display_handle,0)))return ESP_FAIL;
|
||
|
|
return e;
|
||
|
|
}
|
||
|
|
|
||
|
|
esp_err_t device_power_apply(const char *mode, bool maintenance_held) {
|
||
|
|
if(!mode || (strcmp(mode,"active") && strcmp(mode,"standby")))return ESP_ERR_INVALID_ARG;
|
||
|
|
bool standby=!strcmp(mode,"standby");
|
||
|
|
if(!maintenance_held && !call_client_enter_maintenance())return ESP_ERR_INVALID_STATE;
|
||
|
|
esp_err_t e=ESP_OK;
|
||
|
|
volc_hal_context_t *ctx=volc_get_global_hal_context();
|
||
|
|
int brightness=ctx?volc_hal_display_get_brightness(ctx->display_handle):-1;
|
||
|
|
if(brightness<0){e=ESP_FAIL;goto done;}
|
||
|
|
if(standby && !call_client_standby() && brightness>0)active_brightness=brightness;
|
||
|
|
// Keep calls excluded until the microphone and display have both changed.
|
||
|
|
if(standby) {
|
||
|
|
e=call_client_set_standby(true);
|
||
|
|
if(e==ESP_OK && !audio_recorder_is_standby())e=audio_recorder_set_standby(true);
|
||
|
|
if(e==ESP_OK && brightness!=0 && volc_hal_display_set_brightness(ctx->display_handle,0))e=ESP_FAIL;
|
||
|
|
} else {
|
||
|
|
if(brightness==0 && volc_hal_display_set_brightness(ctx->display_handle,active_brightness))e=ESP_FAIL;
|
||
|
|
if(e==ESP_OK)e=audio_recorder_set_standby(false);
|
||
|
|
if(e==ESP_OK)e=call_client_set_standby(false);
|
||
|
|
}
|
||
|
|
if(e!=ESP_OK)goto done;
|
||
|
|
brightness=volc_hal_display_get_brightness(ctx->display_handle);
|
||
|
|
if(brightness<0 || (standby ? brightness!=0 || !audio_recorder_is_standby() : brightness==0 || audio_recorder_is_standby())){e=ESP_FAIL;goto done;}
|
||
|
|
nvs_handle_t n;e=nvs_open("control",NVS_READWRITE,&n);
|
||
|
|
if(e==ESP_OK) {
|
||
|
|
uint8_t stored=2;
|
||
|
|
esp_err_t read=nvs_get_u8(n,"standby",&stored);
|
||
|
|
if(read!=ESP_OK && read!=ESP_ERR_NVS_NOT_FOUND)e=read;
|
||
|
|
else if(stored!=(uint8_t)standby) {
|
||
|
|
e=nvs_set_u8(n,"standby",standby);
|
||
|
|
if(e==ESP_OK)e=nvs_commit(n);
|
||
|
|
if(e==ESP_OK)e=nvs_get_u8(n,"standby",&stored);
|
||
|
|
if(e==ESP_OK && stored!=(uint8_t)standby)e=ESP_FAIL;
|
||
|
|
}
|
||
|
|
nvs_close(n);
|
||
|
|
}
|
||
|
|
done:
|
||
|
|
if(!maintenance_held)call_client_leave_maintenance();
|
||
|
|
return e;
|
||
|
|
}
|
||
|
|
|
||
|
|
cJSON *device_power_report(void) {
|
||
|
|
volc_hal_context_t *ctx=volc_get_global_hal_context();
|
||
|
|
int brightness=ctx?volc_hal_display_get_brightness(ctx->display_handle):-1;
|
||
|
|
bool microphone=audio_recorder_is_standby(),standby=call_client_standby();
|
||
|
|
const char *mode=standby && microphone && brightness==0?"standby":!standby && !microphone && brightness>0?"active":"transition";
|
||
|
|
cJSON *j=cJSON_CreateObject();
|
||
|
|
cJSON_AddStringToObject(j,"mode",mode);
|
||
|
|
cJSON_AddBoolToObject(j,"microphone_paused",microphone);
|
||
|
|
cJSON_AddNumberToObject(j,"backlight_percent",brightness);
|
||
|
|
cJSON_AddNumberToObject(j,"microphone_reads",audio_recorder_microphone_reads());
|
||
|
|
return j;
|
||
|
|
}
|