@ -19,19 +19,19 @@
/* dynamic dictionary for the letters of the dictionary */
/* dynamic dictionary for the letters of the dictionary */
static dict_t * * splitFlapDict ;
static dict_t * * splitFlapDict ;
/* all letters of the splitflap in the correct order */
/* all letters of the splitflap in the correct order */
static char * SF_Letters [ ] = { " " , " A " , " B " , " C " , " D " , " E " , " F " , " G " , " H " , " I " , " J " , " K " ,
static Flap_t SF_Letters [ ] = { " " , " A " , " B " , " C " , " D " , " E " , " F " , " G " , " H " , " I " , " J " , " K " ,
" L " , " M " , " N " , " O " , " P " , " Q " , " R " , " S " , " T " , " U " , " V " , " W " , " X " , " Y " , " Z " ,
" L " , " M " , " N " , " O " , " P " , " Q " , " R " , " S " , " T " , " U " , " V " , " W " , " X " , " Y " , " Z " ,
" 0 " , " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " , " 7 " , " 8 " , " 9 " ,
" 0 " , " 1 " , " 2 " , " 3 " , " 4 " , " 5 " , " 6 " , " 7 " , " 8 " , " 9 " ,
" ! " , " ? " , " : " } ;
" ! " , " ? " , " : " } ;
/* flag if this module is initialized (physically) */
static bool initialized = false ;
/* mutex for if the module is currently being moved */
static SemaphoreHandle_t ongoingMoveMutex = NULL ;
/* function declarations */
/* function declarations */
static bool OngoingMoveMutex_Lock ( void ) ;
static bool OngoingMoveMutex_Lock ( SF_Handle_t instance ) ;
static void OngoingMoveMutex_Unlock ( void ) ;
static void OngoingMoveMutex_Unlock ( SF_Handle_t instance ) ;
static void SF_MoveToNextFlap ( void * pv ) ;
/**********************/
/* INIT / DEINIT */
/**********************/
void SF_InitConfig ( void ) {
void SF_InitConfig ( void ) {
splitFlapDict = dictAlloc ( ) ;
splitFlapDict = dictAlloc ( ) ;
( ( dict_t * ) splitFlapDict ) - > key = NULL ;
( ( dict_t * ) splitFlapDict ) - > key = NULL ;
@ -66,20 +66,33 @@ SF_Handle_t SF_Init(SF_Config_t* instance, int id){
splitflap - > magSensor = McuGPIO_InitGPIO ( & instance - > magSensorConfig ) ;
splitflap - > magSensor = McuGPIO_InitGPIO ( & instance - > magSensorConfig ) ;
splitflap - > motor = McuULN2003_InitMotor ( & instance - > motorConfig ) ;
splitflap - > motor = McuULN2003_InitMotor ( & instance - > motorConfig ) ;
splitflap - > id = id ;
splitflap - > id = id ;
splitflap - > ongoingMoveMutex = xSemaphoreCreateRecursiveMutex ( ) ; // create mutex for ongoing move
splitflap - > initialized = false ;
splitflap - > nextFlap = SF_Letters [ 0 ] ;
// create mutex for ongoing move
// add mutex to registry
ongoingMoveMutex = xSemaphoreCreateRecursiveMutex ( ) ;
char text [ 50 ] = " Ongoing move SplitFlap " ;
char text [ 50 ] = " Ongoing move SplitFlap " ;
McuUtility_strcatNum16s ( ( uint8_t * ) text , sizeof ( text ) + 20 , id ) ;
McuUtility_strcatNum16s ( ( uint8_t * ) text , sizeof ( text ) + 20 , id ) ;
vQueueAddToRegistry ( ongoingMoveMutex , text ) ;
vQueueAddToRegistry ( splitflap - > ongoingMoveMutex , text ) ;
return splitflap ;
return splitflap ;
}
}
void SF_Deinit ( SF_Handle_t instance ) {
vSemaphoreDelete ( ( ( SF_t * ) instance ) - > ongoingMoveMutex ) ;
( ( SF_t * ) instance ) - > initialized = false ;
McuULN2003_DeinitMotor ( ( ( SF_t * ) instance ) - > motor ) ;
McuGPIO_DeinitGPIO ( ( ( SF_t * ) instance ) - > magSensor ) ;
}
/**********************/
/* PUBLIC FUNCTIONS */
/**********************/
bool SF_MoveMotorToZeroPosition ( SF_Handle_t instance , uint16_t offsetSteps ) {
bool SF_MoveMotorToZeroPosition ( SF_Handle_t instance , uint16_t offsetSteps ) {
int numStepsMoved = 0 ;
int numStepsMoved = 0 ;
if ( OngoingMoveMutex_Lock ( ) ) {
if ( OngoingMoveMutex_Lock ( instance ) ) {
// move out of sensor
// move out of sensor
while ( SF_GetMagSensorAtZeroPosition ( ( SF_t * ) instance ) = = true ) {
while ( SF_GetMagSensorAtZeroPosition ( ( SF_t * ) instance ) = = true ) {
McuULN2003_IncStep ( ( ( SF_t * ) instance ) - > motor ) ;
McuULN2003_IncStep ( ( ( SF_t * ) instance ) - > motor ) ;
@ -103,19 +116,18 @@ bool SF_MoveMotorToZeroPosition(SF_Handle_t instance, uint16_t offsetSteps){
McuULN2003_SetPos ( ( ( SF_t * ) instance ) - > motor , 0 ) ;
McuULN2003_SetPos ( ( ( SF_t * ) instance ) - > motor , 0 ) ;
McuULN2003_PowerOff ( ( ( SF_t * ) instance ) - > motor ) ;
McuULN2003_PowerOff ( ( ( SF_t * ) instance ) - > motor ) ;
OngoingMoveMutex_Unlock ( ) ;
OngoingMoveMutex_Unlock ( instance ) ;
}
}
// success if less than one rotation
// success if less than one rotation
initialized = ( numStepsMoved < SPLITFLAP_STEPS_ONE_ROUND ) ;
( ( SF_t * ) instance ) - > initialized = ( numStepsMoved < SPLITFLAP_STEPS_ONE_ROUND ) ;
return initialized ;
return ( ( SF_t * ) instance ) - > initialized ;
}
}
void SF_MoveSteps ( SF_Handle_t instance , uint32_t steps ) {
void SF_MoveSteps ( SF_Handle_t instance , uint32_t steps ) {
if ( initialized ) {
if ( ( ( SF_t * ) instance ) - > initialized ) {
if ( OngoingMoveMutex_Lock ( instance ) ) {
if ( OngoingMoveMutex_Lock ( ) ) {
// run move with acceleration & deceleration
// run move with acceleration & deceleration
McuULN2003_AccelerationStart ( ( ( SF_t * ) instance ) - > motor ) ;
McuULN2003_AccelerationStart ( ( ( SF_t * ) instance ) - > motor ) ;
while ( steps > 0 ) {
while ( steps > 0 ) {
@ -131,7 +143,7 @@ void SF_MoveSteps(SF_Handle_t instance, uint32_t steps){
// no re-init is required
// no re-init is required
McuULN2003_PowerOff ( ( ( SF_t * ) instance ) - > motor ) ;
McuULN2003_PowerOff ( ( ( SF_t * ) instance ) - > motor ) ;
OngoingMoveMutex_Unlock ( ) ;
OngoingMoveMutex_Unlock ( instance ) ;
}
}
}
}
}
}
@ -140,8 +152,8 @@ bool SF_GetMagSensorAtZeroPosition(SF_Handle_t instance){
return McuGPIO_IsLow ( ( ( SF_t * ) instance ) - > magSensor ) ;
return McuGPIO_IsLow ( ( ( SF_t * ) instance ) - > magSensor ) ;
}
}
void SF_MoveToFlap ( SF_Handle_t instance , char * flap ) {
void SF_MoveToFlap ( SF_Handle_t instance , Flap_t flap ) {
if ( OngoingMoveMutex_Lock ( ) ) {
if ( OngoingMoveMutex_Lock ( instance ) ) {
// get flap pos from dictonary
// get flap pos from dictonary
int32_t flapPos = ( int32_t ) getItem ( * splitFlapDict , flap ) ;
int32_t flapPos = ( int32_t ) getItem ( * splitFlapDict , flap ) ;
@ -159,38 +171,23 @@ void SF_MoveToFlap(SF_Handle_t instance, char* flap){
SF_MoveSteps ( instance , stepsToReachFlap ) ;
SF_MoveSteps ( instance , stepsToReachFlap ) ;
}
}
OngoingMoveMutex_Unlock ( ) ;
OngoingMoveMutex_Unlock ( instance ) ;
}
}
}
}
typedef struct {
void SF_MoveToFlapAsync ( SF_Handle_t instance , Flap_t flap ) {
SF_Handle_t instance ;
if ( OngoingMoveMutex_Lock ( instance ) ) {
char * flap ;
} MoveToFlap_Param_t ;
/* pointer to MoveToFlap_Param_t */
static void SF_MoveToFlapWithParameter ( void * pv ) {
MoveToFlap_Param_t param = * ( MoveToFlap_Param_t * ) pv ;
SF_MoveToFlap ( param . instance , param . flap ) ;
}
MoveToFlap_Param_t taskParameters ;
void SF_MoveToFlapAsync ( SF_Handle_t instance , char * flap ) {
if ( OngoingMoveMutex_Lock ( ) ) {
BaseType_t res ;
BaseType_t res ;
char taskName [ 5 0] = " SF Mv " ;
char taskName [ 10 ] = " SF Mv " ;
McuUtility_strcatNum16s ( ( uint8_t * ) taskName , sizeof ( taskName ) + 20 , ( ( SF_t * ) instance ) - > id ) ;
McuUtility_strcatNum16s ( ( uint8_t * ) taskName , sizeof ( taskName ) , ( ( SF_t * ) instance ) - > id ) ;
taskParameters . instance = instance ;
// set next flap
taskParameters . f lap = flap ;
( ( SF_t * ) instance ) - > nextFlap = flap ;
res = xTaskCreate ( SF_MoveToFlapWithParameter ,
res = xTaskCreate ( SF_MoveToNextFlap ,
taskName ,
taskName ,
500 / sizeof ( StackType_t ) ,
500 / sizeof ( StackType_t ) ,
& taskParameters ,
instance , // no &, since otherwise pointing on parameter address! we want handle address!
tskIDLE_PRIORITY ,
tskIDLE_PRIORITY ,
NULL ) ;
NULL ) ;
@ -200,7 +197,7 @@ void SF_MoveToFlapAsync(SF_Handle_t instance, char* flap){
for ( ; ; ) { } // Endless loop
for ( ; ; ) { } // Endless loop
}
}
OngoingMoveMutex_Unlock ( ) ;
OngoingMoveMutex_Unlock ( instance ) ;
}
}
}
}
@ -208,28 +205,37 @@ int32_t SF_GetMotorPosition(SF_Handle_t instance){
return McuULN2003_GetPos ( ( ( SF_t * ) instance ) - > motor ) ;
return McuULN2003_GetPos ( ( ( SF_t * ) instance ) - > motor ) ;
}
}
void SF_Deinit ( SF_Handle_t instance ) {
/**********************/
vSemaphoreDelete ( ongoingMoveMutex ) ;
/* INTERNAL FUNCTIONS */
McuULN2003_DeinitMotor ( ( ( SF_t * ) instance ) - > motor ) ;
/**********************/
McuGPIO_DeinitGPIO ( ( ( SF_t * ) instance ) - > magSensor ) ;
/* voidpointer to instance of splitflap (SF_Handle_t) */
initialized = false ;
static void SF_MoveToNextFlap ( void * pv ) {
// parse parameter
SF_Handle_t instance = ( SF_Handle_t ) pv ;
PRINTF ( " Splitflap: Moving Flap nr. %i to letter '%c'. \n " , ( ( SF_t * ) instance ) - > id , ( ( ( SF_t * ) instance ) - > nextFlap ) [ 0 ] ) ;
// move to next flap
SF_MoveToFlap ( instance , ( ( SF_t * ) instance ) - > nextFlap ) ;
// reset next flap
( ( SF_t * ) instance ) - > nextFlap = SF_Letters [ 0 ] ;
}
}
/* HELPERS */
/**********************/
static bool OngoingMoveMutex_Lock ( void ) {
/* HELPERS */
/**********************/
static bool OngoingMoveMutex_Lock ( SF_Handle_t instance ) {
/* aquire mutex */
/* aquire mutex */
if ( xSemaphoreTakeRecursive ( ongoingMoveMutex , pdMS_TO_TICKS ( 20 ) ) ! = pdTRUE ) {
if ( xSemaphoreTakeRecursive ( ( ( SF_t * ) instance ) - > ongoingMoveMutex , pdMS_TO_TICKS ( 20 ) ) ! = pdTRUE ) {
return false ; /* timeout? */
return false ; /* timeout? */
}
}
return true ;
return true ;
}
}
static void OngoingMoveMutex_Unlock ( void ) {
static void OngoingMoveMutex_Unlock ( SF_Handle_t instance ) {
/* give back mutex */
/* give back mutex */
if ( xSemaphoreGiveRecursive ( ongoingMoveMutex ) ! = pdTRUE ) {
if ( xSemaphoreGiveRecursive ( ( ( SF_t * ) instance ) - > ongoingMoveMutex ) ! = pdTRUE ) {
/* issue */
/* issue */
PRINTF ( " Could not give back ongoing move mutex for splitflap " ) ;
PRINTF ( " Could not give back ongoing move mutex for splitflap %i " , ( ( SF_t * ) instance ) - > id ) ;
for ( ; ; ) ;
for ( ; ; ) ;
}
}
}
}