Skip to content

Commit 82bf21b

Browse files
committed
Separate switches for motion detection and accelerometer-based sleep.
1 parent cd7740c commit 82bf21b

File tree

5 files changed

+36
-17
lines changed

5 files changed

+36
-17
lines changed

firmware/nRF51/tag-proximity/inc/acc.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,11 @@
7070
#define ACC_REG_FIFO_SRC_REG 0x2F
7171
#define ACC_REG_INT1_CFG 0x30
7272

73-
#if CONFIG_ACCEL_SLEEP
73+
#if CONFIG_ACCEL_MOTION
7474
extern uint8_t moving;
75+
#endif
76+
77+
#if CONFIG_ACCEL_SLEEP
7578
extern uint8_t sleep;
7679
#endif
7780

firmware/nRF51/tag-proximity/inc/config.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
/* accelerometer-based sleep */
3232
#define CONFIG_ACCEL_SLEEP 0
3333

34+
/* accelerometer-based motion detection */
35+
#define CONFIG_ACCEL_MOTION 1
36+
#if CONFIG_ACCEL_SLEEP
37+
#define CONFIG_ACCEL_MOTION 1
38+
#endif
39+
3440
/* blink on receiving a proximity packet */
3541
#define CONFIG_PROXIMITY_BLINK 0
3642

firmware/nRF51/tag-proximity/inc/main.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ extern uint16_t status_flags;
3131
extern uint8_t boot_count;
3232
extern uint32_t reset_reason;
3333

34-
#if CONFIG_ACCEL_SLEEP
35-
extern uint8_t sleep;
36-
#endif
37-
3834
extern void blink(uint8_t times);
3935
extern void blink_fast(uint8_t times);
4036
extern uint16_t blink_wait_release(void);

firmware/nRF51/tag-proximity/src/acc.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,29 @@ static const uint8_t g_acc_init[][2] = {
4242
/* most recent acceleration measurement */
4343
static int16_t acc[3];
4444

45-
#if CONFIG_ACCEL_SLEEP
45+
46+
#if CONFIG_ACCEL_MOTION
4647
#define ACC_BUFFER_LEN 10
4748
#define ACC_DELTA_THRES 100000L
48-
#define ACC_SLEEP_THRES 300
4949

5050
static int16_t acc_buffer[ACC_BUFFER_LEN][3];
5151
static int32_t acc_avg[3];
5252
static uint8_t acc_buffer_index;
5353
static uint8_t acc_buffer_full;
54-
static uint16_t sleep_counter;
5554

5655
uint8_t moving = 1;
56+
#endif /* CONFIG_ACCEL_MOTION */
57+
58+
59+
#if CONFIG_ACCEL_SLEEP
60+
#define ACC_SLEEP_THRES 300
61+
5762
uint8_t sleep = 0;
63+
static uint16_t sleep_counter;
5864
#endif /* CONFIG_ACCEL_SLEEP */
5965

6066

67+
6168
void acc_write(uint8_t cmd, uint8_t data)
6269
{
6370
nrf_gpio_pin_clear(CONFIG_ACC_nCS);
@@ -105,7 +112,7 @@ void acc_read(uint8_t cmd, uint8_t len, uint8_t *data)
105112
}
106113

107114

108-
#if CONFIG_ACCEL_SLEEP
115+
#if CONFIG_ACCEL_MOTION
109116
static void acc_process_sample(void)
110117
{
111118
int16_t acc_delta;
@@ -130,16 +137,20 @@ static void acc_process_sample(void)
130137
if (acc_delta_norm < ACC_DELTA_THRES)
131138
{
132139
moving = 0;
140+
#if CONFIG_ACCEL_SLEEP
133141
if (!sleep && ++sleep_counter > ACC_SLEEP_THRES)
134142
sleep = 1;
143+
#endif /* CONFIG_ACCEL_SLEEP */
135144
} else {
145+
moving = 1;
146+
acc_buffer_index = 0;
147+
acc_buffer_full = 0;
148+
#if CONFIG_ACCEL_SLEEP
136149
if (sleep)
137150
status_flags |= FLAG_WOKEUP;
138-
moving = 1;
139151
sleep = 0;
140152
sleep_counter = 0;
141-
acc_buffer_index = 0;
142-
acc_buffer_full = 0;
153+
#endif /* CONFIG_ACCEL_SLEEP */
143154
}
144155
}
145156

@@ -153,7 +164,7 @@ static void acc_process_sample(void)
153164
if (!acc_buffer_full && !acc_buffer_index)
154165
acc_buffer_full = 1;
155166
}
156-
#endif /* CONFIG_ACCEL_SLEEP */
167+
#endif /* CONFIG_ACCEL_MOTION */
157168

158169

159170
void acc_sample(void)
@@ -164,7 +175,7 @@ void acc_sample(void)
164175
acc_read(ACC_REG_OUT_X, sizeof(acc), (uint8_t*)&acc);
165176
acc_write(ACC_REG_CTRL_REG1, 0x00);
166177

167-
#if CONFIG_ACCEL_SLEEP
178+
#if CONFIG_ACCEL_MOTION
168179
acc_process_sample();
169180
#endif
170181
}
@@ -219,10 +230,13 @@ uint8_t acc_init(void)
219230
for(i=0; i<ACC_INIT_COUNT; i++)
220231
acc_write(g_acc_init[i][0], g_acc_init[i][1]);
221232

222-
#if CONFIG_ACCEL_SLEEP
233+
#if CONFIG_ACCEL_MOTION
223234
acc_buffer_index = 0;
224235
acc_buffer_full = 0;
225236
moving = 1;
237+
#endif /* CONFIG_ACCEL_MOTION */
238+
239+
#if CONFIG_ACCEL_SLEEP
226240
sleep = 0;
227241
sleep_counter = 0;
228242
#endif /* CONFIG_ACCEL_SLEEP */

firmware/nRF51/tag-proximity/src/radio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,10 @@ void RADIO_IRQ_Handler(void)
413413
g_pkt_tracker.p.status.flags |= FLAG_LOG_STOPPED;
414414
#endif /* CONFIG_FLASH_LOGGING */
415415

416-
#if CONFIG_ACCEL_SLEEP
416+
#if CONFIG_ACCEL_MOTION
417417
if (moving)
418418
g_pkt_tracker.p.status.flags |= FLAG_MOVING;
419-
#endif /* CONFIG_ACCEL_SLEEP */
419+
#endif /* CONFIG_ACCEL_MOTION */
420420

421421
/* if we have just rebooted, set upper byte of flags to reset reason */
422422
if (status_flags & FLAG_BOOT)

0 commit comments

Comments
 (0)