Skip to content

Commit

Permalink
states and switch with button press ( needs debouncing )
Browse files Browse the repository at this point in the history
  • Loading branch information
xriss committed Jan 12, 2020
1 parent 0b1996a commit 332612e
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 8 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Expand Up @@ -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} <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_C_COMPILER} <LINK_FLAGS> <OBJECTS> -lstdc++ -o <TARGET> <LINK_LIBRARIES>")
set(CMAKE_C_LINK_EXECUTABLE "${CMAKE_C_COMPILER} <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES> -lm")
set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_C_COMPILER} <LINK_FLAGS> <OBJECTS> -lstdc++ -o <TARGET> <LINK_LIBRARIES> -lm")

# basic board definitions and drivers
include_directories(
Expand Down
14 changes: 13 additions & 1 deletion src/main.c
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/main.h
Expand Up @@ -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;
Expand All @@ -19,3 +21,4 @@ extern struct shader_font main_lines[16];
// states

extern int main_test(int mode);
extern int main_clock1(int mode);
188 changes: 188 additions & 0 deletions src/main_clock1.c
@@ -0,0 +1,188 @@

#include <time.h>
#include <string.h>
#include <math.h>

#include <nrf.h>
#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;
}
6 changes: 2 additions & 4 deletions src/main_test.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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!");
Expand All @@ -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;
Expand All @@ -186,7 +185,6 @@ PRINTF("UPDATE TEST\n");
{
lcd_shader(0,y+f,240,1,shader_test,&i); // interlace updates
}

frame++;
}
return 0;
Expand Down
5 changes: 5 additions & 0 deletions timecake.cmake
Expand Up @@ -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
Expand Down Expand Up @@ -36,6 +40,7 @@ list(APPEND SOURCE_FILES

src/main.c
src/main_test.c
src/main_clock1.c

)

Expand Down

0 comments on commit 332612e

Please sign in to comment.