From 8e0a5d1c0f6befd5b0d4e3c3d9ded1fe62d52d15 Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Fri, 13 Aug 2021 21:57:32 +0200 Subject: [PATCH] Made 'platform_getVolumeLevel' and 'platform_getMicLevel' API functions return a normalised 8 bit value ranging from 0 to 255 (TG-293 #closed) --- openrtx/include/interfaces/platform.h | 18 +++++++++------- openrtx/src/rtx/OpMode_FM.cpp | 8 +++---- platform/targets/DM-1801/platform.c | 29 +++++++++++++------------- platform/targets/GD-77/platform.c | 17 +++++++-------- platform/targets/MD-3x0/platform.c | 18 ++++++++++++---- platform/targets/MD-9600/platform.c | 18 ++++++++++++---- platform/targets/MD-UV3x0/platform.c | 18 ++++++++++++---- platform/targets/linux/platform.c | 30 +++++++++++++++++---------- 8 files changed, 97 insertions(+), 59 deletions(-) diff --git a/openrtx/include/interfaces/platform.h b/openrtx/include/interfaces/platform.h index 863c451d..5cd8fb97 100644 --- a/openrtx/include/interfaces/platform.h +++ b/openrtx/include/interfaces/platform.h @@ -58,11 +58,11 @@ typedef struct { char name[10]; /* Manufacturer-assigned hardware name. */ - uint16_t uhf_maxFreq; /* Upper bound for UHF band, in MHz. */ - uint16_t uhf_minFreq; /* Lower bound for UHF band, in MHz. */ + uint16_t uhf_maxFreq; /* Upper bound for UHF band, in MHz. */ + uint16_t uhf_minFreq; /* Lower bound for UHF band, in MHz. */ - uint16_t vhf_maxFreq; /* Upper bound for VHF band, in MHz. */ - uint16_t vhf_minFreq; /* Lower bound for VHF band, in MHz. */ + uint16_t vhf_maxFreq; /* Upper bound for VHF band, in MHz. */ + uint16_t vhf_minFreq; /* Lower bound for VHF band, in MHz. */ uint8_t _unused : 4, uhf_band : 1, /* Device allows UHF band operation. */ @@ -89,14 +89,16 @@ void platform_terminate(); float platform_getVbat(); /** - * This function reads and returns the current microphone input level. + * This function reads and returns the current microphone input level as a + * normalised value between 0 and 255. */ -float platform_getMicLevel(); +uint8_t platform_getMicLevel(); /** - * This function reads and returns the current volume selector level. + * This function reads and returns the current volume selector level as a + * normalised value between 0 and 255. */ -float platform_getVolumeLevel(); +uint8_t platform_getVolumeLevel(); /** * This function reads and returns the current channel selector level. diff --git a/openrtx/src/rtx/OpMode_FM.cpp b/openrtx/src/rtx/OpMode_FM.cpp index 541c3dc3..a97512a8 100644 --- a/openrtx/src/rtx/OpMode_FM.cpp +++ b/openrtx/src/rtx/OpMode_FM.cpp @@ -36,14 +36,14 @@ * digital value to be fed into the HR_C6000 lineout DAC gain. We thus have to * provide the helper function below to keep the real volume level consistent * with the knob position. - * Current knob position corresponds to an analog signal in the range 0 - 1500mV, - * which has to be mapped in a range between 1 and 31. */ #ifdef PLATFORM_MDUV3x0 void _setVolume() { - float level = (platform_getVolumeLevel() / 1560.0f) * 30.0f; - uint8_t volume = ((uint8_t) (level + 0.5f)); + // Volume level range is 0 - 255, by right shifting by 3 we get a value in + // range 0 - 31. + uint8_t volume = platform_getVolumeLevel(); + volume >>= 3; // Mute volume when knob is set below 10% if(volume < 1) diff --git a/platform/targets/DM-1801/platform.c b/platform/targets/DM-1801/platform.c index c8d8eded..8a081eee 100644 --- a/platform/targets/DM-1801/platform.c +++ b/platform/targets/DM-1801/platform.c @@ -69,12 +69,12 @@ void platform_init() memset(&calibration, 0x00, sizeof(gdxCalibration_t)); /* Initialise hardware information structure */ - hwInfo.vhf_maxFreq = 174; - hwInfo.vhf_minFreq = 136; - hwInfo.vhf_band = 1; - hwInfo.uhf_maxFreq = 470; - hwInfo.uhf_minFreq = 400; - hwInfo.uhf_band = 1; + hwInfo.vhf_maxFreq = 174; + hwInfo.vhf_minFreq = 136; + hwInfo.vhf_band = 1; + hwInfo.uhf_maxFreq = 470; + hwInfo.uhf_minFreq = 400; + hwInfo.uhf_band = 1; hwInfo.lcd_type = 0; memcpy(hwInfo.name, "DM-1801", 7); hwInfo.name[7] = '\0'; @@ -100,28 +100,27 @@ void platform_terminate() float platform_getVbat() { - float value = 0.0f; pthread_mutex_lock(&adc_mutex); - value = adc0_getMeasurement(1); + uint16_t value = adc0_getMeasurement(1); pthread_mutex_unlock(&adc_mutex); - return (value * 3.0f)/1000.0f; + return (((float) value) * 3.0f)/1000.0f; } -float platform_getMicLevel() +uint8_t platform_getMicLevel() { - float value = 0.0f; pthread_mutex_lock(&adc_mutex); - value = adc0_getMeasurement(3); + uint16_t value = adc0_getRawSample(3); pthread_mutex_unlock(&adc_mutex); - return value; + /* Value from ADC is 12 bit wide: shift right by four to get 0 - 255 */ + return value >> 4; } -float platform_getVolumeLevel() +uint8_t platform_getVolumeLevel() { /* TODO */ - return 0.0f; + return 0; } int8_t platform_getChSelector() diff --git a/platform/targets/GD-77/platform.c b/platform/targets/GD-77/platform.c index 08d930aa..6252f821 100644 --- a/platform/targets/GD-77/platform.c +++ b/platform/targets/GD-77/platform.c @@ -99,28 +99,27 @@ void platform_terminate() float platform_getVbat() { - float value = 0.0f; pthread_mutex_lock(&adc_mutex); - value = adc0_getMeasurement(1); + uint16_t value = adc0_getMeasurement(1); pthread_mutex_unlock(&adc_mutex); - return (value * 3.0f)/1000.0f; + return (((float) value) * 3.0f)/1000.0f; } -float platform_getMicLevel() +uint8_t platform_getMicLevel() { - float value = 0.0f; pthread_mutex_lock(&adc_mutex); - value = adc0_getMeasurement(3); + uint16_t value = adc0_getRawSample(3); pthread_mutex_unlock(&adc_mutex); - return value; + /* Value from ADC is 12 bit wide: shift right by four to get 0 - 255 */ + return value >> 4; } -float platform_getVolumeLevel() +uint8_t platform_getVolumeLevel() { /* TODO */ - return 0.0f; + return 0; } int8_t platform_getChSelector() diff --git a/platform/targets/MD-3x0/platform.c b/platform/targets/MD-3x0/platform.c index 1fd45728..5efbbbb1 100644 --- a/platform/targets/MD-3x0/platform.c +++ b/platform/targets/MD-3x0/platform.c @@ -99,14 +99,24 @@ float platform_getVbat() return adc1_getMeasurement(ADC_VBAT_CH)*3.0f/1000.0f; } -float platform_getMicLevel() +uint8_t platform_getMicLevel() { - return adc1_getMeasurement(ADC_VOX_CH); + /* Value from ADC is 12 bit wide: shift right by four to get 0 - 255 */ + return (adc1_getRawSample(ADC_VOX_CH) >> 4); } -float platform_getVolumeLevel() +uint8_t platform_getVolumeLevel() { - return adc1_getMeasurement(ADC_VOL_CH); + /* + * Knob position corresponds to an analog signal in the range 0 - 1600mV, + * converted to a value in range 0 - 255 using fixed point math: divide by + * 1600 and then multiply by 256. + */ + uint16_t value = adc1_getMeasurement(ADC_VOL_CH); + if(value > 1599) value = 1599; + uint32_t level = value << 16; + level /= 1600; + return ((uint8_t) (level >> 8)); } int8_t platform_getChSelector() diff --git a/platform/targets/MD-9600/platform.c b/platform/targets/MD-9600/platform.c index 7d5832bd..57b5a8e0 100644 --- a/platform/targets/MD-9600/platform.c +++ b/platform/targets/MD-9600/platform.c @@ -114,14 +114,24 @@ float platform_getVbat() return (adc1_getMeasurement(ADC_VBAT_CH)*5.7f)/1000.0f; } -float platform_getMicLevel() +uint8_t platform_getMicLevel() { - return 0.0f; + /* Value from ADC is 12 bit wide: shift right by four to get 0 - 255 */ + return (adc1_getRawSample(ADC_VOX_CH) >> 4); } -float platform_getVolumeLevel() +uint8_t platform_getVolumeLevel() { - return 0.0f; + /* + * Knob position corresponds to an analog signal in the range 0 - 1600mV, + * converted to a value in range 0 - 255 using fixed point math: divide by + * 1600 and then multiply by 256. + */ + uint16_t value = adc1_getMeasurement(ADC_VOL_CH); + if(value > 1599) value = 1599; + uint32_t level = value << 16; + level /= 1600; + return ((uint8_t) (level >> 8)); } bool platform_getPttStatus() diff --git a/platform/targets/MD-UV3x0/platform.c b/platform/targets/MD-UV3x0/platform.c index 343ca14b..00d34547 100644 --- a/platform/targets/MD-UV3x0/platform.c +++ b/platform/targets/MD-UV3x0/platform.c @@ -102,14 +102,24 @@ float platform_getVbat() return adc1_getMeasurement(ADC_VBAT_CH)*3.0f/1000.0f; } -float platform_getMicLevel() +uint8_t platform_getMicLevel() { - return 0.0f; + /* Value from ADC is 12 bit wide: shift right by four to get 0 - 255 */ + return (adc1_getRawSample(ADC_VOX_CH) >> 4); } -float platform_getVolumeLevel() +uint8_t platform_getVolumeLevel() { - return adc1_getMeasurement(ADC_VOL_CH); + /* + * Knob position corresponds to an analog signal in the range 0 - 1600mV, + * converted to a value in range 0 - 255 using fixed point math: divide by + * 1600 and then multiply by 256. + */ + uint16_t value = adc1_getMeasurement(ADC_VOL_CH); + if(value > 1599) value = 1599; + uint32_t level = value << 16; + level /= 1600; + return ((uint8_t) (level >> 8)); } bool platform_getPttStatus() diff --git a/platform/targets/linux/platform.c b/platform/targets/linux/platform.c index b0fdbb56..99215fb0 100644 --- a/platform/targets/linux/platform.c +++ b/platform/targets/linux/platform.c @@ -29,13 +29,13 @@ void platform_init() // Fill hwinfo struct memset(&hwInfo, 0x00, sizeof(hwInfo)); snprintf(hwInfo.name, 10, "Linux"); - // Frequencies are in MHz - hwInfo.vhf_maxFreq = 174; - hwInfo.vhf_minFreq = 136; - hwInfo.vhf_band = 1; - hwInfo.uhf_maxFreq = 480; - hwInfo.uhf_minFreq = 400; - hwInfo.uhf_band = 1; + // Frequencies are in MHz + hwInfo.vhf_maxFreq = 174; + hwInfo.vhf_minFreq = 136; + hwInfo.vhf_band = 1; + hwInfo.uhf_maxFreq = 480; + hwInfo.uhf_minFreq = 400; + hwInfo.uhf_band = 1; emulator_start(); } @@ -57,15 +57,23 @@ float platform_getVbat() } -float platform_getMicLevel() +uint8_t platform_getMicLevel() { - return Radio_State.micLevel; + float level = Radio_State.micLevel; + if(level < 0.0f) level = 0.0f; + if(level > 255.0f) level = 255.0f; + + return ((uint8_t) level); } -float platform_getVolumeLevel() +uint8_t platform_getVolumeLevel() { - return Radio_State.volumeLevel; + float level = Radio_State.volumeLevel; + if(level < 0.0f) level = 0.0f; + if(level > 255.0f) level = 255.0f; + + return ((uint8_t) level); }