diff --git a/openrtx/include/interfaces/platform.h b/openrtx/include/interfaces/platform.h index 5cd8fb97..0b04aa6d 100644 --- a/openrtx/include/interfaces/platform.h +++ b/openrtx/include/interfaces/platform.h @@ -84,9 +84,9 @@ void platform_init(); void platform_terminate(); /** - * This function reads and returns the current battery voltage in volt. + * This function reads and returns the current battery voltage in millivolt. */ -float platform_getVbat(); +uint16_t platform_getVbat(); /** * This function reads and returns the current microphone input level as a diff --git a/platform/targets/DM-1801/platform.c b/platform/targets/DM-1801/platform.c index 8a081eee..79748b4d 100644 --- a/platform/targets/DM-1801/platform.c +++ b/platform/targets/DM-1801/platform.c @@ -98,13 +98,18 @@ void platform_terminate() gpio_clearPin(PWR_SW); } -float platform_getVbat() +uint16_t platform_getVbat() { pthread_mutex_lock(&adc_mutex); uint16_t value = adc0_getMeasurement(1); pthread_mutex_unlock(&adc_mutex); - return (((float) value) * 3.0f)/1000.0f; + /* + * Battery voltage is measured through an 1:3 voltage divider and + * adc1_getMeasurement returns a value in mV. Thus, to have effective + * battery voltage, multiply by three. + */ + return value * 3; } uint8_t platform_getMicLevel() diff --git a/platform/targets/GD-77/platform.c b/platform/targets/GD-77/platform.c index 6252f821..8076cd57 100644 --- a/platform/targets/GD-77/platform.c +++ b/platform/targets/GD-77/platform.c @@ -97,13 +97,18 @@ void platform_terminate() gpio_clearPin(PWR_SW); } -float platform_getVbat() +uint16_t platform_getVbat() { pthread_mutex_lock(&adc_mutex); uint16_t value = adc0_getMeasurement(1); pthread_mutex_unlock(&adc_mutex); - return (((float) value) * 3.0f)/1000.0f; + /* + * Battery voltage is measured through an 1:3 voltage divider and + * adc1_getMeasurement returns a value in mV. Thus, to have effective + * battery voltage, multiply by three. + */ + return value * 3; } uint8_t platform_getMicLevel() diff --git a/platform/targets/MD-3x0/platform.c b/platform/targets/MD-3x0/platform.c index 5efbbbb1..f6596e5c 100644 --- a/platform/targets/MD-3x0/platform.c +++ b/platform/targets/MD-3x0/platform.c @@ -89,14 +89,14 @@ void platform_terminate() gpio_clearPin(PWR_SW); } -float platform_getVbat() +uint16_t platform_getVbat() { /* * Battery voltage is measured through an 1:3 voltage divider and * adc1_getMeasurement returns a value in mV. Thus, to have effective - * battery voltage multiply by three and divide by 1000 + * battery voltage, multiply by three. */ - return adc1_getMeasurement(ADC_VBAT_CH)*3.0f/1000.0f; + return adc1_getMeasurement(ADC_VBAT_CH) * 3; } uint8_t platform_getMicLevel() diff --git a/platform/targets/MD-9600/platform.c b/platform/targets/MD-9600/platform.c index 57b5a8e0..1d91bc5b 100644 --- a/platform/targets/MD-9600/platform.c +++ b/platform/targets/MD-9600/platform.c @@ -104,14 +104,17 @@ void platform_terminate() while(1) ; } -float platform_getVbat() +uint16_t platform_getVbat() { /* * Battery voltage is measured through an 1:5.7 voltage divider and - * adc1_getMeasurement returns a value in mV. Thus, to have effective - * battery voltage multiply by the ratio and divide by 1000 + * adc1_getMeasurement returns a value in mV. To have effective battery + * voltage we have to multiply by the ratio: with a simple trick we can do + * it also without using floats and with a maximum error of -1mV. */ - return (adc1_getMeasurement(ADC_VBAT_CH)*5.7f)/1000.0f; + + uint16_t vbat = adc1_getMeasurement(ADC_VBAT_CH); + return (vbat * 6) - ((vbat * 3) / 10); } uint8_t platform_getMicLevel() diff --git a/platform/targets/MD-UV3x0/platform.c b/platform/targets/MD-UV3x0/platform.c index 00d34547..7b613d87 100644 --- a/platform/targets/MD-UV3x0/platform.c +++ b/platform/targets/MD-UV3x0/platform.c @@ -92,14 +92,14 @@ void platform_terminate() gpio_clearPin(PWR_SW); } -float platform_getVbat() +uint16_t platform_getVbat() { /* * Battery voltage is measured through an 1:3 voltage divider and * adc1_getMeasurement returns a value in mV. Thus, to have effective - * battery voltage multiply by three and divide by 1000 + * battery voltage, multiply by three. */ - return adc1_getMeasurement(ADC_VBAT_CH)*3.0f/1000.0f; + return adc1_getMeasurement(ADC_VBAT_CH) * 3; } uint8_t platform_getMicLevel() diff --git a/platform/targets/linux/platform.c b/platform/targets/linux/platform.c index 99215fb0..06ac8896 100644 --- a/platform/targets/linux/platform.c +++ b/platform/targets/linux/platform.c @@ -51,9 +51,12 @@ void platform_setBacklightLevel(__attribute__((unused)) uint8_t level) } // Simulate a fully charged lithium battery -float platform_getVbat() +uint16_t platform_getVbat() { - return Radio_State.Vbat; + float voltage = Radio_State.Vbat; + if(voltage < 0.0f) voltage = 0.0f; + if(voltage > 65.0f) voltage = 65.0f; + return ((uint16_t) voltage); }