From 2799821dafb5e36ff508114d29459b176b8cdf3a Mon Sep 17 00:00:00 2001 From: schneider Date: Wed, 3 Aug 2011 19:00:26 +0200 Subject: [PATCH 1/5] 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:08:44 +0200 Subject: [PATCH 2/5] 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 3/5] 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 4/5] 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 9f4b1cf7bcecce16f9055b5cb3dad6f49b8f1ef0 Mon Sep 17 00:00:00 2001 From: schneider Date: Wed, 3 Aug 2011 19:24:05 +0200 Subject: [PATCH 5/5] 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]);