Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
f7ae632
Updated the phony target in the makefile.
shalecraig Mar 23, 2014
c9bc036
There is no file that has MIN and MAX, removed TODO
shalecraig Mar 23, 2014
47579b3
Renamed method to be internal, moved TODO around
shalecraig Mar 23, 2014
b4555c0
Removed build files
shalecraig Mar 23, 2014
f9374f4
Added arg to make the simple-acc.. project build.
shalecraig Mar 23, 2014
549943a
Updated arguments to be more strict
shalecraig Mar 23, 2014
c58f587
Merge pull request #50 from shalecraig/shale/1.1.0/make-simple-accele…
shalecraig Mar 23, 2014
2f3fb22
Merge pull request #49 from shalecraig/shale/1.1.0/TODO-cleanups
shalecraig Mar 23, 2014
9ec21d3
Started initial work on the moving_avg_ticker
shalecraig Mar 23, 2014
c83f5d5
Moved macro definition around
shalecraig Mar 23, 2014
b739935
Change the type of wbuf to uint32_t
shalecraig Mar 23, 2014
2340262
Change 2 params:allocate_moving_avg int->uint32_t
shalecraig Mar 23, 2014
92c069f
Moving average ticker now has more explicit typing
shalecraig Mar 24, 2014
ea24116
Made get_latest_frame faster, fewer ops in loop
shalecraig Mar 24, 2014
16f7bcf
Dimensions is now a unsigned integer
shalecraig Mar 24, 2014
c2dfd7b
Moved types to explicit sizes
shalecraig Mar 24, 2014
4a25ca1
Added documentation note to pebble_makeup header.
shalecraig Mar 24, 2014
a86a99a
Reordered imports for moving_avg_ticker
shalecraig Mar 24, 2014
f0032d6
Cleaned up some types in accel.c
shalecraig Mar 24, 2014
c245614
Added some makefile args.
shalecraig Mar 24, 2014
5798df5
More arguments that will hopefully break the build
shalecraig Mar 24, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# TODO: update this.
PHONY = default echo/objects echo/sources list tests run bin clean
PHONY = default echo/objects echo/sources list tests bin/tests run bin clean

SOURCES_C = $(wildcard src/*.c)
TEST_CC = $(wildcard test/*.cc)
Expand All @@ -11,8 +10,9 @@ TEST_OBJECTS = $(subst test,bin,$(_TEST_OBJECTS_TMP))

EXEC = tests

# Pebble args: -std=c99 -fdata-sections -g -Wall -Wextra -Werror -Wno-unused-parameter -Wno-error=unused-function -Wno-error=unused-variable -fPIE -DRELEASE -Os
# removed: -Wl,-z,relro -Wl,-z,now
C_ARGS = -DIS_NOT_PEBBLE -pipe -m64 -ansi -fPIC -g -O3 -fno-exceptions -fstack-protector -fvisibility=hidden -W -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wformat -Wreturn-type -Wsign-compare -Wmultichar -Wformat-nonliteral -Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -Werror -Wcomment -Wtrigraphs -Wundef -Wunused-macros -pedantic-errors -std=c99
C_ARGS = -DIS_NOT_PEBBLE -DRELEASE -pipe -m64 -ansi -fPIC -fPIE -g -Os -ffunction-sections -fno-exceptions -fstack-protector-all -fvisibility=hidden -W -Wall -Wextra -Wunused-parameter -Wunused-function -Wunused-label -Wpointer-arith -Wformat -Wreturn-type -Wsign-compare -Wmultichar -Wformat-nonliteral -Winit-self -Wuninitialized -Wdeprecated -Wformat-security -Werror -Wcomment -Wtrigraphs -Wundef -Wunused-macros -pedantic-errors -std=c99 -fsanitize-undefined-trap-on-error -fsanitize=alignment,bool,bounds,enum,null,return,shift,undefined-trap,unreachable
# removed: -lpthread, -Wl,-z,relro -Wl,-z,now
CXX_ARGS = -DIS_NOT_PEBBLE -lpthread -pipe -m64 -ansi -fPIC -g -O3 -fno-exceptions -fstack-protector -fvisibility=hidden -W -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wformat -Wreturn-type -Wsign-compare -Wmultichar -Wformat-nonliteral -Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -Wall

Expand Down
2 changes: 2 additions & 0 deletions sample/simple-accelerometer/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@

# Ignore build generated files
build

.lock-waf_linux2_build
8 changes: 0 additions & 8 deletions sample/simple-accelerometer/.lock-waf_linux2_build

This file was deleted.

65 changes: 30 additions & 35 deletions sample/simple-accelerometer/src/accel.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
#ifndef IS_NOT_PEBBLE
#ifndef PEBBLE
#define PEBBLE
#endif
#endif

#ifdef PEBBLE
#include <pebble.h>
#include <pebble_makeup.h>
Expand All @@ -30,7 +24,7 @@ typedef struct {
bool is_recorded;

int recording_size;
int **normalized_recording;
int32_t **normalized_recording;

moving_avg_values **moving_avg_values;
int *offsets;
Expand Down Expand Up @@ -61,7 +55,7 @@ typedef struct internalAccelState {
if (INPUT_STATE->state == NULL) { \
return ACCEL_INTERNAL_ERROR; \
} \
if (INPUT_STATE->dimensions <= 0) { \
if (INPUT_STATE->dimensions == 0) { \
return ACCEL_INTERNAL_ERROR; \
} \
if (INPUT_STATE->state->window_size <= 0) { \
Expand All @@ -88,19 +82,20 @@ typedef struct internalAccelState {
// TODO: should we store the offsets as floats instead?
#define ALPHA 1.0

// TODO: include these from a header file?
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
// #define MAX(a,b) (((a)>(b))?(a):(b))

void accel_destroy_gesture(accel_gesture **gesture, int dimensions) {
// TODO: write tests for "internal" functions.

void accel_destroy_gesture(accel_gesture **gesture, uint32_t dimensions) {
if (gesture == NULL || *gesture == NULL) {
return;
}

accel_gesture *gest = *gesture;

if (gest->moving_avg_values != NULL) {
for (int i = 0; i < dimensions; ++i) {
for (uint32_t i = 0; i < dimensions; ++i) {
free_moving_avg(&(gest->moving_avg_values[i]));
}
}
Expand All @@ -123,11 +118,10 @@ void accel_destroy_gesture(accel_gesture **gesture, int dimensions) {
*gesture = NULL;
}

int accel_generate_gesture(accel_state *state, accel_gesture **gesture) {
int internal_accel_generate_gesture(accel_state *state, accel_gesture **gesture) {
PRECONDITION_VALID_STATE(state);
PRECONDITION_NOT_NULL(gesture);

// TODO: write a test for this value.
PRECONDITION_NULL((*gesture));

size_t gesture_size = sizeof(accel_gesture);
Expand All @@ -146,13 +140,13 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) {
*gesture = NULL;
return ACCEL_MALLOC_ERROR;
}
for (int i = 0; i < state->dimensions; ++i) {
for (uint32_t i = 0; i < state->dimensions; ++i) {
// TODO: these two shouldn't both be the same....
int result = allocate_moving_avg(state->state->window_size, state->state->window_size,
&((*gesture)->moving_avg_values[i]));

if (result != ACCEL_SUCCESS) {
for (int j = 0; j < i; ++j) {
for (uint32_t j = 0; j < i; ++j) {
free_moving_avg(&((*gesture)->moving_avg_values[i]));
}
accel_destroy_gesture(gesture, state->dimensions);
Expand All @@ -162,14 +156,14 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) {
return ACCEL_SUCCESS;
}

int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback,
int accel_generate_state(accel_state **state, uint32_t dimensions, int window_size, accel_callback callback,
const int threshold) {
PRECONDITION_NOT_NULL(state);

// TODO: write a test for this value.
PRECONDITION_NULL(*state);

if (dimensions <= 0) {
if (dimensions == 0) {
return ACCEL_PARAM_ERROR;
}
if (window_size <= 0) {
Expand Down Expand Up @@ -208,6 +202,7 @@ int accel_generate_state(accel_state **state, int dimensions, int window_size, a
(*state)->dimensions = dimensions;
(*state)->state->window_size = window_size > 0 ? window_size : 2;
(*state)->state->threshold = threshold;
// TODO: verify that the state we have created is valid.
return ACCEL_SUCCESS;
}

Expand All @@ -216,7 +211,7 @@ int accel_destroy_state(accel_state **state) {
PRECONDITION_NOT_NULL(state);
PRECONDITION_NOT_NULL(*state);

int dimensions = (*state)->dimensions;
uint32_t dimensions = (*state)->dimensions;
if ((*state)->state != NULL) {
internal_accel_state *istate = (*state)->state;
if (istate->gestures != NULL) {
Expand Down Expand Up @@ -260,7 +255,7 @@ int accel_start_record_gesture(accel_state *state, int *gesture) {

state->state->gestures[*gesture] = NULL;

int result = accel_generate_gesture(state, &(state->state->gestures[*gesture]));
int result = internal_accel_generate_gesture(state, &(state->state->gestures[*gesture]));
if (result != ACCEL_SUCCESS) {
*gesture = -1;
if (state->state->num_gestures_saved == 1) {
Expand All @@ -284,14 +279,14 @@ int accel_start_record_gesture(accel_state *state, int *gesture) {
// The uWave paper suggests a mapping from [-20, 20]->[-15, 15], but cube root
// should to work better for variable ranges.
// TODO: revisit this decision.
int normalize(int sum) { return (int)cbrt(sum); }
int normalize(int32_t sum) { return (int32_t)cbrt(sum); }

int reset_gesture(accel_gesture *gest, const int dimensions) {
int reset_gesture(accel_gesture *gest, const uint32_t dimensions) {
PRECONDITION_NOT_NULL(gest);
for (int i = 0; i < gest->recording_size; ++i) {
gest->offsets[i] = INT16_MAX;
}
for (int d = 0; d < dimensions; ++d) {
for (uint32_t d = 0; d < dimensions; ++d) {
reset_moving_avg(gest->moving_avg_values[d]);
}
return ACCEL_SUCCESS;
Expand Down Expand Up @@ -340,7 +335,7 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) {
for (int i = 0; i < gesture->recording_size; ++i) {
gesture->offsets[i] = INT16_MAX;
}
for (int d = 0; d < state->dimensions; ++d) {
for (uint32_t d = 0; d < state->dimensions; ++d) {
reset_moving_avg(gesture->moving_avg_values[d]);
}
return ACCEL_SUCCESS;
Expand All @@ -349,23 +344,23 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) {
// TODO: gracefully handle malloc failure in this function.
// TODO: this should return error types instead of being void.
// Follow-up: find usages of this method.
void handle_recording_tick(accel_gesture *gesture, int dimensions) {
void handle_recording_tick(accel_gesture *gesture, uint32_t dimensions) {
if (gesture == NULL) {
return;
}
// TODO: grow exponentially, not linearly. Linear growth allocates too frequently.
if (gesture->recording_size != 0) {
gesture->normalized_recording =
(int **)my_realloc(gesture->normalized_recording, (gesture->recording_size + 1) * sizeof(int *),
gesture->recording_size * sizeof(int *));
(int32_t **)my_realloc(gesture->normalized_recording, (gesture->recording_size + 1) * sizeof(int32_t *),
gesture->recording_size * sizeof(int32_t *));
if (gesture->normalized_recording == NULL) {
return;
}
} else {
gesture->normalized_recording = (int **)malloc(sizeof(int *));
gesture->normalized_recording = (int32_t **)malloc(sizeof(int32_t *));
}
gesture->normalized_recording[gesture->recording_size] = (int *)malloc(sizeof(int) * dimensions);
for (int i = 0; i < dimensions; ++i) {
gesture->normalized_recording[gesture->recording_size] = (int32_t *)malloc(sizeof(int32_t) * dimensions);
for (uint32_t i = 0; i < dimensions; ++i) {
// TODO: fix this int/float business.
// TODO: complain about invalid return values.
get_latest_frame_moving_avg(gesture->moving_avg_values[i],
Expand All @@ -379,7 +374,7 @@ void handle_recording_tick(accel_gesture *gesture, int dimensions) {
int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gesture_id) {
// TODO: load the input at the beginning instead of gesture->recording_size times.
PRECONDITION_NOT_NULL(gesture);
int dimensions = state->dimensions;
uint32_t dimensions = state->dimensions;

if (gesture->moving_avg_values == NULL || gesture->offsets == NULL) {
return ACCEL_INTERNAL_ERROR;
Expand All @@ -390,9 +385,9 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu
--i;

int cost = 0;
for (int d = 0; d < dimensions; ++d) {
for (uint32_t d = 0; d < dimensions; ++d) {
int recording_i_d = gesture->normalized_recording[i][d];
int input_i_d = 0;
int32_t input_i_d = 0;
// TODO: complain about invalid return values.
get_latest_frame_moving_avg(gesture->moving_avg_values[d], &input_i_d);
input_i_d = normalize(input_i_d);
Expand All @@ -411,9 +406,9 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu
}
for (i = 1; i < gesture->recording_size; ++i) {
int cost = 0;
for (int d = 0; d < dimensions; ++d) {
for (uint32_t d = 0; d < dimensions; ++d) {
int recording_i_d = gesture->normalized_recording[i][d];
int input_i_d = 0;
int32_t input_i_d = 0;
// TODO: complain about invalid return values.
get_latest_frame_moving_avg(gesture->moving_avg_values[d], &input_i_d);
if (recording_i_d > input_i_d) {
Expand Down Expand Up @@ -464,7 +459,7 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) {
// If the moving average is at a final line.
bool avg_line = false;
int returned = ACCEL_SUCCESS;
for (int d = 0; d < state->dimensions && returned == 0; ++d) {
for (uint32_t d = 0; d < state->dimensions && returned == 0; ++d) {
returned = append_to_moving_avg(gesture->moving_avg_values[d], accel_data[d], &avg_line);
}
if (returned != ACCEL_SUCCESS) {
Expand Down
11 changes: 9 additions & 2 deletions sample/simple-accelerometer/src/accel.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#ifndef ACCEL_H
#define ACCEL_H

#ifndef IS_NOT_PEBBLE
#ifndef PEBBLE
#define PEBBLE
#endif
#endif

#ifdef __cplusplus
extern "C" {
#endif

#include <stdbool.h>
#include <stdint.h>

#define ACCEL_SUCCESS 0
#define ACCEL_PARAM_ERROR -1
Expand Down Expand Up @@ -61,7 +68,7 @@ struct accelState;
typedef int (*accel_callback)(struct accelState *state, int gesture_id, int offset_found, bool *reset_gesture);

typedef struct accelState {
int dimensions;
uint32_t dimensions;

accel_callback callback;
struct internalAccelState *state;
Expand All @@ -84,7 +91,7 @@ typedef struct accelState {
* gestures must be before the callback is called.
* @return ACCEL_SUCCESS if successful, an error code otherwise.
*/
int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback,
int accel_generate_state(accel_state **state, uint32_t dimensions, int window_size, accel_callback callback,
const int threshold);

/**
Expand Down
Loading