diff --git a/nx/include/switch/services/pwm.h b/nx/include/switch/services/pwm.h new file mode 100644 index 000000000..f0e78b957 --- /dev/null +++ b/nx/include/switch/services/pwm.h @@ -0,0 +1,38 @@ +/** + * @file pwm.h + * @author MasaGratoR + * @copyright libnx Authors + */ + +#pragma once + +#include "../types.h" +#include "../sf/service.h" + +typedef struct { + Service s; +} PwmChannelSession; + +typedef enum { + PwmChannelDeviceCode_CpuFan = 0x3D000001, + PwmChannelDeviceCode_LcdBacklight = 0x3400003D, + PwmChannelDeviceCode_Led = 0x35000065 +} PwmChannelDeviceCode; + +/// Initialize pwm. +Result pwmInitialize(void); + +/// Exit pwm. +void pwmExit(void); + +/// Gets the Service for pwm. +Service* pwmGetServiceSession(void); + +/// Takes a PwmChannelDeviceCode and returns a PwmChannelSession. Only available on [6.0.0+]. +Result pwmOpenSession2(PwmChannelSession *out, PwmChannelDeviceCode device_code); + +/// Closes a PwmChannelSession. +void pwmChannelSessionClose(PwmChannelSession *c); + +/// Takes a PwmChannelSession, returns an output double. Only available on [6.0.0+]. +Result pwmChannelSessionGetDutyCycle(PwmChannelSession *c, double* out); diff --git a/nx/source/services/pwm.c b/nx/source/services/pwm.c new file mode 100644 index 000000000..b3e138541 --- /dev/null +++ b/nx/source/services/pwm.c @@ -0,0 +1,46 @@ +#define NX_SERVICE_ASSUME_NON_DOMAIN +#include "service_guard.h" +#include "services/pwm.h" +#include "runtime/hosversion.h" + +static Service g_pwmSrv; + +NX_GENERATE_SERVICE_GUARD(pwm); + +Result _pwmInitialize(void) { + return smGetService(&g_pwmSrv, "pwm"); +} + +void _pwmCleanup(void) { + serviceClose(&g_pwmSrv); +} + +Service* pwmGetServiceSession(void) { + return &g_pwmSrv; +} + +Result pwmOpenSession2(PwmChannelSession *out, PwmChannelDeviceCode device_code) { + if (hosversionBefore(6,0,0)) + return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); + + u32 tmp = device_code; + + return serviceDispatchIn(&g_pwmSrv, 2, tmp, + .out_num_objects = 1, + .out_objects = &out->s, + ); +} + +void pwmChannelSessionClose(PwmChannelSession *c) { + serviceClose(&c->s); +} + +Result pwmChannelSessionGetDutyCycle(PwmChannelSession *c, double* out) { + if (hosversionBefore(6,0,0)) + return MAKERESULT(Module_Libnx, LibnxError_IncompatSysVer); + + if (!serviceIsActive(&c->s)) + return MAKERESULT(Module_Libnx, LibnxError_NotInitialized); + + return serviceDispatchOut(&c->s, 7, *out); +}