Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using API_EVENT_ID_POWER_INFO ChatGpt Generate Solution #555

Open
Trion opened this issue Aug 6, 2024 · 2 comments
Open

Using API_EVENT_ID_POWER_INFO ChatGpt Generate Solution #555

Trion opened this issue Aug 6, 2024 · 2 comments

Comments

@Trion
Copy link

Trion commented Aug 6, 2024

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

  • param1: Contains the charger state and the charge level.
  • Charger State: PM_Charger_State_t (upper 16 bits)
  • Charge Level: Charge level in percentage (lower 16 bits)
  • param2: Contains the battery state and the battery voltage.
  • Battery State: PM_Battery_State_t (upper 16 bits)
  • Battery Voltage: Battery voltage in millivolts (lower 16 bits)

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;

  • Shifts param1 right by 16 bits to get the upper 16 bits.
  • Masks with 0xFFFF to ensure we get the correct value.

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

  • Masks param1 with 0xFFFF to get the lower 16 bits.

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

  • Similar process as extracting charger state but from param2.

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

  • Similar process as extracting charge level but from param2.

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
Copy link
Author

Trion commented Aug 7, 2024

chargingstateOk

@Trion
Copy link
Author

Trion commented Aug 7, 2024

Test Code

/*

  • @file app_main.c
  • @brief An example of SDK's mini system
  • @author: Trion
  • @Date: 2024-8-7 11:22
  • @last Modified by: Trion
  • @last Modified time:2024-8-7 august 11:22 AM
    */

#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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant