static portTASK_FUNCTION(RFIDTask, pvParameters) { /* Write your task initialization code here ... */ PCD_Init(); vTaskDelay(500/portTICK_RATE_MS); for (;;) { /* Write your task code here ... */ mfrc522_loop(); /* You can use a task delay like vTaskDelay(1000/portTICK_RATE_MS); */ } /* Destroy the task */ vTaskDelete(RFIDTask); }
int main_controller_tick(int state){ static unsigned short main_hold_time = 0; switch(state){//main_task transitions/actions case st_main_start: state = st_main_init; break; case st_main_init: SPI_Master_Init(); PCD_Init(); state = st_main_wait_new; QueueEnqueue(lcd_command_queue, lcd_write_ready); break; case st_main_wait_new: if(PICC_IsNewCardPresent()){ storeTagID(); PICC_HaltA(); state = st_main_checkID; } else { state = st_main_wait_new; } break; case st_main_checkID: if(isMaster(tag_id)){ state = st_main_master; QueueEnqueue(lcd_command_queue, lcd_write_master); } else if(search_valid_tag(tag_id) != -1){ state = st_main_hold; main_hold_time = 0; if(current_lock_position == locked || current_lock_position == malfunction){ QueueEnqueue(lock_command_queue, lc_unlock); } else if(current_lock_position == unlocked){ QueueEnqueue(lock_command_queue, lc_lock); } } else { state = st_main_hold; main_hold_time = 0; QueueEnqueue(lcd_command_queue, lcd_write_invalid); } break; case st_main_master: if(!(PIND & 0x10)){ if(isTagDBFull()){ state = st_main_hold; main_hold_time = 0; QueueEnqueue(lcd_command_queue, lcd_write_add_full); } else { state = st_main_add_pressed; QueueEnqueue(lcd_command_queue, lcd_write_add); } } else if(!(PIND & 0x40)){ if(isTagDBEmpty()){ state = st_main_hold; main_hold_time = 0; QueueEnqueue(lcd_command_queue, lcd_write_remove_empty); } else { state = st_main_rm_pressed; current_remove_option = 0; QueueEnqueue(lcd_command_queue, lcd_write_remove); } } else { state = st_main_master; } break; case st_main_add_pressed: if(PIND & 0x10){ state = st_main_add_tag; } else { state = st_main_add_pressed; } break; case st_main_add_tag: if(PICC_IsNewCardPresent()){ storeTagID(); PICC_HaltA(); if(isMaster(tag_id)){ state = st_main_add_tag; } else if(search_valid_tag(tag_id) == -1){ state = st_main_hold; main_hold_time = 0; add_valid_tag(tag_id); QueueEnqueue(lcd_command_queue, lcd_write_added); } else { state = st_main_hold; main_hold_time = 0; QueueEnqueue(lcd_command_queue, lcd_write_add_exist); } } else { state = st_main_add_tag; } break; case st_main_rm_pressed: if(PIND & 0x40){ state = st_main_rm_tag; } else { state = st_main_rm_pressed; } break; case st_main_rm_tag: if(!(PIND & 0x10)){ current_remove_option = (current_remove_option + (numValidKeys - 1)) % numValidKeys; QueueEnqueue(lcd_command_queue, lcd_write_remove); state = st_main_lr_pressed; } else if(!(PIND & 0x40)){ current_remove_option = (current_remove_option + 1) % numValidKeys; QueueEnqueue(lcd_command_queue, lcd_write_remove); state = st_main_lr_pressed; } else if(!(PIND & 0x20)){ remove_valid_tag(current_remove_option); QueueEnqueue(lcd_command_queue, lcd_write_removed); state = st_main_hold; main_hold_time = 0; } else { state = st_main_rm_tag; } break; case st_main_lr_pressed: if( (PIND & 0x10) && (PIND & 0x40) ){ state = st_main_rm_tag; } else { state = st_main_lr_pressed; } break; case st_main_hold: if(main_hold_time < 20){ main_hold_time++; state = st_main_hold; } else { state = st_main_wait_new; } break; default: state = st_main_start; break; }//end main_task transitions/actions return state; };