如何检查3DS充电器是否已插入?

huangapple go评论69阅读模式
英文:

How do I check if the 3ds charger is plugged in?

问题

I am writing a function to know whether or not the charger is plugged in my 3DS. However, my function keeps returning false while the charger is plugged in.

I expect my function to return true if the charger is plugged into my 3DS.

Here is the function:

bool isChargerPluggedin() {
    bool chargeState;
    PTMU_GetAdapterState(&chargeState);
    return chargeState == 1;
}

My first attempt to solve my problem was to allocate memory to the chargeState variable:

bool isChargerPluggedin() {
    bool *chargeState = new bool;
    PTMU_GetAdapterState(chargeState);
    bool state = *chargeState == 1;
    delete chargeState;
    return state;
}

My second attempt was by using a global variable for chargeState instead of a local variable:

bool chargeState;
bool isChargerPluggedin() {
    PTMU_GetAdapterState(&chargeState);
    return chargeState == 1;
}
英文:

I am writting a function to know wether or not the charger is plugged in my 3ds however my function keep returning false while the charger is plugged in.

I expect my function to return true if the charger is plugged in my 3ds.

Here is the function:

bool isChargerPluggedin() {
    bool chargeState;
    PTMU_GetAdapterState(&chargeState);
    return chargeState == 1;
}

My first attempt to solve my problem was to allocate memory to the chargeState variable:

bool isChargerPluggedin() {
    bool *chargeState = new bool;
    PTMU_GetAdapterState(chargeState);
    bool state = *chargeState == 1;
    delete chargeState;
    return state;
}

My second attempt was by using an global variable to chargeState instead of a local variable :

bool chargeState;
bool isChargerPluggedin() {
    PTMU_GetAdapterState(&chargeState);
    return chargeState == 1;
}

答案1

得分: 0

为了解决这个问题,你必须在你的主函数中首先使用函数 ptmuInit 初始化库。在程序退出之前,你必须反初始化这个库。以下是一个示例:

int main() {
  ptmuInit();
  // 程序循环
  while (true) {
    bool isPlugged = isChargerPluggedin();
    ...
  }
  ptmuExit();
}
英文:

To fix this, you must initialize first the libraries in your main function using the function ptmuInit. You must deinitialize the library before your program exitHere is a example:

int main() {
  ptmuInit();
  // program loop
  while (true) {
    bool isPlugged = isChargerPluggedin();
    ...
  }
  ptmuExit();
}

huangapple
  • 本文由 发表于 2023年2月6日 12:57:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定