Ai-Thinker-Open / GPRS_C_SDK

Ai-Thinker A9/A9G GPRS (with GPS(A9G)) module C development SDK
https://ai-thinker-open.github.io/GPRS_C_SDK_DOC
MIT License
449 stars 236 forks source link

Using API_EVENT_ID_POWER_INFO ChatGpt Generate Solution #555

Open Trion opened 3 months ago

Trion commented 3 months ago

SDK Version : Latest File location : api_event.h & api_inc_pm.h

typedef enum{ PM_CHARGER_STATE_DISCONNECTED = 0, PM_CHARGER_STATE_CONNECTED = 1, PM_CHARGER_STATE_CHRGING = 2, PM_CHARGER_STATE_FINISHED = 3, PM_CHARGER_STATE_ERROR_TEMPERATURE = 4, PM_CHARGER_STATE_ERROR_VOLTAGE = 5, PM_CHARGER_STATE_ERROR_UNKNOWN = 9, PM_CHARGER_STATE_MAX }PM_Charger_State_t;

api_event.h API_EVENT_ID_POWER_INFO (PM_Charger_State_t<<16|charge_level(%)) , param2: (PM_Battery_State_t<<16|battery_voltage(mV)) (log output result in traccer:
Battery_AsyncEventProcess nBatteryChargeState = 3,nBatteryChargeLevel = 98,nBatteryState = 0,nBatteryVoltage = 4187 )

Getting Charger State how to solve it? generate chatgpt solution for beginner

To handle API_EVENT_ID_POWER_INFO and extract the charger state in the GPRS C SDK, you need to interpret the parameters param1 and param2 correctly. These parameters are bit-packed to provide multiple pieces of information. Here’s how you can extract the charger state from param1.

Breakdown of Parameters

Here is an example of how you can extract the charger state from param1:

#include <stdio.h>
#include "api_event.h"
// Example enum for PM_Charger_State_t
typedef enum {
    CHARGER_STATE_DISCONNECTED = 0,
    CHARGER_STATE_CONNECTED,
    CHARGER_STATE_CHARGING,
    CHARGER_STATE_FULL,
    // Add other states as per your SDK documentation
} PM_Charger_State_t;
// Event handler function
void EventDispatch(API_Event_t* pEvent) {
    if (pEvent->id == API_EVENT_ID_POWER_INFO) {
        // Extract charger state and charge level from param1
        uint16_t chargerState = (pEvent->param1 >> 16) & 0xFFFF;
        uint16_t chargeLevel = pEvent->param1 & 0xFFFF;
      // Extract battery state and battery voltage from param2
        uint16_t batteryState = (pEvent->param2 >> 16) & 0xFFFF;
        uint16_t batteryVoltage = pEvent->param2 & 0xFFFF;
        // Print extracted values
        printf("Charger State: %d\n", chargerState);
        printf("Charge Level: %d%%\n", chargeLevel);
        printf("Battery State: %d\n", batteryState);
        printf("Battery Voltage: %dmV\n", batteryVoltage);
        // Process charger state
        switch(chargerState) {
            case CHARGER_STATE_DISCONNECTED:
                printf("Charger is disconnected.\n");
                break;
            case CHARGER_STATE_CONNECTED:
                printf("Charger is connected.\n");
                break;
            case CHARGER_STATE_CHARGING:
                printf("Charging in progress.\n");
                break;
            case CHARGER_STATE_FULL:
                printf("Battery is fully charged.\n");
                break;
            default:
                printf("Unknown charger state.\n");
                break;
        }
    } else {
        printf("Other Event Received\n");
    }
}
int main() {
    // Initialize the system and register the event handler
    API_Event_Register(EventDispatch);
    // Main loop
    while (1) {
        // Perform other tasks
    }
    return 0;
}

Explanation

Extract Charger State: uint16_t chargerState = (pEvent->param1 >> 16) & 0xFFFF;

Extract Charge Level: uint16_t chargeLevel = pEvent->param1 & 0xFFFF;

Extract Battery State: uint16_t batteryState = (pEvent->param2 >> 16) & 0xFFFF;

Extract Battery Voltage: uint16_t batteryVoltage = pEvent->param2 & 0xFFFF;

By correctly interpreting these bit-packed parameters, you can extract and handle the necessary power information in your application. Make sure to adapt the PM_Charger_State_t enum and the handling logic as per your SDK documentation.

Trion commented 3 months ago

chargingstateOk

Trion commented 3 months ago

Test Code

/*

include "stdint.h"

include "stdbool.h"

include "api_os.h"

include "api_event.h"

include "api_debug.h"

include "api_hal_pm.h"

include "api_key.h"

define AppMain_TASK_STACK_SIZE (1024 * 2)

define AppMain_TASK_PRIORITY 1

HANDLE mainTaskHandle = NULL; HANDLE otherTaskHandle = NULL; uint16_t chargerState ; uint16_t chargeLevel ; uint16_t batteryState ; uint16_t batteryVoltage;

void LoopTask(VOID pData) { uint64_t count = 0; while(1) { ++count; if(count == 3000) { count = 0; Trace(1,"Test Test"); OS_Sleep(1000); Trace(1,"Test Test2"); } } } void EventDispatch(API_Event_t pEvent) { switch(pEvent->id) { case API_EVENT_ID_POWER_ON: break; case API_EVENT_ID_NO_SIMCARD: break; case API_EVENT_ID_NETWORK_REGISTERED_HOME: case API_EVENT_ID_NETWORK_REGISTERED_ROAMING: break; case API_EVENT_ID_POWER_INFO: chargerState = (pEvent->param1 >> 16) & 0xFFFF; chargeLevel = pEvent->param1 & 0xFFFF; batteryState = (pEvent->param2 >> 16) & 0xFFFF; batteryVoltage = pEvent->param2 & 0xFFFF; Trace(1,"Charger State: %d",chargerState); Trace(1,"Charger Level: %d",chargeLevel); Trace(1,"battery State: %d",batteryState); Trace(1,"battery Voltage: %d",batteryVoltage); switch(chargerState){ case PM_CHARGER_STATE_DISCONNECTED: Trace(1,"Charger is disconnected"); break; case PM_CHARGER_STATE_CONNECTED: Trace(1,"Charger is connected"); break; case PM_CHARGER_STATE_CHRGING: Trace(1,"Charging is progress"); break; case PM_CHARGER_STATE_FINISHED: Trace(1,"Battery is fully charged"); break;
} default: break; } } void AppMainTask(VOID pData) { API_Event_t event=NULL; otherTaskHandle = OS_CreateTask(LoopTask , NULL, NULL, AppMain_TASK_STACK_SIZE, AppMain_TASK_PRIORITY, 0, 0, "ohter Task"); while(1) { if(OS_WaitEvent(mainTaskHandle, &event, OS_TIME_OUT_WAIT_FOREVER)) { EventDispatch(event); OS_Free(event->pParam1); OS_Free(event->pParam2); OS_Free(event); } } } void app_Main(void) { mainTaskHandle = OS_CreateTask(AppMainTask , NULL, NULL, AppMain_TASK_STACK_SIZE, AppMain_TASK_PRIORITY, 0, 0, "init Task"); OS_SetUserMainHandle(&mainTaskHandle); }