/** * @brief Main loop for the adc * @param None * @retval None */ void Thread_ADC (void const *argument) { while(1){ osSignalWait (ADC_FLAG,osWaitForever); osSignalClear(tid_Thread_ADC,ADC_FLAG); poll(); } }
/*---------------------------------------------------------------------------- Thread 3 'lights': executes if current time is between start & end time *---------------------------------------------------------------------------*/ void lights (void const *argument) { /* traffic light operation */ SetLights (RED, 1); /* RED & STOP lights on */ SetLights (STOP, 1); SetLights (YELLOW, 0); SetLights (GREEN, 0); SetLights (WALK, 0); while (1) { /* endless loop */ osDelay(500); /* wait for timeout: 500ms */ if (!signalon ()) { /* if traffic signal time over */ tid_blinking = osThreadCreate(osThread(blinking), NULL); /* start blinking */ return; /* STOP lights */ } SetLights (YELLOW, 1); osDelay(500); /* wait for timeout: 500ms */ SetLights (RED, 0); /* GREEN light for cars */ SetLights (YELLOW, 0); SetLights (GREEN, 1); osSignalClear(tid_lights, 0x0010); osDelay(500); /* wait for timeout: 500ms */ osSignalWait(0x0010, 7500); /* wait for event with timeout */ SetLights (YELLOW, 1); /* timeout value: 7.5s */ SetLights (GREEN, 0); osDelay(500); /* wait for timeout: 500ms */ SetLights (RED, 1); /* RED light for cars */ SetLights (YELLOW, 0); osDelay(500); /* wait for timeout: 500ms */ SetLights (STOP, 0); /* GREEN light for WALKers */ SetLights (WALK, 1); osDelay(2500); /* wait for timeout: 2.5s */ SetLights (STOP, 1); /* RED light for WALKers */ SetLights (WALK, 0); } }
/** * @brief Accelerometer thread * @param arguments * @retval None */ void Thread_Accelerometer(void const *argument){ while(1){ osSignalWait (data_ready_flag,osWaitForever); osSignalClear(tid_Thread_Accelerometer,data_ready_flag); calculateAngles(); } }
/** * @brief Thread that read the accelerometer * @param argument: * @retval None */ void Thread_Acc (void const *argument) { while(1) { //Wait for the interrupt to issue a flag before reading the value //0 for the timeout value so that it never starts before the flag osSignalWait(ACC_INT_FLAG, osWaitForever); ReadAcc(); //printf("%f\n",accValue[0]); osSignalClear(tid_Thread_Acc,ACC_INT_FLAG); } }
void xb_ReceiveTask(void const *argument){ for(;;){ osSignalWait(XB_SIG_DATARECEIVED, osWaitForever); //Wait until end of command char arrives osSignalClear(xb_ReceiveTaskId, XB_SIG_DATARECEIVED); while( xb_RxCmdCnt > xb_HandledRxCmdCnt ){ //If there is more received than parsed commands xb_HandledRxCmdCnt++; //Increment handled command counter HandleReceivedData(); //Handle command } } }
/*! @brief Thread to display temperature @param argument Unused */ void temperature_DispFlag_thread(void const * argument) { // Extracts the semaphore object from the argument. // This semaphore object was created in the main osSemaphoreId* semaphore; semaphore = (osSemaphoreId*)argument; uint8_t ResourceLocked = 1; // Flag gets turned on if semaphore_lock achieved uint32_t tokentemp; // Return of semwait // Wait for the semaphore with 1ms timeout osEvent event = osSignalWait(SIGNAL_DISP_TEMPERATURE, 1); while(1) { tokentemp=osSemaphoreWait (*semaphore, osWaitForever); ResourceLocked=1; // turn flag on if resource_locked while (ResourceLocked) { // wait for a signal with 1ms timeout event = osSignalWait(SIGNAL_DISP_TEMPERATURE, 1); if (event.status == osEventTimeout) { // in case of timeout display LCD_DisplayTemperature(temp); } else { // signal received which is not timeout // clear the signal flag for the tilt display thread osSignalClear (tilt_TurnDispFlag_thread, SIGNAL_DISP_TILT); //clear the screen LCD_clear_display(); // turn the resource_locked flag off ResourceLocked = 0; tokentemp=0; // turn the flag to display the string "temperature" once GLB_Temperature_display_flag=1; // release the semaphore osSemaphoreRelease(*semaphore); //wait 5ms for the other thread to grab the semaphore osDelay(5); } } } }
int os_usart1_gets(char *pBuffer){ osMutexWait(usart1_rx_mutex_id, osWaitForever); usart1_rx_owner = osThreadGetId(); osSignalClear(usart1_rx_owner, USART_SIGNAL); usart1_async_gets(pBuffer, _os_usart1_rx_callback); osSignalWait(USART_SIGNAL, osWaitForever); osMutexRelease(usart1_rx_mutex_id); return usart1_rx_nBytesRead; }
void accelerometer_thread(void const *argument){ float angle; while (1){ osSignalWait(ACC_DATA_READY_SIGNAL, osWaitForever); angle = Rangle(); MAIL_send_input(MAIL_ANGLE, k_filter_acc(angle)); osSignalClear(accelerometer_thread_id, ACC_DATA_READY_SIGNAL); } }
void temperature_thread(void const *argument) { float temp; while (1){ osSignalWait(TEMP_DATA_READY_SIGNAL, osWaitForever); temp = getTemp(); MAIL_send_input(MAIL_TEMP, k_filter_temp(temp)); osSignalClear(temperature_thread_id, ACC_DATA_READY_SIGNAL); } }
/*---------------------------------------------------------------------------- * Thread 'Angle_Thread' *---------------------------------------------------------------------------*/ void Thread_angle (void const *argument) { //float temperature = 0.0f; display_temp = 0.0f; display_pitch = 0.0f; display_roll = 0.0f; display_angle_counter = 0; while(1){ osSignalWait(angle_flag, osWaitForever); osMutexWait(angle_mutex, osWaitForever); //Check if it is in angle display mode if (display_mode == 1) { //If the EXTI0 callback function is called and flag is set to active, read accelerometer values if(EXTI0_flag_value == INTERRUPT_ACTIVE_FLAG) { //Get the accelerometer readings get_calibrated_acceleration(); //Store pitch angle for display if ( tilt_selection == 0) { display_pitch = pitch; } //Store roll angle for display if (tilt_selection == 1) { display_roll = roll; } //Slow down the updating of the angle display to 5Hz (25Hz of acc readings/5) if(display_angle_counter % 5 == 0) { //check_temperature_status(display_temp); display = tilt_selection == 0 ? display_pitch: display_roll; display_angle_counter = 0; } //Reset the flag EXTI0_flag_value = 0; } } osMutexRelease(angle_mutex); osSignalClear(tid_Thread_angle, angle_flag); } }
/**---------------------------------------------------------------------------- * Thread 'Thread_angles': set Pitch and Roll display *---------------------------------------------------------------------------*/ void Thread_LED(void const *argument) { currentPin = GPIO_PIN_12; while(1){ osSignalWait(1, osWaitForever); if (speed == 10) { LED_PWM_Init(); setIntensity(intensity); } else { LED_PWM_DeInit(); LED_GPIO_Init(); rotate(); //LED_GPIO_DeInit(); LED_PWM_Init(); } osSignalClear(tid_Thread_LED, 1); } }
/**---------------------------------------------------------------------------- * Thread 'Thread_angles': set Pitch and Roll display *---------------------------------------------------------------------------*/ void Thread_angles (void const *argument) { pitch_mutex = osMutexCreate(osMutex(pitch_mutex)); roll_mutex = osMutexCreate(osMutex(roll_mutex)); while(1){ float out[3], acc[3]; osSignalWait(1, osWaitForever); LIS3DSH_ReadACC(out); /* Calculations based on Eq 8 and 9 in Doc 15*/ /* Y direction is towards mini usb connector, Z is downwards*/ /* Use RHR to digure out X*/ acc[0] = out[0] - XOFFSET; acc[1] = out[1] - YOFFSET; acc[2] = out[2] - ZOFFSET; filtered_acc[0] = kalmanfilter_c(acc[0],&xacc_state); filteredX = filtered_acc[0]; filtered_acc[1] = kalmanfilter_c(acc[1],&yacc_state); filteredY = filtered_acc[1]; filtered_acc[2] = kalmanfilter_c(acc[2],&zacc_state); filteredZ = filtered_acc[2]; osSignalSet(tid_Thread_doubleTap, 1); osMutexWait(pitch_mutex, osWaitForever); pitch = 90 + (180/M_PI)*atan2(filtered_acc[1], sqrt(pow(filtered_acc[0], 2) + pow(filtered_acc[2], 2))); //Pitch in degrees osMutexRelease(pitch_mutex); osMutexWait(roll_mutex, osWaitForever); roll = 90 + (180/M_PI)*atan2(filtered_acc[0], sqrt(pow(filtered_acc[1], 2) + pow(filtered_acc[2], 2))); //Roll in degrees osMutexRelease(roll_mutex); osSignalClear(tid_Thread_angles, 1); } }
netTaskErr getEpochTime(void) { uint8_t count; netTaskErr result = errOK; netStatus sntpStatus, dnsStatus; osEvent statusNtpWait, statusDnsWait; // getting IP of NTP server for (count = 0; count < DNS_WAIT_TRIES; count++) { osSignalClear(netTaskId, FLAG_DNS_RESOLVED); dnsStatus = get_host_by_name (ntpHostName, dnsCBack); if (dnsStatus == netOK) { statusDnsWait = osSignalWait(FLAG_DNS_RESOLVED, DNS_TIMEOUT_MS); if ((statusDnsWait.status == osEventSignal) && ((ntpHostIP[0] != 0) || (ntpHostIP[1] != 0) || (ntpHostIP[2] != 0) || (ntpHostIP[3] != 0)) ) { result = errOK; break; } else { osSignalClear(netTaskId, FLAG_DNS_RESOLVED); result = errDnsTOut; } } else { result = errDnsResolve; } osDelay(DNS_WAIT_DELAY_MS); } if (result != errOK) return result; // requesting Unix time from NTP server for (count = 0; count < SNTP_WAIT_TRIES; count++) { osSignalClear(netTaskId, FLAG_UDP_PACKET_RECV); sntpStatus = sntp_get_time (&ntpHostIP[0], sntpCBack); if (sntpStatus == netOK) { statusNtpWait = osSignalWait(FLAG_UDP_PACKET_RECV, UDP_TIMEOUT_MS); if ((statusNtpWait.status == osEventSignal) && (ntpEpochTime != 0)) { result = errOK; break; } else { osSignalClear(netTaskId, FLAG_UDP_PACKET_RECV); result = errNtpCBackTOut; } } else { result = errNtpNotReady; // SNTP not ready or bad parameters. } osDelay(SNTP_WAIT_DELAY_MS); } return result; }
/* Keyboard Thread Checks the keys on the keyboard. Waits for an OSsignal from the Keyboard pins interrupts before checking what key has been pressed. It has three main states Idle, Record Sequence, or PRT Idle = simply waits for a valid key press Record Sequence = begins recording the boards movements and saves to a buffer PRT = Pitch, Roll, Time waits for User input to send the desired angles to receiver. */ void keyboard_thread(void const *argument) { struct Values *values = (struct Values*)argument; uint8_t keyboard_mode=0; float currentRoll=0; float currentPitch=0; float previousRoll=0; float previousPitch=0; float mstime=0; uint8_t keyCurrent = 0xFF; while(1){ // Wait for keypad Interrupt osSignalWait(0x08, osWaitForever); // Read the current key keyCurrent = readKeyboard(); transmit_locked = 1; // Lock transmitter //printf("Keyboard Value 0x%x\n", keyCurrent); //Record Angle Sequence State if((keyCurrent == D) && (keyboard_mode == KEYBOARD_STATE_IDLE)){ //printf("Recording sequence!!! \n"); float pitchBuffer[256]; float rollBuffer[256]; uint32_t time_ms = 100; // Record sequence int index = 0; //Record 256 samples or 25 seconds of movemement while (index < 256) { pitchBuffer[index] = measurements.pitch; rollBuffer[index] = measurements.roll; //printf("Index = %i Pitch = %f Roll = %f \n",index,pitchBuffer[index],rollBuffer[index]); index++; osDelay(time_ms); //Delay of 100ms } //printf("Sending sequence!!! \n"); //Send the sequence transmit_record_sequence(256, pitchBuffer, rollBuffer, (float) time_ms); //printf("Done transmitting sequence!!! \n"); osDelay(10000); transmit_locked = 0; //Enable Broadcast Mode } //PRT STATE, sends Desired Pitch,Roll and Time else if((keyCurrent==A) || (keyboard_mode== KEYBOARD_STATE_PRT)){ keyboard_mode = KEYBOARD_STATE_PRT; if(keyCurrent == A){ //Reset Variables osMutexWait(measureUpdate, osWaitForever); values->follow=0; previousRoll= values->roll; previousPitch = values->pitch; measurements.rollIncrement = 0; measurements.pitchIncrement = 0; measurements.time = 0; osMutexRelease(measureUpdate); currentRoll=0; //Begin Keypad Transmission Command for (int i = 0; i < 1; i++) transmit_keypad_begin(); //read Roll readValue(¤tRoll, &tid_keyboard); } //Read Pitch on B press if(keyCurrent == B){ currentPitch=0; readValue(¤tPitch, &tid_keyboard); } //Read Time on C press if(keyCurrent == C){ mstime=0; readValue(&mstime, &tid_keyboard); //Convert time to count value and pitch and roll to increment values osMutexWait(measureUpdate, osWaitForever); values->time= (float)(mstime/100); values->rollIncrement = (currentRoll-previousRoll)/(mstime/100); values->pitchIncrement = (currentPitch-previousPitch)/(mstime/100); //printf("Roll %f Pitch %f Time %f ms\n", currentRoll, currentPitch, mstime); // Transmit Data //printf("SENDING pitchIncrement = %f \n",values->pitchIncrement); transmit_keypad_pitch(values->pitchIncrement); osDelay(100); //printf("SENDING rollIncrement = %f \n",values->rollIncrement); transmit_keypad_roll(values->rollIncrement); osDelay(100); //printf("SENDING time = %f \n",values->time); transmit_keypad_time(values->time); osDelay(100); //printf("SENDING END \n"); transmit_keypad_end(); osDelay(100); osMutexRelease(measureUpdate); //osTimerStart(timerid_motor,100); //printf("Time %f\n", values->time); // Start transmitting again after delay osDelay(6000); transmit_locked = 0; // unlock transmit keyboard_mode = KEYBOARD_STATE_IDLE; } } else { transmit_locked = 0; } //Add delay before allowing next keyboard read to avoid debouncing. osDelay(200); osSignalClear(tid_keyboard, 0x08); } }
void TH_GUI (void const *argument) { static volatile uint16_t i=0; static uint16_t Triggered_Sample=0; osEvent evt; GUI_MEMDEV_Handle hMem0; GUI_SelectLayer(0); hMem0 = GUI_MEMDEV_Create(0,0,480,272); if(!GUI_CURSOR_GetState()) GUI_CURSOR_Select(&GUI_CursorCrossM); GUI_CURSOR_Show(); while (1) { Anal_CH1_Set(/*Main loop start*/); Anal_CH2_Set(/*Wait start*/); evt = osSignalWait(DMA_ConvCpltSig,(uint32_t)2); if( evt.status == osEventTimeout) Error_Handler(); Anal_CH2_Reset(/*Wait finish*/); //osSignalSet(tid_Touch,GUI_TouchStateReqSig); Anal_CH3_Set(/*Copy start*/); for(i=0;i<ADC_BUFFER_LENGTH;i++) // <- Temporary. Take the full advantage of DMA ! values_BUF[i]=255-values[i]; Anal_CH3_Reset(/*Copy finish*/); HAL_ADC_Start_DMA(&g_AdcHandle, values, ADC_BUFFER_LENGTH); osSignalClear(tid_TH_GUI, DMA_ConvCpltSig); Anal_CH4_Set(/*Wait start*/); osSignalWait(GUI_TouchGetSig,(uint32_t)0); GUI_CURSOR_SetPosition(g_Touched.pState->x,g_Touched.pState->y); Anal_CH4_Reset(/*Wait finish*/); Trigger_Point = g_Touched.MSG; Triggered_Sample = Trigger(Trigger_Point, values_BUF, ADC_BUFFER_LENGTH, 1348000UL); //if(Triggered_Sample >=20)Triggered_Sample -=20; // Offset to see the edge in the center <- bullshit ? GUI_MEMDEV_Select(hMem0); GUI_MEMDEV_Clear(hMem0); GUI_SetColor(GUI_DARKGRAY); GUI_FillRect(0,0,480,272); GUI_SetColor(GUI_BLACK); GUI_DrawRect(34,5,474,266); GUI_SetColor(GUI_WHITE); GUI_FillRect(35,6,475,266); Draw_GraphGrid(440,260,40,40); GUI_SetColor(GUI_BLUE); /*Draw garph start*/HAL_GPIO_TogglePin(GPIOI,GPIO_PIN_1); GUI_DrawGraph((short*)&values_BUF[Triggered_Sample],440,35,6); // Useful: GUI_COUNTOF(values) /*Draw garph finish*/HAL_GPIO_TogglePin(GPIOI,GPIO_PIN_1); GUI_SetColor(GUI_ORANGE); GUI_DrawHLine(Trigger_Point,0,480); GUI_FillCircle(15,Trigger_Point,10); GUI_SetColor(GUI_YELLOW); GUI_DrawCircle(15,Trigger_Point,10); GUI_MEMDEV_CopyToLCD(hMem0); Anal_CH1_Reset(/*Main loop finish*/); osThreadYield (); // suspend thread } }
int32_t Thread::signal_clr(int32_t signals) { // osSignalClear is thread safe as long as the underlying // thread does not get terminated or return from main return osSignalClear(_tid, signals); }
int32_t Thread::signal_clr(int32_t signals) { return osSignalClear(_tid, signals); }
/*---------------------------------------------------------------------------- Thread 1 'command': command processor *---------------------------------------------------------------------------*/ void command (void const *argument) { int i; printf (menu); /* display command menu */ while (1) { /* endless loop */ printf ("\r\nCommand: "); /* display prompt */ #ifdef __USE_FFLUSH fflush (stdout); #endif getline (cmdline, sizeof (cmdline)); /* get command line input */ for (i = 0; cmdline[i] != 0; i++) { /* convert to uppercase */ cmdline[i] = (char) toupper((int)cmdline[i]); } for (i = 0; cmdline[i] == ' '; i++); /* skip blanks */ switch (cmdline[i]) { /* proceed to command function */ case 'D': /* Display Time Command */ printf ("Start Time: %02d:%02d:%02d " "End Time: %02d:%02d:%02d\r\n", stime.hour, stime.min, stime.sec, etime.hour, etime.min, etime.sec); printf (" type ESC to abort\r"); #ifdef __USE_FFLUSH fflush (stdout); #endif tid_getesc = osThreadCreate(osThread(get_escape), NULL); /* ESC check in display loop */ escape = false; /* clear escape flag */ display_time = true; /* set display time flag */ osSignalClear(tid_command, 0x0003); /* clear pending signals */ while (!escape) { /* while no ESC enteRED */ printf ("Clock Time: %02d:%02d:%02d\r", /* display time */ ctime.hour, ctime.min, ctime.sec); #ifdef __USE_FFLUSH fflush (stdout); #endif osSignalWait(0, osWaitForever); /* wait for time change or ESC */ } osThreadTerminate(tid_getesc); /* terminate 'get_escape' */ display_time = false; /* clear display time flag */ printf ("\r\n\r\n"); break; case 'T': /* Set Time Command */ if (readtime (&cmdline[i+1])) { /* read time input and */ ctime.hour = rtime.hour; /* store in 'ctime' */ ctime.min = rtime.min; ctime.sec = rtime.sec; } break; case 'E': /* Set End Time Command */ if (readtime (&cmdline[i+1])) { /* read time input and */ etime.hour = rtime.hour; /* store in 'end' */ etime.min = rtime.min; etime.sec = rtime.sec; } break; case 'S': /* Set Start Time Command */ if (readtime (&cmdline[i+1])) { /* read time input and */ stime.hour = rtime.hour; /* store in 'start' */ stime.min = rtime.min; stime.sec = rtime.sec; } break; default: /* Error Handling */ printf (menu); /* display command menu */ break; } } }