From 761c56f4f23458123e8a77b7de8d3b5a07f5ad92 Mon Sep 17 00:00:00 2001 From: schneider Date: Wed, 3 Aug 2011 11:34:18 +0200 Subject: [PATCH 01/41] s.c --- firmware/applications/s.c | 130 +++++++------------------------------- 1 file changed, 22 insertions(+), 108 deletions(-) diff --git a/firmware/applications/s.c b/firmware/applications/s.c index e6d4910..f047c80 100644 --- a/firmware/applications/s.c +++ b/firmware/applications/s.c @@ -5,120 +5,34 @@ #include "lcd/render.h" #include "lcd/allfonts.h" -void ReinvokeISP(void); -void EnableWatchdog(uint32_t ms); -void delayms(uint32_t ms); +void backlightInit(void); /**************************************************************************/ void main_s(void) { - //Make PIO1_11 an analog input - gpioSetDir(RB_LED3, gpioDirection_Input); - IOCON_PIO1_11 = 0x41; - - backlightInit(); - - uint32_t j=0; - - //disable the JTAG on PIO3_3 - IOCON_PIO3_3 = 0x10; - - int yctr=8; - int dx=0; - - font_direction = FONT_DIR_LTR; // LeftToRight is the default - font = &Font_8x8; - - static FONT fonts[] = { - &Font_7x8, - &Font_Ubuntu18pt, // 3 byte-font - &Font_8x8, - }; - - int fontctr=0; - yctr=18; - - uint8_t trigger; - -#define SEND -#ifdef SEND - trigger=200; - gpioSetDir(RB_LED0, gpioDirection_Output); - IOCON_JTAG_TDI_PIO0_11 = 0x11; -#else - trigger=380; - gpioSetDir(RB_LED0, gpioDirection_Input); - IOCON_JTAG_TDI_PIO0_11 = 0x42; -#endif - - - uint32_t ctr=0; + uint8_t key; + //lcdClear(); while (1) { - ctr++; - uint32_t results; - lcdDisplay(); - delayms(10); + uint8_t contrast = lcdRead(0xDF); + lcdInit(); // display + lcdPrint("reg="); + lcdPrintCharHex(contrast); + lcdNl(); + lcdDisplay(); + while(1); + delayms(500); - font=fonts[fontctr]; + //key= getInputWait(); - if(gpioGetValue(RB_BTN3)==0){ - while(gpioGetValue(RB_BTN3)==0); - trigger +=10; - }; - if(gpioGetValue(RB_BTN2)==0){ - while(gpioGetValue(RB_BTN2)==0); - trigger -=10; - }; - //dx=DoString(0,0,"Trig:"); - //dx=DoInt(dx,0,trigger); - //DoString(dx,0," "); - - if(gpioGetValue(RB_BTN0)==0){ - while(gpioGetValue(RB_BTN0)==0); - DoString(0,8,"Enter ISP!"); - lcdDisplay(); - EnableWatchdog(1000*5); - ReinvokeISP(); - }; - - font = &Font_Ubuntu36pt; - static uint8_t ctrx=0, ctry=0, dirx=1, diry=1; - dx=DoString(ctrx,ctry,"S"); - if( dirx ){ - if(ctrx++ == 60) - dirx = 0; - }else{ - if(ctrx-- == 0) - dirx=1; + // Easy flashing + if(key==BTN_LEFT){ + DoString(0,8,"Enter ISP!"); + lcdDisplay(); + ISPandReset(); + } } - if( diry ){ - if(ctry++ == 12) - diry = 0; - }else{ - if(ctry-- == 0) - diry=1; - } - font = &Font_7x8; - - results = adcRead(1); - //dx=DoString(0,yctr+28,"Voltage:"); - results *= 10560; - results /= 1024; - //DoInt(dx,yctr+28,results); - - if( results < 3500 ){ - DoString(0,yctr+30,"Shutdown"); - gpioSetValue (RB_PWR_GOOD, 0); - gpioSetValue (RB_LCD_BL, 0); - SCB_SCR |= SCB_SCR_SLEEPDEEP; - PMU_PMUCTRL = PMU_PMUCTRL_DPDEN_DEEPPOWERDOWN; - __asm volatile ("WFI"); - }else{ - //DoString(0,yctr+30,"OK "); - ; - } - - } - - return; } + +void tick_minimal(void){ + return; +}; From 9b2cee24d03fcb9bbc97f4bbdb9cad9403fcaec5 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 17:10:52 +0200 Subject: [PATCH 02/41] Allow parsing of /*-style comments --- firmware/l0dable/mktable.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/l0dable/mktable.pl b/firmware/l0dable/mktable.pl index cc96e87..9c56a50 100755 --- a/firmware/l0dable/mktable.pl +++ b/firmware/l0dable/mktable.pl @@ -47,14 +47,14 @@ sub wanted { open(F,"<",$_) || die; while(){ chomp;s/\r$//; - if(m!^[^(]* ([\w]+)\s*\(.*\);\s*(//.*)?$!){ + if(m!^[^(]* ([\w]+)\s*\(.*\);\s*(//.*)?(/\*[^/]*\*/)?$!){ $id=$1; s/$id/(*)/; s/;//; s!//.*!!; $types{$id}="*($_)"; $files{$id}=$File::Find::name; - }elsif (m!^\s*extern\s[^(]* ([\w]+)\s*(\[\w*\]\s*)?;\s*(//.*)?$!){ + }elsif (m!^\s*extern\s[^(]* ([\w]+)\s*(\[\w*\]\s*)?;\s*(//.*)?(/\*[^/]*\*/)?$!){ $id=$1; s/extern //; my $star="*"; From 3542f8acd6f7d3c6a9b930112c7158d8e51d6445 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 17:11:50 +0200 Subject: [PATCH 03/41] Ignore empty lines in EXPORTS --- firmware/l0dable/mktable.pl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/firmware/l0dable/mktable.pl b/firmware/l0dable/mktable.pl index 9c56a50..38cf043 100755 --- a/firmware/l0dable/mktable.pl +++ b/firmware/l0dable/mktable.pl @@ -18,6 +18,8 @@ my @symb; open(Q,"<","l0dable/EXPORTS") || die "$!"; while(){ chomp;s/\r$//; + next if /^#/; + next if /^\s*$/; push @symb,$_; }; close(Q); From 6dcdaf17fe9ed607f48d8288ca2068a93d3e51f1 Mon Sep 17 00:00:00 2001 From: schneider Date: Wed, 3 Aug 2011 17:38:30 +0200 Subject: [PATCH 04/41] added sendcard l0dable --- firmware/l0dable/EXPORTS | 12 ++ firmware/l0dable/loadable.ld | 2 +- firmware/l0dable/sendcard.c | 284 +++++++++++++++++++++++++++++++++++ 3 files changed, 297 insertions(+), 1 deletion(-) create mode 100644 firmware/l0dable/sendcard.c diff --git a/firmware/l0dable/EXPORTS b/firmware/l0dable/EXPORTS index 05a3962..ea15048 100644 --- a/firmware/l0dable/EXPORTS +++ b/firmware/l0dable/EXPORTS @@ -35,3 +35,15 @@ push_queue the_config the_queue work_queue +selectFile +nrf_snd_pkt_crc_encr +nrf_rcv_pkt_time_encr +getInput +ECIES_encyptkeygen +f_open +f_read +strlen +strcpy +xxtea_encode_words +getRandom +crc16 diff --git a/firmware/l0dable/loadable.ld b/firmware/l0dable/loadable.ld index fb4d66c..b569c2f 100644 --- a/firmware/l0dable/loadable.ld +++ b/firmware/l0dable/loadable.ld @@ -1,5 +1,5 @@ MEMORY { - sram(rwx): ORIGIN = 0x10002000 - 2048, LENGTH = 2048 + sram(rwx): ORIGIN = 0x10002000 - 2548, LENGTH = 2548 } INCLUDE ram.ld diff --git a/firmware/l0dable/sendcard.c b/firmware/l0dable/sendcard.c new file mode 100644 index 0000000..8bcf110 --- /dev/null +++ b/firmware/l0dable/sendcard.c @@ -0,0 +1,284 @@ +#include +#include +#include +#include +#include +#include "basic/basic.h" +#include "lcd/render.h" +#include "lcd/allfonts.h" +#include "basic/ecc.h" +#include "funk/nrf24l01p.h" +#include "filesystem/ff.h" +#include "filesystem/diskio.h" +#include "funk/filetransfer.h" +#include "lcd/print.h" +#include +#include "funk/nrf24l01p.h" +#include "funk/filetransfer.h" +#include "funk/rftransfer.h" +#include "basic/basic.h" +#include "basic/xxtea.h" +#include "filesystem/ff.h" +#include "funk/rftransfer.h" +#include "funk/nrf24l01p.h" +#include +#include +#include +//#include + +#include "usetable.h" + +//#include "lcd/print.h" + + + +uint8_t mac[5] = {1,2,3,2,1}; + +void ram(void) +{ + char file[13]; + selectFile(file,"TXT"); + sendFile(file); +} + +void sendR(uint8_t *rx, uint8_t *ry) +{ + uint8_t exp[2 + 4*NUMWORDS + 2]; + exp[0] = 'R'; + for(int i=0; i<4*NUMWORDS; i++) + exp[2+i] = rx[i]; + exp[1] = 'X'; + nrf_snd_pkt_crc(32, exp); + delayms(10); + exp[1] = 'Y'; + for(int i=0; i<4*NUMWORDS; i++) + exp[2+i] = ry[i]; + nrf_snd_pkt_crc(32, exp); + delayms(10); +} + +int receiveKey(uint8_t type, uint8_t *x, uint8_t *y) +{ + uint8_t buf[32]; + uint8_t n; + + n = nrf_rcv_pkt_time(1000, 32, buf); + if( n == 32 && buf[0] == type && buf[1] == 'X' ){ + for(int i=0; i MAXSIZE ) + return 1; //File to big + + res=f_open(&file, (const char*)filename, FA_OPEN_EXISTING|FA_READ); + if( res ) + return res; + + //res = f_read(&file, (char *)buf, size, &readbytes); + for(uint16_t i=0; i> 8; + metadata[21] = size & 0xFF; + + //nrf_get_tx_max(5,macbuf); + + //nrf_set_tx_mac(5, mac); + nrf_snd_pkt_crc_encr(32, metadata, k); + delayms(20); + xxtea_encode_words((uint32_t *)buf, wordcount, k); + rftransfer_send(wordcount*4, buf); + //nrf_set_tx_mac(5, macbuf); + return 0; +} + +#define MAXPACKET 32 +void rftransfer_send(uint16_t size, uint8_t *data) +{ + uint8_t buf[MAXPACKET]; + buf[0] = 'L'; + buf[1] = size >> 8; + buf[2] = size & 0xFF; + + uint16_t rand = getRandom() & 0xFFFF; + buf[3] = rand >> 8; + buf[4] = rand & 0xFF; + + nrf_snd_pkt_crc(32,buf); //setup packet + delayms(20); + uint16_t index = 0; + uint8_t i; + uint16_t crc = crc16(data,size); + + while(size){ + buf[0] = 'D'; + buf[1] = index >> 8; + buf[2] = index & 0xFF; + buf[3] = rand >> 8; + buf[4] = rand & 0xFF; + for(i=5; i0; i++,size--){ + buf[i] = *data++; + } + index++; + nrf_snd_pkt_crc(32,buf); //data packet + delayms(20); + } + + buf[0] = 'C'; + buf[1] = crc >> 8; + buf[2] = crc & 0xFF; + buf[3] = rand >> 8; + buf[4] = rand & 0xFF; + nrf_snd_pkt_crc(32,buf); //setup packet + delayms(20); +} + From 4f5ec4e8fc44a4551f4a9f8d6aad3430555598e0 Mon Sep 17 00:00:00 2001 From: iggy Date: Wed, 3 Aug 2011 17:45:57 +0200 Subject: [PATCH 05/41] invaders scoring --- firmware/loadable/spaceinvaders.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/firmware/loadable/spaceinvaders.c b/firmware/loadable/spaceinvaders.c index c4f6457..029625e 100644 --- a/firmware/loadable/spaceinvaders.c +++ b/firmware/loadable/spaceinvaders.c @@ -6,9 +6,9 @@ #include "lcd/display.h" #include "lcd/allfonts.h" -void ReinvokeISP(void); -void EnableWatchdog(uint32_t ms); -void delayms(uint32_t ms); +//void ReinvokeISP(void); +//void EnableWatchdog(uint32_t ms); +//void delayms(uint32_t ms); /**************************************************************************/ #define POS_PLAYER_Y 60 @@ -150,11 +150,17 @@ void init_game(void) { game.shot_y = 0; game.alive = ENEMY_ROWS*ENEMY_COLUMNS; game.move = 0; - game.direction = -1; - game.lastcol = ENEMY_COLUMNS-1; + if (getRandom()%2 == 0) { + game.direction = -1; + game.lastcol = ENEMY_COLUMNS-1; + } else { + game.direction = 1; + game.lastcol = 0; + } game.killed = 0; game.step = false; game.ufo = DISABLED; + game.score = 0; init_enemy(); for (char col=0; col Date: Wed, 3 Aug 2011 18:01:16 +0200 Subject: [PATCH 06/41] invaders cleanup, no more warnings --- firmware/loadable/spaceinvaders.c | 74 ++++++++----------------------- 1 file changed, 18 insertions(+), 56 deletions(-) diff --git a/firmware/loadable/spaceinvaders.c b/firmware/loadable/spaceinvaders.c index 029625e..fbcf38a 100644 --- a/firmware/loadable/spaceinvaders.c +++ b/firmware/loadable/spaceinvaders.c @@ -1,15 +1,13 @@ #include +#include #include "basic/basic.h" +#include "basic/random.h" #include "lcd/render.h" #include "lcd/display.h" #include "lcd/allfonts.h" -//void ReinvokeISP(void); -//void EnableWatchdog(uint32_t ms); -//void delayms(uint32_t ms); - /**************************************************************************/ #define POS_PLAYER_Y 60 #define POS_PLAYER_X RESX/2-3 @@ -20,11 +18,11 @@ #define UFO_PROB 1024 -#define TYPE_PLAYER 1 +#define TYPE_PLAYER 1 #define TYPE_ENEMY_A 3 #define TYPE_ENEMY_B 2 #define TYPE_ENEMY_C 4 -#define TYPE_UFO 5 +#define TYPE_UFO 5 #define BUNKERS 3 #define BUNKER_WIDTH 10 @@ -52,16 +50,19 @@ struct gamestate { char key; void init_game(); +void init_enemy(); void check_end(); void move_ufo(); +void move_shot(); void move_shots(); void move_player(); -void move_enemay(); +void move_enemy(); void draw_score(); void draw_bunker(); void draw_player(); void draw_enemy(); void draw_shots(); +void draw_sprite(char type, char x, char y); void draw_ufo(); void screen_intro(); void screen_gameover(); @@ -163,7 +164,7 @@ void init_game(void) { game.score = 0; init_enemy(); - for (char col=0; colBUNKER_X[BUNKERS-1-b] && -// game.shot_xRESY-16) { -// int offset = BUNKER_WIDTH - (game.shot_x-BUNKER_X[BUNKERS-1-b]); -// if (game.bunker[b][offset]!=0) { -// game.bunker[b][offset]&=game.bunker[b][offset]<<1; -// game.shot_x=DISABLED; -// } -// } -// } - if (check_bunker(game.shot_x,game.shot_y-5,1 )) + + if (check_bunker(game.shot_x,game.shot_y-5,1 )) game.shot_x=DISABLED; //check for collision with enemy, kill enemy if @@ -254,6 +242,7 @@ void move_shot() { } } } + //check for collision with ufo if (game.ufo != DISABLED && game.shot_x>game.ufo && @@ -271,10 +260,10 @@ void move_shot() { void move_shots() { - for (char col = 0; colBUNKER_X[BUNKERS-1-b] && -// game.shots_x[col]RESY-16) { -// int offset = BUNKER_WIDTH - (game.shots_x[col]-BUNKER_X[BUNKERS-1-b])-1; -// if (game.bunker[b][offset]!=0) { -// game.bunker[b][offset]&=game.bunker[b][offset]>>1; -// game.shots_x[col]=DISABLED; -// } -// } -// } - //check for collision with player - + //check for collision with player if (game.shots_y[col] >= RESY-13 && game.shots_x[col] > game.player+1 && game.shots_x[col] < game.player+6) { @@ -369,25 +345,12 @@ void move_enemy() { game.killed = true; } check_bunker(pos,game.enemy_row_y[row]+8,-2); - //check for collision with bunker, dirty - // for (int b=0; b=BUNKER_X[BUNKERS-1-b] && - // pos<=BUNKER_X[BUNKERS-1-b]+BUNKER_WIDTH && - // game.enemy_row_y[row]+8RESY-16) { - // int offset = BUNKER_WIDTH - (pos-BUNKER_X[BUNKERS-1-b]); - // if (game.bunker[b][offset]!=0) { - // game.bunker[b][offset]&=game.bunker[b][offset]>>2; - // } - // } - // } - - + //Are we at the beginning or end? Direction change if((pos <=0 && game.direction != 1) || (pos >=RESX-10 && game.direction == 1)){ game.direction = (game.direction==1)?-1:1; - for (char r = 0; r=23?4:2; } return; @@ -401,7 +364,6 @@ void move_enemy() { } void draw_player() { - //draw_sprite(50, 20); draw_sprite(TYPE_PLAYER, game.player, POS_PLAYER_Y); } @@ -433,7 +395,7 @@ void draw_shots() { } } - for (char col = 0; col < ENEMY_COLUMNS; col++) { + for (int col = 0; col < ENEMY_COLUMNS; col++) { if (game.shots_x[col] != DISABLED) { for (int length=0; length<=5; length++) { lcdSetPixel(game.shots_x[col], game.shots_y[col]+length,true); From 11ae0a5b9a39e70afc0ce7476d7bcab0a3047a1f Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 18:19:07 +0200 Subject: [PATCH 07/41] Make Timeout of 0 work as intended (no timeout) --- firmware/basic/keyin.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/firmware/basic/keyin.c b/firmware/basic/keyin.c index a895a0f..cc4cecb 100644 --- a/firmware/basic/keyin.c +++ b/firmware/basic/keyin.c @@ -52,6 +52,8 @@ uint8_t getInputWait(void) { uint8_t getInputWaitTimeout(int timeout) { uint8_t key; + if(timeout==0) + return getInputWait(); int end=_timectr+timeout*(1000/SYSTICKSPEED); while ((key=getInputRaw())==BTN_NONE){ if(_timectr>end) From c62c06c08764cc5d2e9f246f525bcbf9b0c9ff8c Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 18:21:49 +0200 Subject: [PATCH 08/41] Make prototypes single-line so my parser understands them --- firmware/basic/ecc.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/firmware/basic/ecc.h b/firmware/basic/ecc.h index d946cd3..05b1d3b 100644 --- a/firmware/basic/ecc.h +++ b/firmware/basic/ecc.h @@ -17,20 +17,16 @@ int bitstr_parse_export(char *exp, const char *s); void ECIES_setup(void); -void ECIES_encyptkeygen(uint8_t *px, uint8_t *py, - uint8_t k1[16], uint8_t k2[16], uint8_t *Rx_exp, uint8_t *Ry_exp); +void ECIES_encyptkeygen(uint8_t *px, uint8_t *py, uint8_t k1[16], uint8_t k2[16], uint8_t *Rx_exp, uint8_t *Ry_exp); -int ECIES_decryptkeygen(uint8_t *rx, uint8_t *ry, - uint8_t k1[16], uint8_t k2[16], const char *privkey); +int ECIES_decryptkeygen(uint8_t *rx, uint8_t *ry, uint8_t k1[16], uint8_t k2[16], const char *privkey); #define ECIES_OVERHEAD (8 * NUMWORDS + 8) /* ECIES encryption; the resulting cipher text message will be (len + ECIES_OVERHEAD) bytes long */ -void ECIES_encryption(char *msg, const char *text, int len, - const char *Px, const char *Py); +void ECIES_encryption(char *msg, const char *text, int len, const char *Px, const char *Py); /* ECIES decryption */ -int ECIES_decryption(char *text, const char *msg, int len, - const char *privkey); +int ECIES_decryption(char *text, const char *msg, int len, const char *privkey); #define MACRO(A) do { A; } while(0) #define MIN(a, b) ((a) < (b) ? (a) : (b)) From 8ec395879ba4b036ceee42bd3cacb629e5d89774 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 18:23:56 +0200 Subject: [PATCH 09/41] Fix off-by-one bug. [Found by 9r] --- firmware/applications/final/config.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/applications/final/config.c b/firmware/applications/final/config.c index a0054d7..444c8ea 100644 --- a/firmware/applications/final/config.c +++ b/firmware/applications/final/config.c @@ -47,7 +47,7 @@ void changer(void){ lcdPrint("["); lcdPrint(IntToStr(current_offset/visible_lines,1,0)); lcdPrint("/"); - lcdPrint(IntToStr(numentries/visible_lines,1,0)); + lcdPrint(IntToStr((numentries-1)/visible_lines,1,0)); lcdPrint("]"); lcdNl(); From 18dc658956b00052e1b44e14ad99115772577136 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 18:28:24 +0200 Subject: [PATCH 10/41] inline these helper functions to save space --- firmware/applications/mkfirmware.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/applications/mkfirmware.pl b/firmware/applications/mkfirmware.pl index 2c465b6..5817b67 100755 --- a/firmware/applications/mkfirmware.pl +++ b/firmware/applications/mkfirmware.pl @@ -109,13 +109,13 @@ print "}};"; print ""; print "// Tick & init functions:"; -print qq!void tick_$app(void) {!; +print qq!inline void tick_$app(void) {!; for (sort @ticks){ print qq!\t$_();!; }; print qq!};!; -print qq!void init_$app(void) {!; +print qq!inline void init_$app(void) {!; for (sort @inits){ print qq!\t$_();!; }; From b23caed4549b50038cc7f5e66f7f8e90abb2a547 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 18:37:41 +0200 Subject: [PATCH 11/41] add static to local-only stuff --- firmware/lcd/decoder.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/firmware/lcd/decoder.c b/firmware/lcd/decoder.c index 7073ee4..539b8d2 100644 --- a/firmware/lcd/decoder.c +++ b/firmware/lcd/decoder.c @@ -2,10 +2,10 @@ #include // Local function: Get next nibble. - int ctr=0; // offset for next nibble - int hilo=0; // 0= high nibble next, 1=low nibble next + static int ctr=0; // offset for next nibble + static int hilo=0; // 0= high nibble next, 1=low nibble next const uint8_t * data; - char gnn(){ // Get next nibble + static char gnn(){ // Get next nibble static int byte; int val; if(hilo==1) From 5244d1cda2571438f38bcb4ef3794731da5defc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Kol=C3=A5s?= Date: Wed, 3 Aug 2011 16:50:22 +0100 Subject: [PATCH 12/41] removed 76 bytes from stringin --- firmware/basic/stringin.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/firmware/basic/stringin.c b/firmware/basic/stringin.c index cce6e3f..0e5bfa5 100644 --- a/firmware/basic/stringin.c +++ b/firmware/basic/stringin.c @@ -14,7 +14,7 @@ struct in{ bool done; } s_input; -void inputInit(char p[],char s[], uint8_t l, uint8_t as, uint8_t ae) { +static void inputInit(char p[],char s[], uint8_t l, uint8_t as, uint8_t ae) { //TODO: Check length! s_input.prompt = p; s_input.line = s; @@ -30,7 +30,7 @@ void inputInit(char p[],char s[], uint8_t l, uint8_t as, uint8_t ae) { } -void inputMove() { +static void inputMove() { char *cur = s_input.line+s_input.pos+s_input.dcursor; switch(getInputWaitRepeat()){ case BTN_LEFT: @@ -78,7 +78,7 @@ void inputMove() { } } -void inputDraw() { +static void inputDraw() { char tmp[2]= {0,0}; lcdClear(); DoString(0,0,s_input.prompt); @@ -89,7 +89,7 @@ void inputDraw() { DoString(s_input.dcursor * CHARWIDTH, 40, "-"); } -void inputClean() { +static void inputClean() { for (int x=0;x<=s_input.maxlength;x++) { if (s_input.line[x] == 0) { x--; From c4c67865e8637d21ba8e10be28dc9e48f003b75c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Kol=C3=A5s?= Date: Wed, 3 Aug 2011 16:47:44 +0100 Subject: [PATCH 13/41] simpletime refactor away 28 bytes --- firmware/basic/simpletime.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/firmware/basic/simpletime.c b/firmware/basic/simpletime.c index a43d683..64d96ca 100644 --- a/firmware/basic/simpletime.c +++ b/firmware/basic/simpletime.c @@ -4,9 +4,8 @@ time_t _timet=0; -int _ytab[2][12] = { - { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, - { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } +char _ytab[12] = { + { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; struct tm * mygmtime(register const time_t time) { @@ -14,6 +13,7 @@ struct tm * mygmtime(register const time_t time) { register struct tm *timep = &br_time; register unsigned long dayclock, dayno; int year = EPOCH_YR; + int month_days; dayclock = (unsigned long)time % SECS_DAY; dayno = (unsigned long)time / SECS_DAY; @@ -29,9 +29,10 @@ struct tm * mygmtime(register const time_t time) { timep->tm_year = year - YEAR0; timep->tm_yday = dayno; timep->tm_mon = 0; - while (dayno >= _ytab[LEAPYEAR(year)][timep->tm_mon]) { - dayno -= _ytab[LEAPYEAR(year)][timep->tm_mon]; - timep->tm_mon++; + while (dayno >= (month_days = _ytab[timep->tm_mon] + + (LEAPYEAR (year) && timep->tm_mon==2)?1:0)) { + dayno -= month_days; + timep->tm_mon++; } timep->tm_mday = dayno + 1; timep->tm_isdst = 0; From 81294835ec2cc65aa4dfca95458dc5df2f4de3f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Kol=C3=A5s?= Date: Wed, 3 Aug 2011 16:31:58 +0100 Subject: [PATCH 14/41] save 4 bytes by making internal function static --- firmware/lcd/render.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/lcd/render.c b/firmware/lcd/render.c index 2a2f692..11890e5 100644 --- a/firmware/lcd/render.c +++ b/firmware/lcd/render.c @@ -14,7 +14,7 @@ const struct FONT_DEF * font = NULL; struct EXTFONT efont; -FIL file; /* current font file */ +static FIL file; /* current font file */ /* Exported Functions */ @@ -138,7 +138,7 @@ int _getFontData(int type, int offset){ return 0; }; -int _getIndex(int c){ +static int _getIndex(int c){ #define ERRCHR (font->u8FirstChar+1) /* Does this font provide this character? */ if(cu8FirstChar) From ad0c9931361bda8686e863ed72b4fa64ea17b9ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20Kol=C3=A5s?= Date: Wed, 3 Aug 2011 14:53:04 +0100 Subject: [PATCH 15/41] lcd/render.c: save 56 bytes Introduce a utility function that reads a single byte from the current position in the font file. --- firmware/lcd/render.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/firmware/lcd/render.c b/firmware/lcd/render.c index 11890e5..968b481 100644 --- a/firmware/lcd/render.c +++ b/firmware/lcd/render.c @@ -8,6 +8,7 @@ #include "fonts/smallfonts.h" #include "filesystem/ff.h" +#include "render.h" /* Global Variables */ const struct FONT_DEF * font = NULL; @@ -36,10 +37,17 @@ void setExtFont(const char *fname){ int getFontHeight(void){ if(font) - return font->u8Height; + return font->u8Height; return 8; // XXX: Should be done right. }; +static uint8_t read_byte (void) +{ + UINT readbytes; + uint8_t byte; + f_read(&file, &byte, sizeof(uint8_t), &readbytes); + return byte; +} int _getFontData(int type, int offset){ UINT readbytes; @@ -51,10 +59,10 @@ int _getFontData(int type, int offset){ if(efont.type == FONT_EXTERNAL){ if (type == START_FONT){ - res = f_read(&file, &efont.def.u8Width, sizeof(uint8_t), &readbytes); - res = f_read(&file, &efont.def.u8Height, sizeof(uint8_t), &readbytes); - res = f_read(&file, &efont.def.u8FirstChar, sizeof(uint8_t), &readbytes); - res = f_read(&file, &efont.def.u8LastChar, sizeof(uint8_t), &readbytes); + efont.def.u8Width = read_byte (); + efont.def.u8Height = read_byte (); + efont.def.u8FirstChar = read_byte (); + efont.def.u8LastChar = read_byte (); res = f_read(&file, &extras, sizeof(uint16_t), &readbytes); return 0; }; @@ -72,9 +80,7 @@ int _getFontData(int type, int offset){ return 0; }; if(type == GET_WIDTH || type == GET_DATA){ - uint8_t width; - res = f_read(&file, &width, sizeof(uint8_t), &readbytes); - return width; + return read_byte (); }; if(type == SEEK_DATA){ character=offset; @@ -87,7 +93,7 @@ int _getFontData(int type, int offset){ }; if(type == PEEK_DATA){ uint8_t width; - res = f_read(&file, &width, sizeof(uint8_t), &readbytes); + width = read_byte (); f_lseek(&file,6+ (extras*sizeof(uint16_t))+ ((extras+font->u8LastChar-font->u8FirstChar)*sizeof(uint8_t))+ @@ -240,10 +246,10 @@ int DoChar(int sx, int sy, int c){ UINT res; UINT readbytes; uint8_t testbyte; - res = f_read(&file, &testbyte, sizeof(uint8_t), &readbytes); + testbyte = read_byte (); if(testbyte>>4 ==15){ - res = f_read(&file, &preblank, sizeof(uint8_t), &readbytes); - res = f_read(&file, &postblank, sizeof(uint8_t), &readbytes); + preblank = read_byte (); + postblank = read_byte (); width-=3; width/=height; res = f_read(&file, charBuf, width*height, &readbytes); From 04e0577b558641857bfdcdc4b69466da0652c6fe Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 18:51:58 +0200 Subject: [PATCH 16/41] Remove unneeded/wrong braces --- firmware/basic/simpletime.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/firmware/basic/simpletime.c b/firmware/basic/simpletime.c index 64d96ca..b7988c7 100644 --- a/firmware/basic/simpletime.c +++ b/firmware/basic/simpletime.c @@ -4,9 +4,7 @@ time_t _timet=0; -char _ytab[12] = { - { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } -}; +char _ytab[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; struct tm * mygmtime(register const time_t time) { static struct tm br_time; From c856eb4f99dc6da568f54044e7404bb785ec38eb Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 18:52:56 +0200 Subject: [PATCH 17/41] Remove stuff for old loadable requirements --- firmware/applications/final.c | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/firmware/applications/final.c b/firmware/applications/final.c index bedce8c..cce4e3b 100644 --- a/firmware/applications/final.c +++ b/firmware/applications/final.c @@ -15,25 +15,9 @@ void init_nick(); void fancyNickname(); -#include "lcd/allfonts.h" -void forLoadables(int i){ - if(i){ - lcdSetPixel(0,0); - font=&Font_Invaders; - ECIES_encyptkeygen(0,0,0,0,0,0); - systickGetTicks(); - ECIES_embedded_public_key_validation(); - point_copy(0,0,0,0); - point_is_zero(0,0); - bitstr_parse_export(0,0); - nrf_rcv_pkt_time_encr(); - }; -}; - void main_final(void) { //checkFirstBoot(); init_final(); - forLoadables(0); menuflags|=MENU_TIMEOUT; while(1){ From 2799821dafb5e36ff508114d29459b176b8cdf3a Mon Sep 17 00:00:00 2001 From: schneider Date: Wed, 3 Aug 2011 19:00:26 +0200 Subject: [PATCH 18/41] added receive card l0adable --- firmware/l0dable/EXPORTS | 7 + firmware/l0dable/recvcard.c | 340 ++++++++++++++++++++++++++++++++++++ 2 files changed, 347 insertions(+) create mode 100644 firmware/l0dable/recvcard.c diff --git a/firmware/l0dable/EXPORTS b/firmware/l0dable/EXPORTS index ea15048..ead6d87 100644 --- a/firmware/l0dable/EXPORTS +++ b/firmware/l0dable/EXPORTS @@ -47,3 +47,10 @@ strcpy xxtea_encode_words getRandom crc16 +f_write +f_close +ECIES_decryptkeygen +bitstr_parse_export +f_get_rc_string +xxtea_decode_words +systickGetTicks diff --git a/firmware/l0dable/recvcard.c b/firmware/l0dable/recvcard.c new file mode 100644 index 0000000..d19da8d --- /dev/null +++ b/firmware/l0dable/recvcard.c @@ -0,0 +1,340 @@ +#include +#include +#include +#include +#include +#include "basic/basic.h" +#include "lcd/render.h" +#include "lcd/allfonts.h" +#include "basic/ecc.h" +#include "funk/nrf24l01p.h" +#include "filesystem/ff.h" +#include "filesystem/diskio.h" +#include "funk/filetransfer.h" +#include "lcd/print.h" +#include +#include "funk/nrf24l01p.h" +#include "funk/filetransfer.h" +#include "funk/rftransfer.h" +#include "basic/basic.h" +#include "basic/xxtea.h" +#include "filesystem/ff.h" +#include "lcd/print.h" +#include "usetable.h" + + +uint8_t mac[5] = {1,2,3,2,1}; + +void ram(void) +{ + if( sendKeys() ) + return; + + char priv[42]; + UINT readbytes; + FIL file; + + if( f_open(&file, "priv.key", FA_OPEN_EXISTING|FA_READ) ){ + return; + } + if( f_read(&file, priv, 41, &readbytes) || readbytes != 41 ){ + return; + } + f_close(&file); + priv[41] = 0; + + uint8_t done = 0; + uint8_t key; + uint8_t k1[16], k2[16], rx[4*NUMWORDS], ry[4*NUMWORDS]; + + while( !done ){ + lcdClear(); + lcdPrintln("Receiving file"); + lcdPrintln("Down=Abort"); + lcdRefresh(); + key = getInput(); + delayms(20); + if( key == BTN_DOWN ){ + return -1; + } + if( receiveR(rx,ry) ) + continue; + lcdPrintln("Creating key"); + lcdRefresh(); + ECIES_decryptkeygen(rx, ry, k1, k2, priv); + if( filetransfer_receive(mac,(uint32_t*)k1) < 0 ) + continue; + lcdPrintln("Right=OK"); + lcdPrintln("Left=Retry"); + lcdPrintln("Down=Abort"); + lcdRefresh(); + + while(1){ + key = getInput(); + delayms(20); + if( key == BTN_LEFT ){ + break; + }else if( key == BTN_RIGHT ){ + done = 1; + break; + }else if( key == BTN_DOWN ){ + return -1; + } + } + } +} + +void sendPublicKey(void) +{ + uint8_t exp[2 + 4*NUMWORDS + 2]; + char buf[42]; + UINT readbytes; + FIL file; + + if( f_open(&file, "pubx.key", FA_OPEN_EXISTING|FA_READ) ){ + return; + } + if( f_read(&file, buf, 41, &readbytes) || readbytes != 41 ){ + return; + } + f_close(&file); + buf[41] = 0; + + exp[0] = 'P'; + bitstr_parse_export((char*)exp+2, buf); + exp[1] = 'X'; + nrf_snd_pkt_crc(32, exp); + delayms(10); + + if( f_open(&file, "puby.key", FA_OPEN_EXISTING|FA_READ) ){ + return; + } + if( f_read(&file, buf, 41, &readbytes) || readbytes != 41 ){ + return; + } + f_close(&file); + buf[41] = 0; + + exp[1] = 'Y'; + bitstr_parse_export((char*)exp+2, buf); + nrf_snd_pkt_crc(32, exp); + delayms(10); +} + + +int receiveKey(uint8_t type, uint8_t *x, uint8_t *y) +{ + uint8_t buf[32]; + uint8_t n; + + n = nrf_rcv_pkt_time(1000, 32, buf); + if( n == 32 && buf[0] == type && buf[1] == 'X' ){ + for(int i=0; i MAXSIZE ) {lcdPrintln("too big"); lcdRefresh(); while(1);} + if( size > MAXSIZE ) return 1; //file to big + //if(fileexists(metadata)) return 1; //file already exists + + //lcdPrint("open"); lcdPrintln((const char*)metadata); lcdRefresh(); + res = f_open(&file, (const char*)metadata, FA_OPEN_ALWAYS|FA_WRITE); + + //lcdPrintln("file opened"); lcdRefresh(); + if( res ) {lcdPrintln("res"); lcdPrint(f_get_rc_string(res)); lcdRefresh(); while(1);} + if( res ) + return res; + + uint16_t wordcount = (size+3)/4; + + //nrf_set_rx_mac(0, 32, 5, mac); + //lcdPrintln("get file"); lcdRefresh(); + int fres = rftransfer_receive(buf, wordcount*4, 1000); + if( fres == -1 ){ + lcdPrintln("checksum wrong"); + }else if( fres == -2 ){ + lcdPrintln("timeout"); + }else{ + //lcdPrintln("got file"); + } + lcdRefresh(); + if( fres < 0 ) + return 1; + //nrf_set_rx_mac(0, 32, 5, macbuf); + + xxtea_decode_words((uint32_t *)buf, wordcount, k); + + res = f_write(&file, buf, size, &written); + f_close(&file); + if( res ) + return res; + if( written != size ) + return 1; //error while writing + lcdClear(); + lcdPrintln("Received"); lcdPrintln((const char*)metadata); lcdRefresh(); + + return 0; +} + +#define MAXPACKET 32 +int16_t rftransfer_receive(uint8_t *buffer, uint16_t maxlen, uint16_t timeout) +{ + uint8_t buf[MAXPACKET]; + uint8_t state = 0; + uint16_t pos = 0, seq = 0, size = 0, rand = 0, crc = 0; + int n,i; + unsigned int currentTick = systickGetTicks(); + unsigned int startTick = currentTick; + + while(systickGetTicks() < (startTick+timeout) ){//this fails if either overflows + n = nrf_rcv_pkt_time(1000, MAXPACKET, buf); + switch(state){ + case 0: + if( n == 32 && buf[0] == 'L' ){ + size = (buf[1] << 8) | buf[2]; + rand = (buf[3] << 8) | buf[4]; + seq = 0; + pos = 0; + if( size <= maxlen ){ + //lcdClear(); + //lcdPrint("got l="); lcdPrintInt(size); + //lcdPrintln(""); lcdRefresh(); + state = 1; + } + } + break; + case 1: + if( n == 32 && buf[0] == 'D' && ((buf[3]<<8)|buf[4])==rand ){ + //lcdPrint("got d"); lcdRefresh(); + if( seq == ((buf[1]<<8)|buf[2]) ){ + //lcdPrintln(" in seq"); lcdRefresh(); + for(i=5; i Date: Wed, 3 Aug 2011 19:02:05 +0200 Subject: [PATCH 19/41] fix zoom: long click = in, double click = out --- firmware/loadable/mandelbrot.c | 49 +++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/firmware/loadable/mandelbrot.c b/firmware/loadable/mandelbrot.c index 2d9ae16..f66b691 100644 --- a/firmware/loadable/mandelbrot.c +++ b/firmware/loadable/mandelbrot.c @@ -36,7 +36,8 @@ void ram(void) { struct mb { long rmin, rmax, imin, imax; - bool dirty, dup, ddown, dleft, dright; + bool dirty, dup, ddown, dleft, dright, clickmark; + int count, limitZIn, limitZOut; } mandel; void mandelInit() { @@ -48,12 +49,16 @@ void mandelInit() { mandel.rmax = fixpt(1); mandel.imin = fixpt(-2); mandel.imax = fixpt(2); - + mandel.count = 0; + mandel.limitZIn = 40; + mandel.limitZOut = 30; + mandel.dirty = true; mandel.dup = false; mandel.ddown = false; mandel.dleft = false; mandel.dright = false; + mandel.clickmark = false; } void mandelMove() { @@ -65,7 +70,7 @@ void mandelMove() { char key = getInputRaw(); - if (key == BTN_LEFT) { + if (key == BTN_LEFT) { mandel.imax -=is; mandel.imin -=is; mandel.dleft = true; @@ -77,23 +82,37 @@ void mandelMove() { mandel.rmax += rs; mandel.rmin += rs; mandel.ddown = true; - } else if (key == BTN_UP) { - mandel.rmax -= rs; - mandel.rmin -= rs; - mandel.dup = true; - } else if (key == (BTN_ENTER + BTN_UP)) { - mandel.imin = mandel.imin + (mandel.imax-mandel.imin)/10; - mandel.imax = mandel.imax - (mandel.imax-mandel.imin)/10; - mandel.rmin = mandel.rmin +(mandel.rmax-mandel.rmin)/10; - mandel.rmax = mandel.rmax -(mandel.rmax-mandel.rmin)/10; - mandel.dirty = true; - } else if (key == (BTN_ENTER + BTN_DOWN)) { - mandel.imin = mandel.imin - (mandel.imax-mandel.imin)/10; + } else if (key == BTN_UP) { + mandel.rmax -= rs; + mandel.rmin -= rs; + mandel.dup = true; + } else if (key == BTN_ENTER) { + if (mandel.count < mandel.limitZIn) { + mandel.count = mandel.count + 1; + } + } else if (key == BTN_NONE) { + if(mandel.count > 0 ) { + mandel.count = mandel.count - 1; + mandel.clickmark = true; + } + if (mandel.count == 0 ) { + mandel.clickmark = false; + } + } + if (mandel.count > mandel.limitZOut && mandel.clickmark && key == BTN_ENTER) { + mandel.imin = mandel.imin - (mandel.imax-mandel.imin)/10; mandel.imax = mandel.imax + (mandel.imax-mandel.imin)/10; mandel.rmin = mandel.rmin -(mandel.rmax-mandel.rmin)/10; mandel.rmax = mandel.rmax +(mandel.rmax-mandel.rmin)/10; mandel.dirty = true; } + if (mandel.count == mandel.limitZIn && key == BTN_ENTER) { + mandel.imin = mandel.imin + (mandel.imax-mandel.imin)/10; + mandel.imax = mandel.imax - (mandel.imax-mandel.imin)/10; + mandel.rmin = mandel.rmin +(mandel.rmax-mandel.rmin)/10; + mandel.rmax = mandel.rmax -(mandel.rmax-mandel.rmin)/10; + mandel.dirty = true; + } } void mandelPixel(int x, int y) { From 99ce5e362da89369e41d56e71f6cdd14d30d63fb Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 19:08:44 +0200 Subject: [PATCH 20/41] save space by telling the compiler that lcdRefresh is actually lcdDisplay --- firmware/lcd/display.c | 1 + firmware/lcd/print.c | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/firmware/lcd/display.c b/firmware/lcd/display.c index 5922e8e..d3fb731 100644 --- a/firmware/lcd/display.c +++ b/firmware/lcd/display.c @@ -216,6 +216,7 @@ void lcdDisplay(void) { lcd_deselect(); } +void lcdRefresh() __attribute__ ((weak, alias ("lcdDisplay"))); inline void lcdInvert(void) { GLOBAL(lcdinvert)=!GLOBAL(lcdinvert); diff --git a/firmware/lcd/print.c b/firmware/lcd/print.c index 6f6ac75..46adf84 100644 --- a/firmware/lcd/print.c +++ b/firmware/lcd/print.c @@ -53,9 +53,6 @@ void lcdClear(){ lcdFill(0); }; -void lcdRefresh(){ - lcdDisplay(); -}; void lcdMoveCrsr(signed int dx,signed int dy){ x+=dx; From 77d35aff7abbe59ff5499f799a5ec47c43223af5 Mon Sep 17 00:00:00 2001 From: schneider Date: Wed, 3 Aug 2011 19:09:03 +0200 Subject: [PATCH 21/41] added spaceinvaders l0dable --- firmware/l0dable/EXPORTS | 7 + firmware/l0dable/spaceinvaders.c | 463 +++++++++++++++++++++++++++++++ 2 files changed, 470 insertions(+) create mode 100644 firmware/l0dable/spaceinvaders.c diff --git a/firmware/l0dable/EXPORTS b/firmware/l0dable/EXPORTS index ead6d87..c5384b0 100644 --- a/firmware/l0dable/EXPORTS +++ b/firmware/l0dable/EXPORTS @@ -54,3 +54,10 @@ bitstr_parse_export f_get_rc_string xxtea_decode_words systickGetTicks +lcdFill +memcpy +DoChar +font +Font_Invaders +Font_7x8 +lcdBuffer diff --git a/firmware/l0dable/spaceinvaders.c b/firmware/l0dable/spaceinvaders.c new file mode 100644 index 0000000..0ba30c7 --- /dev/null +++ b/firmware/l0dable/spaceinvaders.c @@ -0,0 +1,463 @@ +#include +#include + +#include "basic/basic.h" +#include "basic/random.h" + +#include "lcd/render.h" +#include "lcd/display.h" +#include "lcd/allfonts.h" + +#include "usetable.h" +/**************************************************************************/ +#define POS_PLAYER_Y 60 +#define POS_PLAYER_X RESX/2-3 +#define POS_UFO_Y 0 +#define ENEMY_ROWS 3 +#define ENEMY_COLUMNS 6 +#define DISABLED 255 + +#define UFO_PROB 1024 + +#define TYPE_PLAYER 1 +#define TYPE_ENEMY_A 3 +#define TYPE_ENEMY_B 2 +#define TYPE_ENEMY_C 4 +#define TYPE_UFO 5 + +#define BUNKERS 3 +#define BUNKER_WIDTH 10 +static const uint8_t BUNKER_X[] = {15, RESX/2-BUNKER_WIDTH/2,RESX-BUNKER_WIDTH-15}; +static const uint8_t ENEMY_WIDTHS[] = {8,10,12}; + +struct gamestate { + char player; + char ufo; + char shot_x, shot_y; + char shots_x[ENEMY_COLUMNS]; + char shots_y[ENEMY_COLUMNS]; + char alive; + int16_t move; + char direction, lastcol; + bool killed; + bool step; + uint32_t score; + uint16_t level; + int8_t rokets; + char enemy_x[ENEMY_ROWS][ENEMY_COLUMNS]; + char enemy_row_y[ENEMY_ROWS]; + uint8_t bunker[BUNKERS][BUNKER_WIDTH]; +} game; +char key; + +void init_game(); +void init_enemy(); +void check_end(); +void move_ufo(); +void move_shot(); +void move_shots(); +void move_player(); +void move_enemy(); +void draw_score(); +void draw_bunker(); +void draw_player(); +void draw_enemy(); +void draw_shots(); +void draw_sprite(char type, char x, char y); +void draw_ufo(); +void screen_intro(); +void screen_gameover(); +void screen_level(); +bool check_bunker(char xpos, char ypos, int8_t shift); + +void ram(void) { + //gpioSetValue (RB_LED1, CFG_LED_OFF); + //backlightInit(); + while(1) { + screen_intro(); + game.rokets = 3; + game.level = 1; + init_game(); + screen_level(); + while (game.rokets>=0) { + ////checkISP(); + lcdFill(0); + check_end(); + move_ufo(); + move_shot(); + move_shots(); + move_player(); + move_enemy(); + draw_score(); + draw_ufo(); + draw_bunker(); + draw_player(); + draw_enemy(); + draw_shots(); + // draw_status(); + lcdDisplay(); + delayms(12); + } + screen_gameover(); + } + return; +} + +void screen_intro() { + char key=0; + while(key==0) { + lcdFill(0); + font = &Font_Invaders; + DoString(28,25,"ABC"); + font = &Font_7x8; + DoString (28,40,"SPACE"); + DoString (18,50,"INVADERS"); + //DoString (20,RESY-24, "Highscore"); + DoString (0, 0, "12345"); + DoString (0, 9, "iggy"); + lcdDisplay(); + + delayms_queue(50); + key=getInput(); + } +} + +void screen_gameover() { + char key =0; + while(key==0) { + lcdFill(0); + font = &Font_7x8; + DoString (12,32, "GAME OVER"); + DoInt (0,0, game.score); + DoString (0,9,"HIGHSCORE!"); + lcdDisplay(); + delayms_queue(50); + key=getInput(); + } +} + +void screen_level() { + lcdFill(0); + draw_score(); + font = &Font_7x8; + int dx = DoString(20,32, "Level "); + DoInt(dx,32,game.level); + lcdDisplay(); + delayms(500); +} + +void init_game(void) { + game.player = POS_PLAYER_X; + game.shot_x = DISABLED; + game.shot_y = 0; + game.alive = ENEMY_ROWS*ENEMY_COLUMNS; + game.move = 0; + if (getRandom()%2 == 0) { + game.direction = -1; + game.lastcol = ENEMY_COLUMNS-1; + } else { + game.direction = 1; + game.lastcol = 0; + } + game.killed = 0; + game.step = false; + game.ufo = DISABLED; + game.score = 0; + init_enemy(); + + for (int col=0; colBUNKER_X[BUNKERS-1-b] && + xposRESY-16) { + int offset = BUNKER_WIDTH - (xpos-BUNKER_X[BUNKERS-1-b]); + if (game.bunker[b][offset]!=0) { + if (shift>0) + game.bunker[b][offset]&=game.bunker[b][offset]<>-shift; + return true; + } + } + } + return false; +} + +void move_shot() { + //No shot, do nothing + if(game.shot_x == DISABLED) { + return; + } + + //moving out of top, end shot + if (game.shot_y <= 0) { + game.shot_x = DISABLED; + return; + } + + if (check_bunker(game.shot_x,game.shot_y-5,1 )) + game.shot_x=DISABLED; + + //check for collision with enemy, kill enemy if + for (int row=0; row= game.shot_y && game.enemy_row_y[row]+6 < game.shot_y+7) { + for(int col = 0; col= game.enemy_x[row][col] && game.shot_x < game.enemy_x[row][col]+ENEMY_WIDTHS[row]) { + game.enemy_x[row][col]=DISABLED; + game.shot_x = DISABLED; + game.alive--; + game.score+=(3-row)*10; + return; + } + } + } + } + + //check for collision with ufo + if (game.ufo != DISABLED && + game.shot_x>game.ufo && + game.shot_x= RESY) { + game.shots_x[col] = DISABLED; + return; + } + //check for collision with bunker + if (check_bunker(game.shots_x[col],game.shots_y[col],-1)) + game.shots_x[col]=DISABLED; + + //check for collision with player + if (game.shots_y[col] >= RESY-13 && + game.shots_x[col] > game.player+1 && + game.shots_x[col] < game.player+6) { + + game.killed = true; + } + + //move shots down + game.shots_y[col] += 1; + } +} + +void move_ufo() { + if (game.ufo == DISABLED) { + if ((getRandom()%UFO_PROB)==0) { + game.ufo = 0; + } + return; + } + if (game.ufo >= RESX){ + game.ufo = DISABLED; + return; + } + game.ufo++; +} + +void move_player() { + if(gpioGetValue(RB_BTN0)==0 && game.player > 0 ){ + game.player-=1; + } + + if(gpioGetValue(RB_BTN1)==0 && game.player < RESX-8){ + game.player+=1; + } + + if(gpioGetValue(RB_BTN4)==0 && game.shot_x == 255){ + game.shot_x = game.player+4; + game.shot_y = POS_PLAYER_Y; + } +} + +void move_enemy() { + if(game.move > 0){ + game.move-=game.level/5+1; + return; + } + + game.step = !game.step; + for (int col = 0; col < ENEMY_COLUMNS; col++) { + for (int row = 0; row < ENEMY_ROWS; row++) { + char pos = game.enemy_x[row][(game.direction==1)?(ENEMY_COLUMNS-(col+1)):col]; + if (pos != DISABLED) { + //Check collision with player + if((game.enemy_row_y[row]+8 >= POS_PLAYER_Y && pos+8 >= game.player && pos < game.player+8) || + game.enemy_row_y[row]+8 >= POS_PLAYER_Y+8) { + for(int row=0; row=RESX-10 && game.direction == 1)){ + game.direction = (game.direction==1)?-1:1; + for (int r = 0; r=23?4:2; + } + return; + } + game.enemy_x[row][(game.direction==1)?(ENEMY_COLUMNS-(col+1)):col] += game.direction; + } + } + } + + game.move = game.alive*2-1; +} + +void draw_player() { + draw_sprite(TYPE_PLAYER, game.player, POS_PLAYER_Y); +} + +void draw_ufo() { + if (game.ufo!=DISABLED) + draw_sprite(TYPE_UFO, game.ufo, POS_UFO_Y); +} + +void draw_enemy() { + for (int row = 0; row Date: Wed, 3 Aug 2011 19:13:20 +0200 Subject: [PATCH 22/41] Fix mktable regexp --- firmware/l0dable/mktable.pl | 4 ++-- firmware/l0dable/system-include-hack.h | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 firmware/l0dable/system-include-hack.h diff --git a/firmware/l0dable/mktable.pl b/firmware/l0dable/mktable.pl index 38cf043..ad54420 100755 --- a/firmware/l0dable/mktable.pl +++ b/firmware/l0dable/mktable.pl @@ -56,11 +56,11 @@ sub wanted { s!//.*!!; $types{$id}="*($_)"; $files{$id}=$File::Find::name; - }elsif (m!^\s*extern\s[^(]* ([\w]+)\s*(\[\w*\]\s*)?;\s*(//.*)?(/\*[^/]*\*/)?$!){ + }elsif (m!^\s*extern\s[^(]* ([\w]+)\s*(\[[^]]*\]\s*)?;\s*(//.*)?(/\*[^/]*\*/)?$!){ $id=$1; s/extern //; my $star="*"; - if( s/\[\w*\]//){ + if( s/\[.*\]//){ $star=""; }; s/$id/*/; diff --git a/firmware/l0dable/system-include-hack.h b/firmware/l0dable/system-include-hack.h new file mode 100644 index 0000000..4f8f217 --- /dev/null +++ b/firmware/l0dable/system-include-hack.h @@ -0,0 +1,3 @@ +size_t strlen(const char *s); +char * strcpy(char * restrict dst, const char * restrict src); +void * memcpy(void *dst, const void *src, size_t len); From ef4f5d13713344b0abce4848e94dd3af05bfc4cd Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 19:19:49 +0200 Subject: [PATCH 23/41] Remove stuff that is now loadable --- firmware/applications/final/mesh.c | 53 ----------- firmware/applications/final/util.c | 141 ----------------------------- 2 files changed, 194 deletions(-) diff --git a/firmware/applications/final/mesh.c b/firmware/applications/final/mesh.c index 84b4784..82ffc73 100644 --- a/firmware/applications/final/mesh.c +++ b/firmware/applications/final/mesh.c @@ -22,59 +22,6 @@ void init_mesh(void){ initMesh(); }; -void m_tset(void){ - _timet=1311961112; -}; - -//# MENU debug MeshInfo -void m_time(void){ - struct tm* tm; - char c[2]={0,0}; - getInputWaitRelease(); - delayms(100); - do{ - lcdClear(); - tm= mygmtime(getSeconds()); - lcdPrint(IntToStr(tm->tm_hour,2,F_LONG)); - lcdPrint(":"); - lcdPrint(IntToStr(tm->tm_min,2,F_LONG|F_ZEROS)); - lcdPrint(":"); - lcdPrint(IntToStr(tm->tm_sec,2,F_LONG|F_ZEROS)); - lcdNl(); - lcdPrint(IntToStr(tm->tm_mday,2,F_LONG)); - lcdPrint("."); - lcdPrint(IntToStr(tm->tm_mon+1,2,0)); - lcdPrint("."); - lcdPrint(IntToStr(tm->tm_year+YEAR0,4,F_LONG|F_ZEROS)); - lcdNl(); - - lcdNl(); - lcdPrint("<"); - - for(int i=0;i"); - - lcdPrint("Gen:"); - lcdPrintInt(meshgen); - lcdNl(); - lcdRefresh(); - delayms_queue(50); - }while ((getInputRaw())==BTN_NONE); -}; - - -inline void blink(char a, char b){ - gpioSetValue (a,b, 1-gpioGetValue(a,b)); -}; - - int choose(char * texts, int8_t menuselection){ uint8_t numentries = 0; uint8_t visible_lines = 0; diff --git a/firmware/applications/final/util.c b/firmware/applications/final/util.c index fb4fbe7..aa99c61 100644 --- a/firmware/applications/final/util.c +++ b/firmware/applications/final/util.c @@ -13,147 +13,6 @@ /**************************************************************************/ -//# MENU debug ChkLight -void ChkLight(void) { - int dx=0; - int dy=8; - dx=DoString(0,dy,"Light:"); - DoString(0,dy+16,"Night:"); - while ((getInputRaw())==BTN_NONE){ - DoInt(dx,dy,GetLight()); - DoInt(dx,dy+16,isNight()); - DoInt(dx,dy+8,GLOBAL(daytrig)); - lcdDisplay(); - delayms_queue(100); - }; - dx=DoString(0,dy+24,"Done."); -} - -//# MENU debug ChkBattery -void ChkBattery(void) { - do{ - lcdClear(); - lcdPrintln("Voltage:"); - lcdPrintln(IntToStr(GetVoltage(),5,0)); - lcdNl(); - lcdPrintln("Chrg_stat:"); - if(gpioGetValue(RB_PWR_CHRG)){ - lcdPrintln("1"); - }else{ - lcdPrintln("0"); - }; - lcdRefresh(); - delayms_queue(100); - } while ((getInputRaw())==BTN_NONE); -} - -//# MENU debug Uptime -void uptime(void) { - int t; - int h; - char flag; - while ((getInputRaw())==BTN_NONE){ - lcdClear(); - lcdPrintln("Uptime:"); - t=getTimer()/(1000/SYSTICKSPEED); - h=t/60/60; - flag=F_ZEROS; - if(h>0){ - lcdPrint(IntToStr(h,2,flag)); - lcdPrint("h"); - flag|=F_LONG; - }; - h=t/60%60; - if(h>0){ - lcdPrint(IntToStr(h,2,flag)); - lcdPrint("m"); - flag|=F_LONG; - }; - h=t%60; - if(h>0){ - lcdPrint(IntToStr(h,2,flag)); - lcdPrint("s"); - }; - lcdNl(); - lcdNl(); - lcdPrintln("Ticks:"); - lcdPrint(IntToStr(_timectr,10,0)); - lcdRefresh(); - delayms_queue(200); - }; - lcdPrintln("done."); -} - -//# MENU debug Uuid -void uuid(void) { - IAP_return_t iap_return; - iap_return = iapReadSerialNumber(); - lcdClear(); - lcdPrintln("UUID:"); - lcdPrintIntHex(iap_return.Result[0]); lcdNl(); - lcdPrintIntHex(iap_return.Result[1]); lcdNl(); - lcdPrintIntHex(iap_return.Result[2]); lcdNl(); - lcdPrintIntHex(iap_return.Result[3]); lcdNl(); - lcdNl(); - lcdPrintln("Beacon ID:"); - lcdPrintln(IntToStrX(GetUUID32(),4)); - lcdRefresh(); -} - -//# MENU debug Qstatus -void Qstatus(void) { - int dx=0; - int dy=8; - lcdClear(); - dx=DoString(0,dy+16,"Qdepth:"); - while ((getInputRaw())!=BTN_ENTER){ - DoInt(dx,dy+16,(the_queue.qend-the_queue.qstart+MAXQENTRIES)%MAXQENTRIES); - lcdDisplay(); - if(getInputRaw()!=BTN_NONE) - work_queue(); - else - delayms(10); - }; - dy+=16; - dx=DoString(0,dy,"Done."); -}; - -void blink_led0(void){ - gpioSetValue (RB_LED0, 1-gpioGetValue(RB_LED0)); -}; - -void tick_alive(void){ - static int foo=0; - - if(GLOBAL(alivechk)==0) - return; - - if(foo++>500/SYSTICKSPEED){ - foo=0; - if(GLOBAL(alivechk)==2) - push_queue(blink_led0); - else - blink_led0(); - }; - return; -}; - -//# MENU debug ShowSP -void getsp(void) { - int dx=0; - int dy=8; - int x; - lcdClear(); - dx=DoString(0,dy,"SP:"); - while ((getInputRaw())==BTN_NONE){ - __asm( "mov %0, sp\n" : "=r" (x) :); - DoIntX(0,dy+8,x); - lcdDisplay(); - delayms_queue(50); - }; - dy+=16; - dx=DoString(0,dy,"Done."); -}; //# MENU msc void msc_menu(void){ From 9f4b1cf7bcecce16f9055b5cb3dad6f49b8f1ef0 Mon Sep 17 00:00:00 2001 From: schneider Date: Wed, 3 Aug 2011 19:24:05 +0200 Subject: [PATCH 24/41] added some stdint includes --- firmware/basic/random.h | 2 +- firmware/basic/xxtea.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/firmware/basic/random.h b/firmware/basic/random.h index 3ff72db..87c640e 100644 --- a/firmware/basic/random.h +++ b/firmware/basic/random.h @@ -1,6 +1,6 @@ #ifndef _RANDOM_H_ #define _RANDOM_H_ - +#include void randomInit(void); uint32_t getRandom(void); diff --git a/firmware/basic/xxtea.h b/firmware/basic/xxtea.h index da93394..1d0ffe0 100644 --- a/firmware/basic/xxtea.h +++ b/firmware/basic/xxtea.h @@ -1,8 +1,8 @@ #ifndef _XXTEA_H_ #define _XXTEA_H_ +#include -void xxtea_cbcmac(uint32_t mac[4], uint32_t *data, - uint32_t len, uint32_t const key[4]); +void xxtea_cbcmac(uint32_t mac[4], uint32_t *data, uint32_t len, uint32_t const key[4]); void xxtea_encode_words(uint32_t *v, int n, uint32_t const k[4]); void xxtea_decode_words(uint32_t *v, int n, uint32_t const k[4]); From 858429cd159807ffdd05da645ba4237765f9da1c Mon Sep 17 00:00:00 2001 From: Kilian Neuner Date: Wed, 3 Aug 2011 20:15:24 +0200 Subject: [PATCH 25/41] fix zoom: doubleclick = out, longclick = in; zoomlimits --- firmware/l0dable/mandel.c | 57 ++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/firmware/l0dable/mandel.c b/firmware/l0dable/mandel.c index d00503a..e47916f 100644 --- a/firmware/l0dable/mandel.c +++ b/firmware/l0dable/mandel.c @@ -5,7 +5,6 @@ #include "lcd/render.h" #include "lcd/display.h" #include "lcd/allfonts.h" - #include "usetable.h" #define FIXSIZE 25 @@ -38,7 +37,8 @@ void ram(void) { struct mb { long rmin, rmax, imin, imax; - bool dirty, dup, ddown, dleft, dright; + bool dirty, dup, ddown, dleft, dright, clickmark; + int presscount, presslimitzin, presslimitzout, zoomlevel, maxzoomin, maxzoomout; } mandel; void mandelInit() { @@ -50,12 +50,19 @@ void mandelInit() { mandel.rmax = fixpt(1); mandel.imin = fixpt(-2); mandel.imax = fixpt(2); - + mandel.presscount = 0; + mandel.presslimitzin = 40; + mandel.presslimitzout = 30; + mandel.zoomlevel = 0; + mandel.maxzoomin = 65; + mandel.maxzoomout = -12; + mandel.dirty = true; mandel.dup = false; mandel.ddown = false; mandel.dleft = false; mandel.dright = false; + mandel.clickmark = false; } void mandelMove() { @@ -67,7 +74,7 @@ void mandelMove() { char key = getInputRaw(); - if (key == BTN_LEFT) { + if (key == BTN_LEFT) { mandel.imax -=is; mandel.imin -=is; mandel.dleft = true; @@ -79,23 +86,41 @@ void mandelMove() { mandel.rmax += rs; mandel.rmin += rs; mandel.ddown = true; - } else if (key == BTN_UP) { - mandel.rmax -= rs; - mandel.rmin -= rs; - mandel.dup = true; - } else if (key == (BTN_ENTER + BTN_UP)) { + } else if (key == BTN_UP) { + mandel.rmax -= rs; + mandel.rmin -= rs; + mandel.dup = true; + } else if (key == BTN_ENTER) { + if (mandel.presscount < mandel.presslimitzin) { + mandel.presscount = mandel.presscount + 1; + } + } else if (key == BTN_NONE) { + if(mandel.presscount > 0 ) { + mandel.presscount = mandel.presscount - 1; + mandel.clickmark = true; + } + if (mandel.presscount == 0 ) { + mandel.clickmark = false; + } + } + if (mandel.presscount > mandel.presslimitzout && mandel.clickmark && key == BTN_ENTER && mandel.zoomlevel >= mandel.maxzoomout) { + mandel.imin = mandel.imin - (mandel.imax-mandel.imin)/10; + mandel.imax = mandel.imax + (mandel.imax-mandel.imin)/10; + mandel.rmin = mandel.rmin -(mandel.rmax-mandel.rmin)/10; + mandel.rmax = mandel.rmax +(mandel.rmax-mandel.rmin)/10; + mandel.dirty = true; + delayms(10); + mandel.zoomlevel = mandel.zoomlevel - 1 ; + } + else if (mandel.presscount == mandel.presslimitzin && key == BTN_ENTER && mandel.zoomlevel <= mandel.maxzoomin ) { mandel.imin = mandel.imin + (mandel.imax-mandel.imin)/10; mandel.imax = mandel.imax - (mandel.imax-mandel.imin)/10; mandel.rmin = mandel.rmin +(mandel.rmax-mandel.rmin)/10; mandel.rmax = mandel.rmax -(mandel.rmax-mandel.rmin)/10; mandel.dirty = true; - } else if (key == (BTN_ENTER + BTN_DOWN)) { - mandel.imin = mandel.imin - (mandel.imax-mandel.imin)/10; - mandel.imax = mandel.imax + (mandel.imax-mandel.imin)/10; - mandel.rmin = mandel.rmin -(mandel.rmax-mandel.rmin)/10; - mandel.rmax = mandel.rmax +(mandel.rmax-mandel.rmin)/10; - mandel.dirty = true; - } + delayms(10); + mandel.zoomlevel = mandel.zoomlevel + 1 ; + } } void mandelPixel(int x, int y) { From dd4666b3d71bcbd80665d096beaff2d246bec181 Mon Sep 17 00:00:00 2001 From: iggy Date: Wed, 3 Aug 2011 21:18:11 +0200 Subject: [PATCH 26/41] scoring bug in invaders --- firmware/l0dable/spaceinvaders.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/l0dable/spaceinvaders.c b/firmware/l0dable/spaceinvaders.c index 0ba30c7..a7f62ec 100644 --- a/firmware/l0dable/spaceinvaders.c +++ b/firmware/l0dable/spaceinvaders.c @@ -77,6 +77,7 @@ void ram(void) { screen_intro(); game.rokets = 3; game.level = 1; + game.score = 0; init_game(); screen_level(); while (game.rokets>=0) { @@ -162,7 +163,6 @@ void init_game(void) { game.killed = 0; game.step = false; game.ufo = DISABLED; - game.score = 0; init_enemy(); for (int col=0; col Date: Wed, 3 Aug 2011 21:56:41 +0200 Subject: [PATCH 27/41] Enhance serial code --- firmware/SERIAL_DOKU | 2 ++ firmware/applications/serial/serial.c | 29 ++++++++++++++++++++------- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/firmware/SERIAL_DOKU b/firmware/SERIAL_DOKU index 4c28ee7..d67b459 100644 --- a/firmware/SERIAL_DOKU +++ b/firmware/SERIAL_DOKU @@ -4,6 +4,8 @@ Ck: set encryptionkey Ct: set txmac Cc: set channel Ct: set rxmac(0) +Cl: set rxlens +Ce: sec encrpytion on/off s : send packet s+ : send packet 10 times diff --git a/firmware/applications/serial/serial.c b/firmware/applications/serial/serial.c index f27dada..287001a 100644 --- a/firmware/applications/serial/serial.c +++ b/firmware/applications/serial/serial.c @@ -29,10 +29,9 @@ uint32_t const beaconkey[4] = { uint32_t remotekey[4] = { 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff }; -uint32_t meshkey[4] = { - 0x0, 0x0, 0x0, 0x0 -}; +extern uint32_t const meshkey[4]; +char funkencrypt=0; #if CFG_USBMSC #error "MSC is defined" @@ -151,6 +150,7 @@ int process(char * input){ config.nrmacs=1; nrf_config_set(&config); memcpy(thekey,meshkey,sizeof(thekey)); + funkencrypt=1; }else if(input[1]=='r'){ config.channel=REMOTE_CHANNEL; memcpy(config.txmac,REMOTE_MAC,5); @@ -159,6 +159,7 @@ int process(char * input){ config.nrmacs=1; nrf_config_set(&config); memcpy(thekey,remotekey,sizeof(thekey)); + funkencrypt=1; }else if(input[1]=='b'){ config.channel=BEACON_CHANNEL; memcpy(config.txmac,BEACON_MAC,5); @@ -167,6 +168,7 @@ int process(char * input){ config.nrmacs=1; nrf_config_set(&config); memcpy(thekey,beaconkey,sizeof(thekey)); + funkencrypt=1; }else if(input[1]=='?'){ nrf_config_get(&config); puts_plus("Ch: ");puts_plus(IntToStrX( config.channel,2 )); puts_plus("\r\n"); @@ -202,6 +204,10 @@ int process(char * input){ puts_plus(" "); }; puts_plus("\r\n"); + + puts_plus("encrypt : "); + puts_plus(IntToStrX( funkencrypt,2 )); + puts_plus("\r\n"); }; }else if(input[0]=='C'){ int len; @@ -228,6 +234,15 @@ int process(char * input){ }else if(input[1]=='c'){ config.channel=*hex; nrf_config_set(&config); + }else if(input[1]=='l'){ + config.maclen[0]=uint8ptouint32(hex); + config.maclen[1]=uint8ptouint32(hex+4); + config.maclen[2]=uint8ptouint32(hex+8); + config.maclen[3]=uint8ptouint32(hex+12); + config.maclen[4]=uint8ptouint32(hex+16); + nrf_config_set(&config); + }else if(input[1]=='e'){ + funkencrypt= uint8ptouint32(hex); }; }else if (input[0]=='s'){ __attribute__ ((aligned (4))) uint8_t buf[32]; @@ -251,7 +266,7 @@ int process(char * input){ len+=2; // Add crc! memcpy(buf,hex,len); - status=nrf_snd_pkt_crc_encr(len,buf,thekey); + status=nrf_snd_pkt_crc_encr(len,buf,funkencrypt?thekey:NULL); puts_plus("P "); puts_plus("[");puts_plus(IntToStrX(len,2));puts_plus("] "); @@ -266,7 +281,7 @@ int process(char * input){ while(--ctr>0){ delayms(23); memcpy(buf,hex,len); - status=nrf_snd_pkt_crc_encr(len,buf,thekey); + status=nrf_snd_pkt_crc_encr(len,buf,funkencrypt?thekey:NULL); }; }else if (input[1]=='t'){ static int ctr=1; @@ -283,7 +298,7 @@ int process(char * input){ buf[12]=0xff; // salt (0xffff always?) buf[13]=0xff; - status=nrf_snd_pkt_crc_encr(16,buf,thekey); + status=nrf_snd_pkt_crc_encr(16,buf,funkencrypt?thekey:NULL); }else{ }; puts_plus("S state="); @@ -310,7 +325,7 @@ int process(char * input){ puts_plus("D receive ...\r\n"); nrf_rcv_pkt_start(); do{ - len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,thekey); + len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,funkencrypt?thekey:NULL); // Receive if(len==0||len==-1||len==-2){ //No pkt, Pkt2large, NoPktError delayms(10); From 0c7afe0eb1707691e5acaeae1d0fdb5c5422276e Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 21:57:09 +0200 Subject: [PATCH 28/41] Disable unencrypted receive by default. also add meshkey --- firmware/funk/mesh.c | 2 +- firmware/funk/nrf24l01p.c | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/firmware/funk/mesh.c b/firmware/funk/mesh.c index e69a798..da30c60 100644 --- a/firmware/funk/mesh.c +++ b/firmware/funk/mesh.c @@ -12,7 +12,7 @@ char meshincctr=0; // Generation MPKT meshbuffer[MESHBUFSIZE]; uint32_t const meshkey[4] = { - 0x00000000, 0x00000000, 0x00000000, 0x00000000 + 0x00000042, 0x000005ec, 0x00000023, 0x00000005 }; struct NRF_CFG oldconfig; diff --git a/firmware/funk/nrf24l01p.c b/firmware/funk/nrf24l01p.c index 8c3c464..383df03 100644 --- a/firmware/funk/nrf24l01p.c +++ b/firmware/funk/nrf24l01p.c @@ -153,13 +153,12 @@ int nrf_rcv_pkt_poll_dec(int maxsize, uint8_t * pkt, uint32_t const key[4]){ return len; cmpcrc=crc16(pkt,len-2); - if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) { + if(key!=NULL) xxtea_decode_words((uint32_t*)pkt,len/4,key); - cmpcrc=crc16(pkt,len-2); - if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) { - return -3; // CRC failed - }; + cmpcrc=crc16(pkt,len-2); + if(cmpcrc != (pkt[len-2] <<8 | pkt[len-1])) { + return -3; // CRC failed }; return len; }; From 2cb887025860ec8a1ea0e642316f4456e6611e8e Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 21:58:11 +0200 Subject: [PATCH 29/41] Invaders renamed for 8.3 --- firmware/l0dable/{spaceinvaders.c => invaders.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename firmware/l0dable/{spaceinvaders.c => invaders.c} (100%) diff --git a/firmware/l0dable/spaceinvaders.c b/firmware/l0dable/invaders.c similarity index 100% rename from firmware/l0dable/spaceinvaders.c rename to firmware/l0dable/invaders.c From ca86558f3a09ed24b56798c34ce99ce1e9bae344 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 21:58:39 +0200 Subject: [PATCH 30/41] better make clean --- firmware/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/firmware/Makefile b/firmware/Makefile index 732c824..349b4ff 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -73,6 +73,8 @@ clean: for dir in $(SUBDIRS); do \ $(MAKE) $(CONFIG_MAKE_PRINTDIRECTORY) -C $$dir clean; \ done + $(MAKE) $(CONFIG_MAKE_PRINTDIRECTORY) -C l0dable clean + flash: all ../tools/bootloader/autoflash From 44802d4b281a65d242c1cd3b70f5eca27d4ad078 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 21:59:47 +0200 Subject: [PATCH 31/41] More ram size for loadables --- firmware/Makefile.inc | 2 +- firmware/filesystem/execute.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/Makefile.inc b/firmware/Makefile.inc index 855d011..69097c0 100644 --- a/firmware/Makefile.inc +++ b/firmware/Makefile.inc @@ -7,7 +7,7 @@ TARGET = LPC13xx FLASH = 32K SRAM = 8K -RAMCODE=2048 +RAMCODE=2548 # For USB HID support the LPC134x reserves 384 bytes from the sram, # if you don't want to use the USB features, just use 0 here. diff --git a/firmware/filesystem/execute.c b/firmware/filesystem/execute.c index 9b0aa91..3ae2a86 100644 --- a/firmware/filesystem/execute.c +++ b/firmware/filesystem/execute.c @@ -30,7 +30,7 @@ void execute_file (const char * fname, uint8_t checksignature, uint8_t decode){ dst=(void (*)(void)) (sram_top); lcdPrint("T:"); lcdPrintIntHex(dst); lcdNl(); */ - dst=(void (*)(void)) 0x10001800; + dst=(void (*)(void)) 0x1000160C; res=f_open(&file, fname, FA_OPEN_EXISTING|FA_READ); From 69a58edf4857e17e689efcc901ded1379be4bc63 Mon Sep 17 00:00:00 2001 From: iggy Date: Wed, 3 Aug 2011 22:01:30 +0200 Subject: [PATCH 32/41] invaders mesh highscore preparation --- firmware/l0dable/spaceinvaders.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/firmware/l0dable/spaceinvaders.c b/firmware/l0dable/spaceinvaders.c index a7f62ec..9314c72 100644 --- a/firmware/l0dable/spaceinvaders.c +++ b/firmware/l0dable/spaceinvaders.c @@ -105,6 +105,8 @@ void ram(void) { } void screen_intro() { + uint32_t highscore; + char[20] highnick; char key=0; while(key==0) { lcdFill(0); @@ -113,9 +115,10 @@ void screen_intro() { font = &Font_7x8; DoString (28,40,"SPACE"); DoString (18,50,"INVADERS"); - //DoString (20,RESY-24, "Highscore"); - DoString (0, 0, "12345"); - DoString (0, 9, "iggy"); + + highscore = highscore_get(highnick); + DoInt(0, 0, highscore); + DoString (0, 9, highnick); lcdDisplay(); delayms_queue(50); @@ -130,7 +133,8 @@ void screen_gameover() { font = &Font_7x8; DoString (12,32, "GAME OVER"); DoInt (0,0, game.score); - DoString (0,9,"HIGHSCORE!"); + if (highscore_set(game.score, GLOBAL(nickname))) + DoString (0,9,"HIGHSCORE!"); lcdDisplay(); delayms_queue(50); key=getInput(); @@ -147,6 +151,12 @@ void screen_level() { delayms(500); } +bool highscore_set(uint32_t score, char nick[]) { +} + +uint32_t highscore_get(char nick[]){ +} + void init_game(void) { game.player = POS_PLAYER_X; game.shot_x = DISABLED; From 03a10013440f5189acfb21c0266874d54528b468 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 22:01:48 +0200 Subject: [PATCH 33/41] uuid is 32bit --- firmware/l0dable/debug.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/l0dable/debug.c b/firmware/l0dable/debug.c index 0ba5381..4686da0 100644 --- a/firmware/l0dable/debug.c +++ b/firmware/l0dable/debug.c @@ -124,7 +124,7 @@ void uuid(void) { lcdPrintIntHex(iap_return.Result[3]); lcdNl(); lcdNl(); lcdPrintln("Beacon ID:"); - lcdPrintln(IntToStrX(GetUUID32(),4)); + lcdPrintln(IntToStrX(GetUUID32(),8)); lcdRefresh(); } From 5256464953dcc4d1465499ebae2221b048038919 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 22:20:20 +0200 Subject: [PATCH 34/41] Revert "Remove unneeded/wrong braces" This reverts commit 04e0577b558641857bfdcdc4b69466da0652c6fe. --- firmware/basic/simpletime.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/firmware/basic/simpletime.c b/firmware/basic/simpletime.c index b7988c7..64d96ca 100644 --- a/firmware/basic/simpletime.c +++ b/firmware/basic/simpletime.c @@ -4,7 +4,9 @@ time_t _timet=0; -char _ytab[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; +char _ytab[12] = { + { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } +}; struct tm * mygmtime(register const time_t time) { static struct tm br_time; From 35c73e626609e4b3037e04e46ba7ffb9e8ecc8b3 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 22:20:44 +0200 Subject: [PATCH 35/41] Revert "simpletime refactor away 28 bytes" This reverts commit c4c67865e8637d21ba8e10be28dc9e48f003b75c. --- firmware/basic/simpletime.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/firmware/basic/simpletime.c b/firmware/basic/simpletime.c index 64d96ca..a43d683 100644 --- a/firmware/basic/simpletime.c +++ b/firmware/basic/simpletime.c @@ -4,8 +4,9 @@ time_t _timet=0; -char _ytab[12] = { - { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } +int _ytab[2][12] = { + { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, + { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; struct tm * mygmtime(register const time_t time) { @@ -13,7 +14,6 @@ struct tm * mygmtime(register const time_t time) { register struct tm *timep = &br_time; register unsigned long dayclock, dayno; int year = EPOCH_YR; - int month_days; dayclock = (unsigned long)time % SECS_DAY; dayno = (unsigned long)time / SECS_DAY; @@ -29,10 +29,9 @@ struct tm * mygmtime(register const time_t time) { timep->tm_year = year - YEAR0; timep->tm_yday = dayno; timep->tm_mon = 0; - while (dayno >= (month_days = _ytab[timep->tm_mon] + - (LEAPYEAR (year) && timep->tm_mon==2)?1:0)) { - dayno -= month_days; - timep->tm_mon++; + while (dayno >= _ytab[LEAPYEAR(year)][timep->tm_mon]) { + dayno -= _ytab[LEAPYEAR(year)][timep->tm_mon]; + timep->tm_mon++; } timep->tm_mday = dayno + 1; timep->tm_isdst = 0; From a5f8a9b04faa6df17d4fe235b6010be283b305be Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 22:31:46 +0200 Subject: [PATCH 36/41] Revert "lcd/render.c: save 56 bytes" This reverts commit ad0c9931361bda8686e863ed72b4fa64ea17b9ad. --- firmware/lcd/render.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/firmware/lcd/render.c b/firmware/lcd/render.c index 968b481..11890e5 100644 --- a/firmware/lcd/render.c +++ b/firmware/lcd/render.c @@ -8,7 +8,6 @@ #include "fonts/smallfonts.h" #include "filesystem/ff.h" -#include "render.h" /* Global Variables */ const struct FONT_DEF * font = NULL; @@ -37,17 +36,10 @@ void setExtFont(const char *fname){ int getFontHeight(void){ if(font) - return font->u8Height; + return font->u8Height; return 8; // XXX: Should be done right. }; -static uint8_t read_byte (void) -{ - UINT readbytes; - uint8_t byte; - f_read(&file, &byte, sizeof(uint8_t), &readbytes); - return byte; -} int _getFontData(int type, int offset){ UINT readbytes; @@ -59,10 +51,10 @@ int _getFontData(int type, int offset){ if(efont.type == FONT_EXTERNAL){ if (type == START_FONT){ - efont.def.u8Width = read_byte (); - efont.def.u8Height = read_byte (); - efont.def.u8FirstChar = read_byte (); - efont.def.u8LastChar = read_byte (); + res = f_read(&file, &efont.def.u8Width, sizeof(uint8_t), &readbytes); + res = f_read(&file, &efont.def.u8Height, sizeof(uint8_t), &readbytes); + res = f_read(&file, &efont.def.u8FirstChar, sizeof(uint8_t), &readbytes); + res = f_read(&file, &efont.def.u8LastChar, sizeof(uint8_t), &readbytes); res = f_read(&file, &extras, sizeof(uint16_t), &readbytes); return 0; }; @@ -80,7 +72,9 @@ int _getFontData(int type, int offset){ return 0; }; if(type == GET_WIDTH || type == GET_DATA){ - return read_byte (); + uint8_t width; + res = f_read(&file, &width, sizeof(uint8_t), &readbytes); + return width; }; if(type == SEEK_DATA){ character=offset; @@ -93,7 +87,7 @@ int _getFontData(int type, int offset){ }; if(type == PEEK_DATA){ uint8_t width; - width = read_byte (); + res = f_read(&file, &width, sizeof(uint8_t), &readbytes); f_lseek(&file,6+ (extras*sizeof(uint16_t))+ ((extras+font->u8LastChar-font->u8FirstChar)*sizeof(uint8_t))+ @@ -246,10 +240,10 @@ int DoChar(int sx, int sy, int c){ UINT res; UINT readbytes; uint8_t testbyte; - testbyte = read_byte (); + res = f_read(&file, &testbyte, sizeof(uint8_t), &readbytes); if(testbyte>>4 ==15){ - preblank = read_byte (); - postblank = read_byte (); + res = f_read(&file, &preblank, sizeof(uint8_t), &readbytes); + res = f_read(&file, &postblank, sizeof(uint8_t), &readbytes); width-=3; width/=height; res = f_read(&file, charBuf, width*height, &readbytes); From 8875fc8d8b7c92254aa1182ce21fbfa7d6179225 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 22:33:14 +0200 Subject: [PATCH 37/41] Revert "save 4 bytes by making internal function static" This reverts commit 81294835ec2cc65aa4dfca95458dc5df2f4de3f0. --- firmware/lcd/render.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/firmware/lcd/render.c b/firmware/lcd/render.c index 11890e5..2a2f692 100644 --- a/firmware/lcd/render.c +++ b/firmware/lcd/render.c @@ -14,7 +14,7 @@ const struct FONT_DEF * font = NULL; struct EXTFONT efont; -static FIL file; /* current font file */ +FIL file; /* current font file */ /* Exported Functions */ @@ -138,7 +138,7 @@ int _getFontData(int type, int offset){ return 0; }; -static int _getIndex(int c){ +int _getIndex(int c){ #define ERRCHR (font->u8FirstChar+1) /* Does this font provide this character? */ if(cu8FirstChar) From 3a5415c0a619d58b65bdafb9a30b57b1f2d023cb Mon Sep 17 00:00:00 2001 From: iggy Date: Wed, 3 Aug 2011 22:34:55 +0200 Subject: [PATCH 38/41] invaders bugfixes --- firmware/l0dable/invaders.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/firmware/l0dable/invaders.c b/firmware/l0dable/invaders.c index 9314c72..cc86cd5 100644 --- a/firmware/l0dable/invaders.c +++ b/firmware/l0dable/invaders.c @@ -2,6 +2,7 @@ #include #include "basic/basic.h" +#include "basic/config.h" #include "basic/random.h" #include "lcd/render.h" @@ -49,6 +50,8 @@ struct gamestate { uint8_t bunker[BUNKERS][BUNKER_WIDTH]; } game; char key; +bool highscore_set(uint32_t score, char nick[]); +uint32_t highscore_get(char nick[]); void init_game(); void init_enemy(); @@ -106,7 +109,7 @@ void ram(void) { void screen_intro() { uint32_t highscore; - char[20] highnick; + char highnick[20]; char key=0; while(key==0) { lcdFill(0); @@ -152,9 +155,11 @@ void screen_level() { } bool highscore_set(uint32_t score, char nick[]) { + return true; } uint32_t highscore_get(char nick[]){ + return 0; } void init_game(void) { From 2a18d27530ca150bfb7569966406d5fc3e5d3098 Mon Sep 17 00:00:00 2001 From: kiu Date: Wed, 3 Aug 2011 22:54:42 +0200 Subject: [PATCH 39/41] fixed stupid casting issues for overfloew detection --- firmware/applications/final/flame.c | 4 ++-- firmware/applications/flame.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/firmware/applications/final/flame.c b/firmware/applications/final/flame.c index 3943458..a9ce06a 100644 --- a/firmware/applications/final/flame.c +++ b/firmware/applications/final/flame.c @@ -94,7 +94,7 @@ void tick_flame(void) { // every 10ms } if (flameMode == FLAME_UP) { - if (flameI2Cpwm + flameSpeedUp > flameI2Cpwm ) { + if (0xFF - flameI2Cpwm >= flameSpeedUp ) { flameI2Cpwm += flameSpeedUp; } else { flameI2Cpwm = 0xFF; @@ -113,7 +113,7 @@ void tick_flame(void) { // every 10ms } if (flameMode == FLAME_DOWN) { - if (flameI2Cpwm - flameSpeedDown < flameI2Cpwm ) { + if (flameSpeedDown <= flameI2Cpwm) { flameI2Cpwm -= flameSpeedDown; } else { flameI2Cpwm = 0x00; diff --git a/firmware/applications/flame.c b/firmware/applications/flame.c index 2831505..80bd060 100644 --- a/firmware/applications/flame.c +++ b/firmware/applications/flame.c @@ -84,7 +84,7 @@ void tick_flame(void) { // every 10ms } if (flameMode == FLAME_UP) { - if (flameI2Cpwm + flameSpeedUp > flameI2Cpwm ) { + if (0xFF - flameI2Cpwm >= flameSpeedUp) { flameI2Cpwm += flameSpeedUp; } else { flameI2Cpwm = 0xFF; @@ -103,7 +103,7 @@ void tick_flame(void) { // every 10ms } if (flameMode == FLAME_DOWN) { - if (flameI2Cpwm - flameSpeedDown < flameI2Cpwm ) { + if (flameSpeedDown <= flameI2Cpwm) { flameI2Cpwm -= flameSpeedDown; } else { flameI2Cpwm = 0x00; From 8b9363542580898cee22d16153c630f3a445e288 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 23:12:43 +0200 Subject: [PATCH 40/41] bugfix --- firmware/funk/mesh.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/firmware/funk/mesh.c b/firmware/funk/mesh.c index da30c60..e15656d 100644 --- a/firmware/funk/mesh.c +++ b/firmware/funk/mesh.c @@ -8,7 +8,7 @@ #include "basic/random.h" char meshgen=0; // Generation -char meshincctr=0; // Generation +char meshincctr=0; MPKT meshbuffer[MESHBUFSIZE]; uint32_t const meshkey[4] = { @@ -45,6 +45,8 @@ MPKT * meshGetMessage(uint8_t type){ if(meshbuffer[free].flags==MF_FREE){ memset(&meshbuffer[free],0,sizeof(MPKT)); MO_TYPE_set(meshbuffer[free].pkt,type); + MO_GEN_set(meshbuffer[free].pkt,meshgen); + meshbuffer[free].flags=MF_USED; }; return &meshbuffer[free]; }; From fe500650e99b3810b44b11f2146a459adbfcb0ee Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Wed, 3 Aug 2011 23:13:44 +0200 Subject: [PATCH 41/41] invaders now do meshscore --- firmware/l0dable/EXPORTS | 4 ++++ firmware/l0dable/invaders.c | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/firmware/l0dable/EXPORTS b/firmware/l0dable/EXPORTS index c5384b0..c25d631 100644 --- a/firmware/l0dable/EXPORTS +++ b/firmware/l0dable/EXPORTS @@ -61,3 +61,7 @@ font Font_Invaders Font_7x8 lcdBuffer +meshGetMessage +nickname +uint32touint8p +uint8ptouint32 diff --git a/firmware/l0dable/invaders.c b/firmware/l0dable/invaders.c index cc86cd5..8fef54c 100644 --- a/firmware/l0dable/invaders.c +++ b/firmware/l0dable/invaders.c @@ -9,6 +9,8 @@ #include "lcd/display.h" #include "lcd/allfonts.h" +#include "funk/mesh.h" + #include "usetable.h" /**************************************************************************/ #define POS_PLAYER_Y 60 @@ -155,11 +157,22 @@ void screen_level() { } bool highscore_set(uint32_t score, char nick[]) { + MPKT * mpkt= meshGetMessage('i'); + if(MO_TIME(mpkt->pkt)>score) + return false; + + MO_TIME_set(mpkt->pkt,score); + strcpy((char*)MO_BODY(mpkt->pkt),nick); + return true; } uint32_t highscore_get(char nick[]){ - return 0; + MPKT * mpkt= meshGetMessage('i'); + + strcpy(nick,(char*)MO_BODY(mpkt->pkt)); + + return MO_TIME(mpkt->pkt); } void init_game(void) {