finish up sleep clock

fix and finish up the phase clock code so it actuall works. Recorded the first sleep phase this night with uberclock and it looks quite good. Code needs cleanup however.
Needs connection checking and relinking on problems....
fertito
Daniel Poelzleithner 13 years ago
parent 374d4b3263
commit d5c538caa5

@ -60,7 +60,8 @@ while True:
ser.write(accDataRequest())
accel = ser.read(7)
if len(accel) < 3:
continue
if ord(accel[0]) != 0 and ord(accel[1]) != 0 and ord(accel[2]) != 0:
print "x: " + str(ord(accel[0])) + " y: " + str(ord(accel[1])) + " z: " + str(ord(accel[2]))

@ -94,9 +94,6 @@ unsigned char phase_clock_data[SIMPLICITI_MAX_PAYLOAD_LENGTH];
// 4 byte device address overrides SimpliciTI end device address set in "smpl_config.dat"
unsigned char phase_clock_ed_address[4];
// Length of data
unsigned char phase_clock_payload_length;
// 1 = send one or more reply packets, 0 = no need to reply
//unsigned char simpliciti_reply;
unsigned char phase_clock_reply_count;
@ -151,13 +148,16 @@ void sx_phase(u8 line)
if (is_bluerobin()) return;
#endif
// Start SimpliciTI in tx only mode
start_simpliciti_sleep();
start_simpliciti_tx_only(SIMPLICITI_PHASE_CLOCK);
}
inline u8 diff(u8 x1, u8 x2) {
u8 diff(u8 x1, u8 x2) {
u8 b1 = x1 - x2;
if(b1 > 128)
if(b1 > 127)
b1 = x2 - x1;
// high pass filter
if (b1 < 2)
return 0;
return b1;
}
@ -170,103 +170,34 @@ inline u8 diff(u8 x1, u8 x2) {
// *************************************************************************************************
void phase_clock_calcpoint() {
u16 x,y,z,res = 0;
// char *str;
u8 i = 0;
for(i=1;i<SLEEP_BUFFER-1;i++) {
for(i=1;i<SLEEP_BUFFER;i++) {
x += diff(sPhase.data[i-1][0], sPhase.data[i][0]);
y += diff(sPhase.data[i-1][1], sPhase.data[i][1]);
z += diff(sPhase.data[i-1][1], sPhase.data[i][1]);
z += diff(sPhase.data[i-1][2], sPhase.data[i][2]);
}
// can't overflow when SLEEP_BUFFER is not larger then 171
res = x + y + z;
memcpy(&sPhase.out + sPhase.data_nr, &res, sizeof(u16));
// reset stack index
sPhase.data_nr += sizeof(u16);
}
// Convert day to string
// str = itoa(x, 2, 0);
// display_chars(LCD_SEG_L2_5_0, str, SEG_ON);
// *************************************************************************************************
// @fn start_simpliciti_tx_only
// @brief Start SimpliciTI (tx only).
// @param simpliciti_state_t SIMPLICITI_ACCELERATION, SIMPLICITI_BUTTONS
// @return none
// *************************************************************************************************
void start_simpliciti_sleep()
{
// Display time in line 1
clear_line(LINE1);
fptr_lcd_function_line1(LINE1, DISPLAY_LINE_CLEAR);
display_time(LINE1, DISPLAY_LINE_UPDATE_FULL);
// Preset simpliciti_data with mode (key or mouse click) and clear other data bytes
simpliciti_data[0] = SIMPLICITI_PHASE_CLOCK_EVENTS;
/*
simpliciti_data[1] = 0;
simpliciti_data[2] = 0;
simpliciti_data[3] = 0;
*/
//memset(&simpliciti_data, 0, SIMPLICITI_MAX_PAYLOAD_LENGTH);
sPhase.data_nr = 0;
sPhase.out_nr = 0;
//memset(&simpliciti_data, 0, SIMPLICITI_MAX_PAYLOAD_LENGTH);
// Turn on beeper icon to show activity
display_symbol(LCD_ICON_BEEPER1, SEG_ON_BLINK_ON);
display_symbol(LCD_ICON_BEEPER2, SEG_ON_BLINK_ON);
display_symbol(LCD_ICON_BEEPER3, SEG_ON_BLINK_ON);
// Debounce button event
Timer0_A4_Delay(CONV_MS_TO_TICKS(BUTTONS_DEBOUNCE_TIME_OUT));
//display_chars(LCD_SEG_L2_5_0, (u8 *)" SLEEP", SEG_ON);
// Prepare radio for RF communication
open_radio();
sPhase.out[sPhase.out_nr] = res;
sPhase.out_nr++;
//sPhase.out[1] = res & 0xFF;
//sPhase.out[0] = (res >> 8) & 0xFF;
// Set SimpliciTI mode
sRFsmpl.mode = SIMPLICITI_PHASE_CLOCK;
// Set SimpliciTI timeout to save battery power
//sRFsmpl.timeout = SIMPLICITI_TIMEOUT;
// Start SimpliciTI stack. Try to link to access point.
// Exit with timeout or by a button DOWN press.
if (simpliciti_link())
{
// Start acceleration sensor
as_start();
// Enter TX only routine. This will transfer button events and/or acceleration data to access point.
simpliciti_main_tx_only();
}
// Set SimpliciTI state to OFF
sRFsmpl.mode = SIMPLICITI_OFF;
// Stop acceleration sensor
as_stop();
//memcpy(&sPhase.out + sPhase.out_nr, &res, sizeof(u16));
// reset stack index
//sPhase.out_nr += 2;
sPhase.data_nr = 0;
// Powerdown radio
close_radio();
// Clear last button events
Timer0_A4_Delay(CONV_MS_TO_TICKS(BUTTONS_DEBOUNCE_TIME_OUT));
BUTTONS_IFG = 0x00;
button.all_flags = 0;
// Clear icons
display_symbol(LCD_ICON_BEEPER1, SEG_OFF_BLINK_OFF);
display_symbol(LCD_ICON_BEEPER2, SEG_OFF_BLINK_OFF);
display_symbol(LCD_ICON_BEEPER3, SEG_OFF_BLINK_OFF);
// Clean up line 1
clear_line(LINE1);
display_time(LINE1, DISPLAY_LINE_CLEAR);
// Force full display update
display.flag.full_update = 1;
}
// *************************************************************************************************
// @fn display_phase_clock
// @brief SimpliciTI display routine.
@ -282,7 +213,7 @@ void display_phase_clock(u8 line, u8 update)
}
}
// *************************************************************************************************
// @fn is_rf
// @fn is_sleep
// @brief Returns TRUE if SimpliciTI receiver is connected.
// @param none
// @return u8

@ -1,6 +1,7 @@
// *************************************************************************************************
//
// Copyright (C) 2009 Texas Instruments Incorporated - http://www.ti.com/
// Copyright (C) 2010 Daniel Poelzleithner
//
//
// Redistribution and use in source and binary forms, with or without
@ -81,7 +82,7 @@ typedef enum
// collect n samples of data before tranmitting (saves energy)
#define SLEEP_BUFFER 30
#define SLEEP_COLLECT 1
#define SLEEP_OUT_BUFFER 10
// how often should a the clock be searched again
#define SEARCH_CLOCK (60*10)
@ -90,8 +91,10 @@ typedef enum
#define SLEEP_ACC_PREFIX 0x82cf // prefix when transmitting acceleration data
#define SLEEP_TIME_PREFIX 0x82aa // prefix when setting the clock
#define PHASE_CLOCK_SEND_LENGTH (SIMPLICITI_MAX_PAYLOAD_LENGTH-2)
#define PHASE_CLOCK_BUFFER ((SIMPLICITI_MAX_PAYLOAD_LENGTH-2)/sizeof(u16))
//#define PHASE_CLOCK_SEND_LENGTH (SIMPLICITI_MAX_PAYLOAD_LENGTH-2)
#define PHASE_CLOCK_SEND_LENGTH 2
#define PHASE_CLOCK_BUFFER 2
//#define PHASE_CLOCK_BUFFER ((SIMPLICITI_MAX_PAYLOAD_LENGTH-2)/sizeof(u16))
// Button flags for SimpliciTI data
//#define SIMPLICITI_BUTTON_STAR (0x10)
@ -119,7 +122,8 @@ struct SPhase
//
u8 data[SLEEP_BUFFER][3];
u8 data_nr;
u16 out[PHASE_CLOCK_BUFFER];
//u8 out[PHASE_CLOCK_BUFFER];
u16 out[SLEEP_OUT_BUFFER];
u8 out_nr;
};
extern struct SPhase sPhase;

