universal7885: fm-aidl: Handle middle states for manual freq setting

Also move FM support instance to vector cached
This commit is contained in:
roynatech2544 2022-10-21 22:35:09 +09:00
commit b9c6e96e83
7 changed files with 63 additions and 5 deletions

View file

@ -11,6 +11,7 @@ cc_binary {
srcs: [
"FMSupport.cpp",
"FMDevControl.cpp",
"MiddleState.cpp",
"service.cpp",
],
defaults: [

View file

@ -54,11 +54,21 @@ namespace aidl::vendor::eureka::hardware::fmradio {
break;
case GetType::GET_TYPE_FM_BEFORE_CHANNEL:
if (index > 0) index -= 1;
if (kMiddleState != nullptr) {
index = kMiddleState->first;
delete kMiddleState;
kMiddleState = nullptr;
}
fm_radio_slsi::set_frequency(fd, freqs_list[index]);
*_aidl_return = freqs_list[index];
break;
case GetType::GET_TYPE_FM_NEXT_CHANNEL:
if (index < freqs_list.size() - 1) index += 1;
if (kMiddleState != nullptr) {
index = kMiddleState->second;
delete kMiddleState;
kMiddleState = nullptr;
}
fm_radio_slsi::set_frequency(fd, freqs_list[index]);
*_aidl_return = freqs_list[index];
break;
@ -86,6 +96,8 @@ namespace aidl::vendor::eureka::hardware::fmradio {
switch (type) {
case SetType::SET_TYPE_FM_FREQ:
fm_radio_slsi::set_frequency(fd, value);
if (std::find(freqs_list.begin(), freqs_list.end(), value) == freqs_list.end())
kMiddleState = saveMiddleState(value, freqs_list);
break;
case SetType::SET_TYPE_FM_MUTE:
fm_radio_slsi::set_mute(fd, value);

View file

@ -19,6 +19,8 @@
#include <mutex>
#include <thread>
#include "MiddleState.h"
namespace aidl::vendor::eureka::hardware::fmradio {
struct FMDevControl : public BnFMDevControl {
@ -38,5 +40,6 @@ struct FMDevControl : public BnFMDevControl {
std::timed_mutex lock;
std::thread search_thread;
std::vector<int> freqs_list;
middlestate_t *kMiddleState;
};
} // namespace aidl::vendor::eureka::hardware::fmradio

View file

@ -43,12 +43,24 @@ constexpr const char *FM_FREQ_SEEK = FM_SYSFS_BASE "/radio_freq_seek";
case GetType::GET_TYPE_FM_RMSSI:
NOT_SUPPORTED;
case GetType::GET_TYPE_FM_BEFORE_CHANNEL:
FileIO::writeline(FM_FREQ_SEEK, "0 " + std::to_string(SYSFS_SPACING * 10));
*_aidl_return = FileIO::readline(FM_FREQ_CTL);
if (index > 0) index -= 1;
if (kMiddleState != nullptr) {
index = kMiddleState->first;
delete kMiddleState;
kMiddleState = nullptr;
}
FileIO::writeline(FM_FREQ_CTL, freqs_list[index]);
*_aidl_return = freqs_list[index];
break;
case GetType::GET_TYPE_FM_NEXT_CHANNEL:
FileIO::writeline(FM_FREQ_SEEK, "1 " + std::to_string(SYSFS_SPACING * 10));
*_aidl_return = FileIO::readline(FM_FREQ_CTL);
if (index < freqs_list.size() - 1) index += 1;
if (kMiddleState != nullptr) {
index = kMiddleState->second;
delete kMiddleState;
kMiddleState = nullptr;
}
FileIO::writeline(FM_FREQ_CTL, freqs_list[index]);
*_aidl_return = freqs_list[index];
break;
case GetType::GET_TYPE_FM_SYSFS_IF:
*_aidl_return = access(FM_SYSFS_BASE, F_OK);
@ -72,6 +84,8 @@ constexpr const char *FM_FREQ_SEEK = FM_SYSFS_BASE "/radio_freq_seek";
switch (type) {
case SetType::SET_TYPE_FM_FREQ:
FileIO::writeline(FM_FREQ_CTL, value * 1000);
if (std::find(freqs_list.begin(), freqs_list.end(), value) == freqs_list.end())
kMiddleState = saveMiddleState(value, freqs_list);
break;
case SetType::SET_TYPE_FM_MUTE:
case SetType::SET_TYPE_FM_VOLUME:

View file

@ -18,6 +18,7 @@
#include <mutex>
#include <thread>
#include "MiddleState.h"
namespace aidl::vendor::eureka::hardware::fmradio {
@ -36,6 +37,7 @@ public:
std::timed_mutex lock;
std::thread search_thread;
std::vector<int> freqs_list;
int index;
unsigned int index;
middlestate_t *kMiddleState;
};
} // namespace aidl::vendor::eureka::hardware::fmradio

View file

@ -0,0 +1,14 @@
#include "MiddleState.h"
middlestate_t *saveMiddleState(const int value, const std::vector<int> &vec)
{
unsigned int i;
for (i = 0; i < vec.size() - 1; i++) {
const int first = vec[i] - value;
const int second = vec[i + 1] - value;
if (first * second < 0)
break;
}
return new middlestate_t {i, i + 1};
}

View file

@ -0,0 +1,12 @@
#include <vector>
#pragma once
struct pair {
unsigned int first;
unsigned int second;
};
using middlestate_t = struct pair;
middlestate_t *saveMiddleState(const int value, const std::vector<int> &vec);