From 30cfb87ec0d95e0cb8a671cf8f2327b4204927ed Mon Sep 17 00:00:00 2001 From: Justin Berger Date: Tue, 10 Apr 2018 23:44:36 -0600 Subject: Renamed api; fixed various warnings --- api_example.c | 25 +++++----- include/libsurvive/survive_api.h | 104 +++++++++++++++++++-------------------- src/survive_api.c | 98 +++++++++++++++++------------------- 3 files changed, 111 insertions(+), 116 deletions(-) diff --git a/api_example.c b/api_example.c index b0c9e4c..1f06740 100644 --- a/api_example.c +++ b/api_example.c @@ -4,31 +4,32 @@ #include int main(int argc, char **argv) { - SurviveAsyncContext *actx = survive_async_init(argc, argv); + SurviveSimpleContext *actx = survive_simple_init(argc, argv); if (actx == 0) // implies -help or similiar return 0; - survive_async_start_thread(actx); + survive_simple_start_thread(actx); - while (survive_async_is_running(actx)) { + while (survive_simple_is_running(actx)) { OGUSleep(30000); SurvivePose pose; - for (const SurviveAsyncObject *it = survive_async_get_first_object(actx); it != 0; - it = survive_async_get_next_object(actx, it)) { - uint32_t timecode = survive_async_object_get_latest_pose(it, &pose); - printf("%s (%u): %f %f %f %f %f %f %f\n", survive_async_object_name(it), timecode, - pose.Pos[0], pose.Pos[1], pose.Pos[2], pose.Rot[0], pose.Rot[1], pose.Rot[2], pose.Rot[3]); + for (const SurviveSimpleObject *it = survive_simple_get_first_object(actx); it != 0; + it = survive_simple_get_next_object(actx, it)) { + uint32_t timecode = survive_simple_object_get_latest_pose(it, &pose); + printf("%s (%u): %f %f %f %f %f %f %f\n", survive_simple_object_name(it), timecode, pose.Pos[0], + pose.Pos[1], pose.Pos[2], pose.Rot[0], pose.Rot[1], pose.Rot[2], pose.Rot[3]); } OGUSleep(30000); - for (const SurviveAsyncObject* it = survive_async_get_next_updated(actx); it != 0; it = survive_async_get_next_updated(actx)) { - printf("%s changed since last checked\n", survive_async_object_name(it)); + for (const SurviveSimpleObject *it = survive_simple_get_next_updated(actx); it != 0; + it = survive_simple_get_next_updated(actx)) { + printf("%s changed since last checked\n", survive_simple_object_name(it)); } } - - survive_async_close(actx); + + survive_simple_close(actx); return 0; } diff --git a/include/libsurvive/survive_api.h b/include/libsurvive/survive_api.h index fc4ce2c..64d1271 100644 --- a/include/libsurvive/survive_api.h +++ b/include/libsurvive/survive_api.h @@ -5,58 +5,58 @@ extern "C" { #endif - struct SurviveAsyncContext; - typedef struct SurviveAsyncContext SurviveAsyncContext; - - /*** - * Initialize a new instance of an async context -- mirrors survive_init - * @return Pointer to the async context - */ - SURVIVE_EXPORT SurviveAsyncContext *survive_async_init(int argc, char *const *argv); - - /** - * Close all devices and free all memory associated with the given context - */ - SURVIVE_EXPORT void survive_async_close(SurviveAsyncContext* actx); - - /** - * Start the background thread which processes various inputs and produces deliverable data like position. - */ - SURVIVE_EXPORT void survive_async_start_thread(SurviveAsyncContext* actx); - - /** - * @return true iff the background thread is still running - */ - SURVIVE_EXPORT bool survive_async_is_running(SurviveAsyncContext *actx); - - struct SurviveAsyncObject; - typedef struct SurviveAsyncObject SurviveAsyncObject; - - /** - * Get the first known object. Note that this also includes lighthouses - */ - SURVIVE_EXPORT const SurviveAsyncObject *survive_async_get_first_object(SurviveAsyncContext *actx); - /** - * Get the next known object from a current one. - */ - SURVIVE_EXPORT const SurviveAsyncObject *survive_async_get_next_object(SurviveAsyncContext *actx, - const SurviveAsyncObject *curr); - - /** - * Gets the next object which has been updated since we last looked at it with this function - */ - SURVIVE_EXPORT const SurviveAsyncObject *survive_async_get_next_updated(SurviveAsyncContext *actx); - - /** - * Gets the pose of a given object - */ - SURVIVE_EXPORT survive_timecode survive_async_object_get_latest_pose(const SurviveAsyncObject *sao, - SurvivePose *pose); - - /** - * Gets the null terminated name of the object. - */ - SURVIVE_EXPORT const char *survive_async_object_name(const SurviveAsyncObject *sao); +struct SurviveSimpleContext; +typedef struct SurviveSimpleContext SurviveSimpleContext; + +/*** + * Initialize a new instance of an simple context -- mirrors survive_init + * @return Pointer to the simple context + */ +SURVIVE_EXPORT SurviveSimpleContext *survive_simple_init(int argc, char *const *argv); + +/** + * Close all devices and free all memory associated with the given context + */ +SURVIVE_EXPORT void survive_simple_close(SurviveSimpleContext *actx); + +/** + * Start the background thread which processes various inputs and produces deliverable data like position. + */ +SURVIVE_EXPORT void survive_simple_start_thread(SurviveSimpleContext *actx); + +/** + * @return true iff the background thread is still running + */ +SURVIVE_EXPORT bool survive_simple_is_running(SurviveSimpleContext *actx); + +struct SurviveSimpleObject; +typedef struct SurviveSimpleObject SurviveSimpleObject; + +/** + * Get the first known object. Note that this also includes lighthouses + */ +SURVIVE_EXPORT const SurviveSimpleObject *survive_simple_get_first_object(SurviveSimpleContext *actx); +/** + * Get the next known object from a current one. + */ +SURVIVE_EXPORT const SurviveSimpleObject *survive_simple_get_next_object(SurviveSimpleContext *actx, + const SurviveSimpleObject *curr); + +/** + * Gets the next object which has been updated since we last looked at it with this function + */ +SURVIVE_EXPORT const SurviveSimpleObject *survive_simple_get_next_updated(SurviveSimpleContext *actx); + +/** + * Gets the pose of a given object + */ +SURVIVE_EXPORT survive_timecode survive_simple_object_get_latest_pose(const SurviveSimpleObject *sao, + SurvivePose *pose); + +/** + * Gets the null terminated name of the object. + */ +SURVIVE_EXPORT const char *survive_simple_object_name(const SurviveSimpleObject *sao); #ifdef __cplusplus } diff --git a/src/survive_api.c b/src/survive_api.c index e2c3d54..a39524b 100644 --- a/src/survive_api.c +++ b/src/survive_api.c @@ -3,13 +3,10 @@ #include "survive.h" #include "stdio.h" -struct SurviveAsyncObject { - struct SurviveAsyncContext* actx; - - enum SurviveAsyncObject_type { - SurviveAsyncObject_LIGHTHOUSE, - SurviveAsyncObject_OBJECT - } type; +struct SurviveSimpleObject { + struct SurviveSimpleContext *actx; + + enum SurviveSimpleObject_type { SurviveSimpleObject_LIGHTHOUSE, SurviveSimpleObject_OBJECT } type; union { int lighthouse; @@ -20,7 +17,7 @@ struct SurviveAsyncObject { bool has_update; }; -struct SurviveAsyncContext { +struct SurviveSimpleContext { SurviveContext* ctx; bool running; @@ -28,21 +25,21 @@ struct SurviveAsyncContext { og_mutex_t poll_mutex; size_t object_ct; - struct SurviveAsyncObject objects[]; + struct SurviveSimpleObject objects[]; }; static void pose_fn(SurviveObject *so, uint32_t timecode, SurvivePose *pose) { - struct SurviveAsyncContext *actx = so->ctx->user_ptr; + struct SurviveSimpleContext *actx = so->ctx->user_ptr; OGLockMutex(actx->poll_mutex); - survive_default_raw_pose_process(so, timecode, pose); + survive_default_raw_pose_process(so, timecode, pose); - int idx = (int)so->user_ptr; + intptr_t idx = (intptr_t)so->user_ptr; actx->objects[idx].has_update = true; OGUnlockMutex(actx->poll_mutex); } static void lh_fn(SurviveContext *ctx, uint8_t lighthouse, SurvivePose *lighthouse_pose, SurvivePose *object_pose) { - struct SurviveAsyncContext *actx = ctx->user_ptr; + struct SurviveSimpleContext *actx = ctx->user_ptr; OGLockMutex(actx->poll_mutex); survive_default_lighthouse_pose_process(ctx, lighthouse, lighthouse_pose, object_pose); @@ -51,7 +48,7 @@ static void lh_fn(SurviveContext *ctx, uint8_t lighthouse, SurvivePose *lighthou OGUnlockMutex(actx->poll_mutex); } -struct SurviveAsyncContext *survive_async_init(int argc, char *const *argv) { +struct SurviveSimpleContext *survive_simple_init(int argc, char *const *argv) { SurviveContext* ctx = survive_init(argc, argv); if (ctx == 0) return 0; @@ -59,25 +56,26 @@ struct SurviveAsyncContext *survive_async_init(int argc, char *const *argv) { survive_startup(ctx); int object_ct = ctx->activeLighthouses + ctx->objs_ct; - struct SurviveAsyncContext * actx = calloc(1, sizeof(struct SurviveAsyncContext) + sizeof(struct SurviveAsyncObject) * object_ct ); + struct SurviveSimpleContext *actx = + calloc(1, sizeof(struct SurviveSimpleContext) + sizeof(struct SurviveSimpleObject) * object_ct); actx->object_ct = object_ct; actx->ctx = ctx; actx->poll_mutex = OGCreateMutex(); ctx->user_ptr = actx; - int i = 0; + intptr_t i = 0; for (i = 0; i < ctx->activeLighthouses; i++) { - struct SurviveAsyncObject* obj = &actx->objects[i]; + struct SurviveSimpleObject *obj = &actx->objects[i]; obj->data.lighthouse = i; - obj->type = SurviveAsyncObject_LIGHTHOUSE; + obj->type = SurviveSimpleObject_LIGHTHOUSE; obj->actx = actx; obj->has_update = ctx->bsd[i].PositionSet; - snprintf(obj->name, 32, "LH%d", i); + snprintf(obj->name, 32, "LH%ld", i); } for (; i < object_ct; i++) { - struct SurviveAsyncObject* obj = &actx->objects[i]; + struct SurviveSimpleObject *obj = &actx->objects[i]; int so_idx = i - ctx->activeLighthouses; obj->data.so = ctx->objs[so_idx]; - obj->type = SurviveAsyncObject_OBJECT; + obj->type = SurviveSimpleObject_OBJECT; obj->actx = actx; obj->data.so->user_ptr = (void*)i; snprintf(obj->name, 32, "%s", obj->data.so->codename); @@ -88,53 +86,52 @@ struct SurviveAsyncContext *survive_async_init(int argc, char *const *argv) { return actx; } -void survive_async_close(struct SurviveAsyncContext* actx) { +int survive_simple_stop_thread(struct SurviveSimpleContext *actx) { + actx->running = false; + intptr_t error = (intptr_t)OGJoinThread(actx->thread); + if (error != 0) { + SurviveContext *ctx = actx->ctx; + SV_INFO("Warning: Loope exited with error %ld", error); + } + return error; +} + +void survive_simple_close(struct SurviveSimpleContext *actx) { if (actx->running) { - survive_async_stop_thread(actx); + survive_simple_stop_thread(actx); } survive_close(actx->ctx); } -static inline void* __async_thread(void* _actx) { - struct SurviveAsyncContext* actx = _actx; - int error = 0; +static inline void *__simple_thread(void *_actx) { + struct SurviveSimpleContext *actx = _actx; + intptr_t error = 0; while (actx->running && error == 0) { error = survive_poll(actx->ctx); } actx->running = false; return (void*)error; } -bool survive_async_is_running(struct SurviveAsyncContext* actx) { - return actx->running; -} -void survive_async_start_thread(struct SurviveAsyncContext* actx) { +bool survive_simple_is_running(struct SurviveSimpleContext *actx) { return actx->running; } +void survive_simple_start_thread(struct SurviveSimpleContext *actx) { actx->running = true; - actx->thread = OGCreateThread(__async_thread, actx); -} -int survive_async_stop_thread(struct SurviveAsyncContext* actx) { - actx->running = false; - int error = (int)OGJoinThread(actx->thread); - if (error != 0) { - SurviveContext* ctx = actx->ctx; - SV_INFO("Warning: Loope exited with error %d", error); - } - return error; + actx->thread = OGCreateThread(__simple_thread, actx); } -const struct SurviveAsyncObject *survive_async_get_next_object(struct SurviveAsyncContext *actx, - const struct SurviveAsyncObject *curr) { - const struct SurviveAsyncObject* next = curr + 1; +const struct SurviveSimpleObject *survive_simple_get_next_object(struct SurviveSimpleContext *actx, + const struct SurviveSimpleObject *curr) { + const struct SurviveSimpleObject *next = curr + 1; if (next >= actx->objects + actx->object_ct) return 0; return next; } -const struct SurviveAsyncObject *survive_async_get_first_object(struct SurviveAsyncContext *actx) { +const struct SurviveSimpleObject *survive_simple_get_first_object(struct SurviveSimpleContext *actx) { return actx->objects; } -const struct SurviveAsyncObject* survive_async_get_next_updated(struct SurviveAsyncContext* actx) { +const struct SurviveSimpleObject *survive_simple_get_next_updated(struct SurviveSimpleContext *actx) { for (int i = 0; i < actx->object_ct; i++) { if (actx->objects[i].has_update) { actx->objects[i].has_update = false; @@ -144,17 +141,17 @@ const struct SurviveAsyncObject* survive_async_get_next_updated(struct SurviveAs return 0; } -uint32_t survive_async_object_get_latest_pose(const struct SurviveAsyncObject* sao, SurvivePose* pose) { +uint32_t survive_simple_object_get_latest_pose(const struct SurviveSimpleObject *sao, SurvivePose *pose) { uint32_t timecode = 0; OGLockMutex(sao->actx->poll_mutex); switch (sao->type) { - case SurviveAsyncObject_LIGHTHOUSE: { + case SurviveSimpleObject_LIGHTHOUSE: { if(pose) *pose = sao->actx->ctx->bsd[sao->data.lighthouse].Pose; break; } - case SurviveAsyncObject_OBJECT: + case SurviveSimpleObject_OBJECT: if(pose) *pose = sao->data.so->OutPose; timecode = sao->data.so->OutPose_timecode; @@ -165,7 +162,4 @@ uint32_t survive_async_object_get_latest_pose(const struct SurviveAsyncObject* s return timecode; } -const char * survive_async_object_name(const SurviveAsyncObject * sao) -{ - return sao->name; -} +const char *survive_simple_object_name(const SurviveSimpleObject *sao) { return sao->name; } -- cgit v1.2.3