From 332612e0baacddcbeab549cc7946a00a428095ae Mon Sep 17 00:00:00 2001 From: kriss Date: Sun, 12 Jan 2020 04:41:19 +0000 Subject: [PATCH] states and switch with button press ( needs debouncing ) --- CMakeLists.txt | 6 +- src/main.c | 14 +++- src/main.h | 3 + src/main_clock1.c | 188 ++++++++++++++++++++++++++++++++++++++++++++++ src/main_test.c | 6 +- timecake.cmake | 5 ++ 6 files changed, 214 insertions(+), 8 deletions(-) create mode 100644 src/main_clock1.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a9f446..bf51a11 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -113,10 +113,10 @@ set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O1") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3") set(CMAKE_ASM_FLAGS "-MP -MD -std=c99 -x assembler-with-cpp") - set(CMAKE_EXE_LINKER_FLAGS "-mthumb -mabi=aapcs -std=gnu++98 -std=c99 -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT} ${CPU_FLAGS} -Wl,--gc-sections -lc -lm ${SPECS_LINKER_FLAGS}") + set(CMAKE_EXE_LINKER_FLAGS "-mthumb -mabi=aapcs -std=gnu++98 -std=c99 -L ${NRF5_SDK_PATH}/modules/nrfx/mdk -T${NRF5_LINKER_SCRIPT} ${CPU_FLAGS} -Wl,--gc-sections -lc ${SPECS_LINKER_FLAGS}") # note: we must override the default cmake linker flags so that CMAKE_C_FLAGS are not added implicitly - set(CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_COMPILER} -o ") - set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_C_COMPILER} -lstdc++ -o ") + set(CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_COMPILER} -o -lm") + set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_C_COMPILER} -lstdc++ -o -lm") # basic board definitions and drivers include_directories( diff --git a/src/main.c b/src/main.c index 804e7a3..9739719 100644 --- a/src/main.c +++ b/src/main.c @@ -15,19 +15,21 @@ // hardware reference manual // https://infocenter.nordicsemi.com/pdf/nRF52832_OPS_v0.6.3.pdf +int main_butt=0; char main_text[32*16]; struct shader_font main_lines[16]; int main_state=0; -int main_state_next=1; +int main_state_next=2; int main_state_call(int mode) { switch(main_state) { case 1: return main_test(mode); + case 2: return main_clock1(mode); } return 0; } @@ -53,10 +55,20 @@ int main(void) main_state=main_state_next; main_state_next=0; + if(main_state>2) { main_state=1; } // max state wrap + main_state_call(1); // setup new state } + main_butt = button_read(); + main_state_call(2); // update current state + + if( main_butt & 2 ) // pressed down + { + main_state_next=main_state+1; + } + } return 0; diff --git a/src/main.h b/src/main.h index 43895a4..94ef66f 100644 --- a/src/main.h +++ b/src/main.h @@ -3,6 +3,8 @@ // hardware reference manual // https://infocenter.nordicsemi.com/pdf/nRF52832_OPS_v0.6.3.pdf +// button state updated every frame +extern int main_butt; extern int main_state; @@ -19,3 +21,4 @@ extern struct shader_font main_lines[16]; // states extern int main_test(int mode); +extern int main_clock1(int mode); diff --git a/src/main_clock1.c b/src/main_clock1.c new file mode 100644 index 0000000..8394cbc --- /dev/null +++ b/src/main_clock1.c @@ -0,0 +1,188 @@ + +#include +#include +#include + +#include +#include "nrf_gpio.h" + +#include "sys/debug.h" + +#include "sys/saveram.h" +#include "sys/acc.h" +#include "sys/heart.h" +#include "sys/touch.h" +#include "sys/battery.h" +#include "sys/lcd.h" +#include "sys/clock.h" +#include "sys/button.h" + +#include "main.h" + +static int frame=0; +static struct tm *clocks=0; + + + +static int shader_test(int _x,int _y,void *data) +{ + int x=239-_y; // rotate screen + int y=_x; + + int r=-1; // background flag + + int ty=(y/16)&0xf; // 0-15 line + struct shader_font *line=main_lines+ty; + if(line->length>0) // render some text + { + r=shader_textline(x,y,line); + } + + + if(r==-1) // fill background with simple animation + { + int d=120; + int dd=d*d; + float cx=x-120.0f; + float cy=y-120.0f; + float cc=( cx*cx + cy*cy ); + if( cc < dd ) + { + r=0x000010; + + float t=(atan2f(-cx,cy)/(M_PI*2.0f))+0.5f; + float c=sqrtf(cc)/120.0f; + + if(c<0.2f) + { + if( t*365.0f < clocks->tm_yday ) + { r=0x444444; } + } + else + if(c<0.4f) + { + if( t*31.0f < clocks->tm_mday ) + { r=0x666666; } + } + else + if(c<0.6f) + { + if( t*24.0f < clocks->tm_hour ) + { r=0x888888; } + } + else + if(c<0.8f) + { + if( t*60.0f < clocks->tm_min ) + { r=0xaaaaaa; } + } + else + { + if( t*60.0f < clocks->tm_sec ) + { r=0xcccccc; } + } + } + else + { + r=0x000010; + } + + } + return r; +} + + +static int main_setup() +{ + // setup text screen buffers for a 30x15 character display. + for(int idx=0;idx<16;idx++) + { + struct shader_font *line=main_lines+idx; + line->name=funfont_8x16r; + line->hx=8; + line->hy=16; + line->foreground=0xffffff; + line->dropshadow=0x000000; + line->background=-1; // transparent + line->ax=0;line->mx=1;line->dx=1; + line->ay=0;line->my=1;line->dy=1; + + line->ay=-(idx*16); + + line->text=main_text+(idx*32); // 32 char buffers per line (30 visible with 8x16 font) + line->text[0]=0; + line->length=0; + } + return 0; +} + +static int main_clean() +{ + return 0; +} + +static int main_update() +{ + + time_t t16 = clock_time(); // seconds since 1970 * 65536 + time_t t = t16>>16; // seconds since 1970 + clocks = localtime(&t); + + int flags; + float voltage; + float percent; + battery_read(&flags,&voltage,&percent); + char * charging=" "; +// if(flags==2) { charging="===="; } // Charged (never reaches this state?) +// else + if(flags&1) // Charging + { + switch(frame&3) // animate + { + case 0: charging="+ "; break; + case 1: charging="++ "; break; + case 2: charging="+++ "; break; + case 3: charging="++++"; break; + } + } + else // Discharging + { + switch(frame&3) // animate + { + case 0: charging="----"; break; + case 1: charging="--- "; break; + case 2: charging="-- "; break; + case 3: charging="- "; break; + } + } + + int idx=0; +// snprintf(main_lines[idx++].text,32," %d.%03dv : %3d%% %s",(int)voltage,(int)((voltage-(int)voltage)*1000.0f),(int)percent,charging); +// snprintf(main_lines[idx++].text,32,"%d-%02d-%02d %02d:%02d:%02d", clocks->tm_year+1900 , clocks->tm_mon + 1, clocks->tm_mday, clocks->tm_hour, clocks->tm_min, clocks->tm_sec ); + + for(int idx=0;idx<16;idx++) { main_lines[idx].length=strlen(main_lines[idx].text); } + + lcd_shader(0,0,240,240,shader_test,0); // interlace updates +/* + int f=frame&1; + for(int y=0;y<240;y+=2) + { + lcd_shader(0,y+f,240,1,shader_test,0); // interlace updates + } +*/ + + frame++; + return 0; +} + + +int main_clock1(int mode) +{ + switch(mode) + { + case 1: return main_setup(); + case 2: return main_update(); + case 3: return main_clean(); + } + return 0; +} diff --git a/src/main_test.c b/src/main_test.c index faf2dab..0f3cb51 100644 --- a/src/main_test.c +++ b/src/main_test.c @@ -75,6 +75,7 @@ PRINTF("SETUP TEST\n"); line->ay=-(idx*16); line->text=main_text+(idx*32); // 32 char buffers per line (30 visible with 8x16 font) + line->text[0]=0; line->length=0; } return 0; @@ -154,8 +155,6 @@ PRINTF("UPDATE TEST\n"); // unsigned char * acc=touch_read(); - int butt = button_read(); - int idx=0; // snprintf(lines[idx++].text,32,"Hello World!"); @@ -165,7 +164,7 @@ PRINTF("UPDATE TEST\n"); snprintf(main_lines[idx++].text,32,"Clock %u . %04x", (unsigned int)t,(int)(t16&0xffff)); - snprintf(main_lines[idx++].text,32,"Butt %d", butt ); + snprintf(main_lines[idx++].text,32,"Butt %d", main_butt ); /* int ai=0; @@ -186,7 +185,6 @@ PRINTF("UPDATE TEST\n"); { lcd_shader(0,y+f,240,1,shader_test,&i); // interlace updates } - frame++; } return 0; diff --git a/timecake.cmake b/timecake.cmake index fe9b54d..2a36520 100644 --- a/timecake.cmake +++ b/timecake.cmake @@ -6,6 +6,10 @@ set(NRF_PROJECT_NAME timecake) set(EXECUTABLE_NAME timecake) +list(APPEND LINK_LIBRARIES + m +) + include_directories(./src) list(APPEND SOURCE_FILES @@ -36,6 +40,7 @@ list(APPEND SOURCE_FILES src/main.c src/main_test.c + src/main_clock1.c )