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

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

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:

  1. bool isChargerPluggedin() {
  2. bool chargeState;
  3. PTMU_GetAdapterState(&chargeState);
  4. return chargeState == 1;
  5. }

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

  1. bool isChargerPluggedin() {
  2. bool *chargeState = new bool;
  3. PTMU_GetAdapterState(chargeState);
  4. bool state = *chargeState == 1;
  5. delete chargeState;
  6. return state;
  7. }

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

  1. bool chargeState;
  2. bool isChargerPluggedin() {
  3. PTMU_GetAdapterState(&chargeState);
  4. return chargeState == 1;
  5. }
英文:

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:

  1. bool isChargerPluggedin() {
  2. bool chargeState;
  3. PTMU_GetAdapterState(&chargeState);
  4. return chargeState == 1;
  5. }

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

  1. bool isChargerPluggedin() {
  2. bool *chargeState = new bool;
  3. PTMU_GetAdapterState(chargeState);
  4. bool state = *chargeState == 1;
  5. delete chargeState;
  6. return state;
  7. }

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

  1. bool chargeState;
  2. bool isChargerPluggedin() {
  3. PTMU_GetAdapterState(&chargeState);
  4. return chargeState == 1;
  5. }

答案1

得分: 0

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

  1. int main() {
  2. ptmuInit();
  3. // 程序循环
  4. while (true) {
  5. bool isPlugged = isChargerPluggedin();
  6. ...
  7. }
  8. ptmuExit();
  9. }
英文:

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:

  1. int main() {
  2. ptmuInit();
  3. // program loop
  4. while (true) {
  5. bool isPlugged = isChargerPluggedin();
  6. ...
  7. }
  8. ptmuExit();
  9. }

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:

确定