2020-11-02 08:04:19 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-11-02 08:04:19 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::pwm {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constinit os::SdkMutex g_init_mutex;
|
|
|
|
constinit int g_initialize_count = 0;
|
|
|
|
|
2021-01-17 16:55:32 +01:00
|
|
|
ams::sf::SharedPointer<pwm::sf::IManager> g_pwm_manager;
|
2020-11-02 08:04:19 +01:00
|
|
|
|
2021-01-17 16:55:32 +01:00
|
|
|
pwm::sf::IChannelSession *GetInterface(const ChannelSession &session) {
|
2020-11-02 08:04:19 +01:00
|
|
|
AMS_ASSERT(session._session != nullptr);
|
2021-01-17 16:55:32 +01:00
|
|
|
return static_cast<pwm::sf::IChannelSession *>(session._session);
|
2020-11-02 08:04:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 16:55:32 +01:00
|
|
|
void InitializeWith(ams::sf::SharedPointer<pwm::sf::IManager> sp) {
|
2020-11-02 08:04:19 +01:00
|
|
|
std::scoped_lock lk(g_init_mutex);
|
|
|
|
|
|
|
|
AMS_ABORT_UNLESS(g_initialize_count == 0);
|
|
|
|
|
2021-01-17 16:55:32 +01:00
|
|
|
g_pwm_manager = sp;
|
2020-11-02 08:04:19 +01:00
|
|
|
|
|
|
|
g_initialize_count = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Finalize() {
|
|
|
|
std::scoped_lock lk(g_init_mutex);
|
|
|
|
|
|
|
|
AMS_ASSERT(g_initialize_count > 0);
|
|
|
|
|
|
|
|
if ((--g_initialize_count) == 0) {
|
2021-01-17 16:55:32 +01:00
|
|
|
g_pwm_manager.Reset();
|
2020-11-02 08:04:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Result OpenSession(ChannelSession *out, DeviceCode device_code) {
|
|
|
|
/* Get the session. */
|
2021-01-17 16:55:32 +01:00
|
|
|
ams::sf::SharedPointer<pwm::sf::IChannelSession> session;
|
2020-11-02 08:04:19 +01:00
|
|
|
{
|
|
|
|
if (hos::GetVersion() >= hos::Version_6_0_0) {
|
2021-01-17 16:55:32 +01:00
|
|
|
R_TRY(g_pwm_manager->OpenSession2(std::addressof(session), device_code));
|
2020-11-02 08:04:19 +01:00
|
|
|
} else {
|
2021-01-17 16:55:32 +01:00
|
|
|
R_TRY(g_pwm_manager->OpenSession(std::addressof(session), ConvertToChannelName(device_code)));
|
2020-11-02 08:04:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set output. */
|
2021-01-17 16:55:32 +01:00
|
|
|
out->_session = session.Detach();
|
2020-11-02 08:04:19 +01:00
|
|
|
|
|
|
|
/* We succeeded. */
|
2022-03-26 08:14:36 +01:00
|
|
|
R_SUCCEED();
|
2020-11-02 08:04:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CloseSession(ChannelSession &session) {
|
|
|
|
/* Close the session. */
|
2021-01-17 16:55:32 +01:00
|
|
|
ams::sf::ReleaseSharedObject(GetInterface(session));
|
2020-11-02 08:04:19 +01:00
|
|
|
session._session = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetPeriod(ChannelSession &session, TimeSpan period) {
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->SetPeriod(period));
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeSpan GetPeriod(ChannelSession &session) {
|
|
|
|
TimeSpanType out_val;
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->GetPeriod(std::addressof(out_val)));
|
|
|
|
return out_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetDuty(ChannelSession &session, int duty) {
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->SetDuty(duty));
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetDuty(ChannelSession &session) {
|
|
|
|
int out_val;
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->GetDuty(std::addressof(out_val)));
|
|
|
|
return out_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetEnabled(ChannelSession &session, bool en) {
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->SetEnabled(en));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetEnabled(ChannelSession &session) {
|
|
|
|
bool out_val;
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->GetEnabled(std::addressof(out_val)));
|
|
|
|
return out_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetScale(ChannelSession &session, double scale) {
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->SetScale(scale));
|
|
|
|
}
|
|
|
|
|
|
|
|
double GetScale(ChannelSession &session) {
|
|
|
|
double out_val;
|
|
|
|
R_ABORT_UNLESS(GetInterface(session)->GetScale(std::addressof(out_val)));
|
|
|
|
return out_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|