@ -211,6 +211,7 @@ void sx_sync(u8 line)
void start_simpliciti_tx_only(simpliciti_mode_t mode)
{
// Display time in line 1
u8 start_as = 0;
clear_line(LINE1);
fptr_lcd_function_line1(LINE1, DISPLAY_LINE_CLEAR);
display_time(LINE1, DISPLAY_LINE_UPDATE_FULL);
@ -219,7 +220,17 @@ void start_simpliciti_tx_only(simpliciti_mode_t mode)
if (mode == SIMPLICITI_ACCELERATION)
{
simpliciti_data[0] = SIMPLICITI_MOUSE_EVENTS;
start_as = 1;
}
#ifdef CONFIG_PHASE_CLOCK
else if (mode == SIMPLICITI_PHASE_CLOCK)
{
simpliciti_data[0] = SIMPLICITI_PHASE_CLOCK_EVENTS;
start_as = 1;
display_symbol(LCD_ICON_RECORD, SEG_ON_BLINK_ON);
}
#endif
else
{
simpliciti_data[0] = SIMPLICITI_KEY_EVENTS;
@ -249,7 +260,7 @@ void start_simpliciti_tx_only(simpliciti_mode_t mode)
// Exit with timeout or by a button DOWN press.
if (simpliciti_link())
{
if (mode == SIMPLICITI_ACCELERATION)
if (start_as)
{
// Start acceleration sensor
as_start();
@ -277,6 +288,14 @@ void start_simpliciti_tx_only(simpliciti_mode_t mode)
display_symbol(LCD_ICON_BEEPER1, SEG_OFF_BLINK_OFF);
display_symbol(LCD_ICON_BEEPER2, SEG_OFF_BLINK_OFF);
display_symbol(LCD_ICON_BEEPER3, SEG_OFF_BLINK_OFF);
#ifdef CONFIG_PHASE_CLOCK
if (mode == SIMPLICITI_PHASE_CLOCK)
{
display_symbol(LCD_ICON_RECORD, SEG_OFF_BLINK_OFF);
}
#endif
// Clean up line 1
clear_line(LINE1);
@ -360,6 +379,8 @@ void simpliciti_get_ed_data_callback(void)
{
static u8 packet_counter = 0;
u8 i;
u16 res;
char *str;
if (sRFsmpl.mode == SIMPLICITI_ACCELERATION)
{
@ -394,7 +415,7 @@ void simpliciti_get_ed_data_callback(void)
} else if (sRFsmpl.mode == SIMPLICITI_PHASE_CLOCK)
{
// Wait for next sample
Timer0_A4_Delay(CONV_MS_TO_TICKS(10));
Timer0_A4_Delay(CONV_MS_TO_TICKS(20));
// Read from sensor if DRDY pin indicates new data (set in PORT2 ISR)
if (request.flag.acceleration_measurement && ((AS_INT_IN & AS_INT_PIN) == AS_INT_PIN))
{
@ -405,27 +426,64 @@ void simpliciti_get_ed_data_callback(void)
as_get_data(sAccel.xyz);
// push messured data onto the stack
if (sPhase.data_nr == SLEEP_BUFFER-1) {
if (sPhase.data_nr > SLEEP_BUFFER-1) {
phase_clock_calcpoint();
/*simpliciti_data[1] = sPhase.out[0];
simpliciti_data[2] = sPhase.out[1];
simpliciti_data[3] = packet_counter++; //8>>sPhase.out[1]&&0xFF;
simpliciti_payload_length = 4;
simpliciti_flag |= SIMPLICITI_TRIGGER_SEND_DATA;
*/
} else {
// copy current value onto the stack
memcpy(&sPhase.data[sPhase.data_nr][0], &sAccel.xyz, sizeof(u8)*3);
//memcpy(&sPhase.data[sPhase.data_nr][0], &sAccel.xyz, sizeof(u8)*3);
sPhase.data[sPhase.data_nr][0] = sAccel.xyz[0];
sPhase.data[sPhase.data_nr][1] = sAccel.xyz[1];
sPhase.data[sPhase.data_nr][2] = sAccel.xyz[2];
//simpliciti_data[3] = sAccel.xyz[2];
sPhase.data_nr++;
}
sPhase.out_nr += sizeof(u16);
if (sPhase.out_nr >= PHASE_CLOCK_SEND_LENGTH)
//str = itoa(accel_data, 3, 0);
if (sPhase.out_nr > SLEEP_OUT_BUFFER-1)
{
// Reset counter
sPhase.out_nr = 1;
sPhase.out_nr = 0;
// copy out buffer into the simplicti out buffer skip prefix
memcpy(&simpliciti_data+sizeof(u8), &sPhase.out, PHASE_CLOCK_SEND_LENGTH);
simpliciti_data[0] = SIMPLICITI_PHASE_CLOCK_EVENTS;
//memcpy(&simpliciti_data+sizeof(u8), &sPhase.out, PHASE_CLOCK_SEND_LENGTH);
for(i=0; i < SLEEP_OUT_BUFFER; i++) {
//if (((2**17)-1)
// FIXME: overflow detection ?
res += sPhase.out[i];
}
simpliciti_data[3] = packet_counter++;
simpliciti_data[2] = res & 0xFF;
simpliciti_data[1] = (res >> 8) & 0xFF;
simpliciti_payload_length = 4;
//simpliciti_data[0] = SIMPLICITI_PHASE_CLOCK_EVENTS;
// Trigger packet sending
// str = itoa(simpliciti_data[2], 2, 0);
// clear_line(LINE1);
// display_chars(LCD_SEG_L2_5_0, str, SEG_ON);
// Force full display update
//display.flag.full_update = 1;
//str = itoa(simpliciti_data[1], 2, 0);
// display_chars(LCD_SEG_L2_5_2, str, SEG_ON);
//display_chars(LCD_SEG_L1_3_0, str, SEG_ON);
//str = itoa(simpliciti_data[2], 2, 0);
//display_chars(LCD_SEG_L1_1_0, str, SEG_ON);
simpliciti_flag |= SIMPLICITI_TRIGGER_SEND_DATA;
}
sRFsmpl.timeout = SIMPLICITI_TIMEOUT;
}
#endif

Loading…
Cancel
Save