Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <api_os.h>
- #include <api_event.h>
- #include <api_hal_gpio.h>
- #include <api_hal_pm.h>
- #define MAIN_TASK_STACK_SIZE (1024 * 2)
- #define MAIN_TASK_PRIORITY 0
- #define MAIN_TASK_NAME "Main Task"
- static HANDLE mainTaskHandle = NULL;
- static uint8_t preSmsEventsCount = 0;
- int frequency = 0;
- void EventDispatch(API_Event_t* pEvent) {
- switch(pEvent->id) {
- case API_EVENT_ID_NO_SIMCARD:
- Trace(10,"!!NO SIM CARD%d!!!!",pEvent->param1);
- break;
- case API_EVENT_ID_SYSTEM_READY:
- Trace(1,"system initialize complete");
- preSmsEventsCount |= 1;
- break;
- case API_EVENT_ID_NETWORK_REGISTERED_HOME:
- case API_EVENT_ID_NETWORK_REGISTERED_ROAMING:
- Trace(2,"network register success");
- preSmsEventsCount |= 2;
- Network_StartAttach();
- break;
- case API_EVENT_ID_NETWORK_ATTACHED:
- Trace(2,"network attach success");
- Network_PDP_Context_t context = {
- .apn ="internet",
- .userName = "",
- .userPasswd = ""
- };
- Network_StartActive(context);
- break;
- case API_EVENT_ID_NETWORK_ACTIVATED:
- Trace(2,"network activate success");
- break;
- default:
- break;
- }
- }
- void OnPinFalling(GPIO_INT_callback_param_t* param) {
- // Trace(1,"OnPinFalling");
- switch(param->pin) {
- case GPIO_PIN3:
- frequency++;
- break;
- default:
- break;
- }
- }
- void sampleSpeed(void* param) {
- Trace(1, "Frequency: %d", frequency);
- // reset frequency
- frequency = 0;
- // reschedule the speed sampler for per second
- OS_StartCallbackTimer(mainTaskHandle, 1000, sampleSpeed, NULL);
- }
- void mainTask(void *pData) {
- API_Event_t* event=NULL;
- // enable power for used pins
- PM_PowerEnable(POWER_TYPE_VPAD, true);
- PM_PowerEnable(POWER_TYPE_LCD, true);
- // configure frequency pin
- GPIO_config_t intPin = {
- .mode = GPIO_MODE_INPUT_INT,
- .pin = GPIO_PIN3,
- .defaultLevel = GPIO_LEVEL_LOW,
- .intConfig.debounce = 0,
- .intConfig.type = GPIO_INT_TYPE_FALLING_EDGE,
- .intConfig.callback = OnPinFalling
- };
- GPIO_Init(intPin);
- // schedule the speed sampler for per second
- OS_StartCallbackTimer(mainTaskHandle, 1000, sampleSpeed, NULL);
- // infinite loop
- while(1) {
- // if an event is received
- if(OS_WaitEvent(mainTaskHandle, (void**)&event, OS_TIME_OUT_WAIT_FOREVER)) {
- // signal the event
- EventDispatch(event);
- // clean up
- OS_Free(event->pParam1);
- OS_Free(event->pParam2);
- OS_Free(event);
- }
- }
- }
- void speed_governor_5_Main(void) {
- // create the main task
- mainTaskHandle = OS_CreateTask(mainTask,
- NULL, NULL, MAIN_TASK_STACK_SIZE, MAIN_TASK_PRIORITY, 0, 0, MAIN_TASK_NAME);
- // set up the main task to receive messages
- OS_SetUserMainHandle(&mainTaskHandle);
- }
Advertisement
Add Comment
Please, Sign In to add comment