Merge branch 'master' of github.com:r0ket/r0ket

This commit is contained in:
schneider 2011-07-31 20:35:36 +02:00
commit c95eb0844f
25 changed files with 1010 additions and 280 deletions

14
firmware/SERIAL_DOKU Normal file
View File

@ -0,0 +1,14 @@
i: initialize
cb / cm / cr: config funk for beacon/mesh/remote
Ck<hex>: set encryptionkey
Ct<hex>: set txmac
Cc<hex>: set channel
Ct<hex>: set rxmac(0)
s <hex>: send packet
s+ <hex>: send packet 10 times
sd <hex>: send packet with debugoutput
r: recv packets
r+<hex>: recv packets for <hex> seconds
r-<hex>: recv <hex> packets (5 seconds max)

View File

@ -38,10 +38,17 @@ void tick_default(void) {
}; };
}; };
if(isNight()) if(ctr>100/SYSTICKSPEED){
backlightSetBrightness(globalconfig.backlightvalue); if(isNight()){
else backlightSetBrightness(GLOBAL(lcdbacklight));
if(GLOBAL(nightinvert))
lcdSetInvert(0);
} else {
backlightSetBrightness(0); backlightSetBrightness(0);
if(GLOBAL(nightinvert))
lcdSetInvert(1);
}
}
if(ctr%(50/SYSTICKSPEED)==0){ if(ctr%(50/SYSTICKSPEED)==0){

View File

@ -0,0 +1,29 @@
#include <sysinit.h>
#include "basic/basic.h"
#include "lcd/render.h"
#include "lcd/display.h"
#include "lcd/allfonts.h"
#define CHARWIDTH 12
#define CHARSPACE 0x20
void main_inputtest(void) {
backlightInit();
char nick[20];
memset(nick,0,20);
strcpy(nick,"iggy");
while(1) {
input("Nick?", nick, 0x20, 0x7f, 20);
lcdFill(0);
font=&Font_Orbitron14pt;
DoString(20,20,nick);
lcdDisplay();
while(getInput() != BTN_ENTER){}
}
}

View File

@ -23,16 +23,102 @@ void applycfg(void){
applyConfig(); applyConfig();
}; };
void show(void){ void changer(void){
lcdClear(); uint8_t numentries = 0;
lcdPrint("time:"); lcdPrintInt(globalconfig.time); lcdNl(); signed char menuselection = 0;
lcdPrint("btrig:"); lcdPrintInt(globalconfig.backlighttrigger); lcdNl(); uint8_t visible_lines = 0;
lcdPrint("bval:"); lcdPrintInt(globalconfig.backlightvalue); lcdNl(); uint8_t current_offset = 0;
lcdPrint("lcd:"); lcdPrintInt(globalconfig.lcdstate); lcdNl();
lcdPrint("priv:"); lcdPrintInt(globalconfig.privacy); lcdNl(); for (int i=0;the_config[i].name!=NULL;i++){
lcdRefresh(); numentries++;
}; };
void lcdmirror(void){ visible_lines = ((RESY/getFontHeight())-1)/2;
lcdToggleFlag(LCD_MIRRORX);
while (1) {
// Display current menu page
lcdClear();
lcdPrint("Config");
lcdSetCrsrX(60);
lcdPrint("[");
lcdPrint(IntToStr(current_offset/visible_lines,1,0));
lcdPrint("/");
lcdPrint(IntToStr(numentries/visible_lines,1,0));
lcdPrint("]");
lcdNl();
lcdNl();
for (uint8_t i = current_offset; i < (visible_lines + current_offset) && i < numentries; i++) {
if(i==0){
lcdPrintln("Save changes:");
if (i == menuselection)
lcdPrint("*");
lcdSetCrsrX(14);
if (i == menuselection)
lcdPrintln("YES");
else
lcdPrintln("no");
}else{
lcdPrintln(the_config[i].name);
if (i == menuselection)
lcdPrint("*");
lcdSetCrsrX(14);
lcdPrint("<");
lcdPrint(IntToStr(the_config[i].value,3,F_LONG));
lcdPrintln(">");
}; };
}
lcdRefresh();
switch (getInputWait()) {
case BTN_UP:
menuselection--;
if (menuselection < current_offset) {
if (menuselection < 0) {
menuselection = numentries-1;
current_offset = ((numentries-1)/visible_lines) * visible_lines;
} else {
current_offset -= visible_lines;
}
}
break;
case BTN_DOWN:
menuselection++;
if (menuselection > (current_offset + visible_lines-1) || menuselection >= numentries) {
if (menuselection >= numentries) {
menuselection = 0;
current_offset = 0;
} else {
current_offset += visible_lines;
}
}
break;
case BTN_LEFT:
if(the_config[menuselection].value >
the_config[menuselection].min)
the_config[menuselection].value--;
if(the_config[menuselection].value > the_config[menuselection].max)
the_config[menuselection].value=
the_config[menuselection].max;
applyConfig();
break;
case BTN_RIGHT:
if(the_config[menuselection].value <
the_config[menuselection].max)
the_config[menuselection].value++;
if(the_config[menuselection].value < the_config[menuselection].min)
the_config[menuselection].value=
the_config[menuselection].min;
applyConfig();
break;
case BTN_ENTER:
if(menuselection==0)
saveConfig();
return;
}
getInputWaitRelease();
}
/* NOTREACHED */
}

View File

@ -10,39 +10,12 @@
#include "funk/nrf24l01p.h" #include "funk/nrf24l01p.h"
#include "funk/mesh.h"
#include <string.h> #include <string.h>
#define MESH_CHANNEL 85
#define MESH_MAC "MESHB"
/**************************************************************************/ /**************************************************************************/
uint32_t const meshkey[4] = {
0x00000000, 0x00000000, 0x00000000, 0x00000000
};
#define MESHBUFSIZE 10
#define MESHPKTSIZE 32
typedef struct {
uint8_t pkt[32];
char flags;
} MPKT;
MPKT meshbuffer[MESHBUFSIZE];
#define MF_FREE (0)
#define MF_USED (1<<0)
time_t _timet=0;
// Timezones suck. Currently we only do it all in localtime.
// I know it's broken. Sorry
time_t getSeconds(void){
return _timet+(getTimer()*SYSTICKSPEED/1000);
};
// **********************************************************************
void m_init(void){ void m_init(void){
nrf_init(); nrf_init();
@ -56,161 +29,248 @@ void m_init(void){
nrf_config_set(&config); nrf_config_set(&config);
for(int i=0;i<MESHBUFSIZE;i++){ initMesh();
meshbuffer[i].flags=MF_FREE;
};
memset(meshbuffer[0].pkt,0,MESHPKTSIZE);
meshbuffer[0].pkt[0]='T';
uint32touint8p(getSeconds(),meshbuffer[0].pkt+2);
meshbuffer[0].flags=MF_USED;
}; };
void m_tset(void){ void m_tset(void){
_timet=1311961112; _timet=1311961112;
}; };
void m_cleanup(void){
time_t now=getSeconds();
for(int i=0;i<MESHBUFSIZE;i++){
if(meshbuffer[i].flags&MF_USED){
if (uint8ptouint32(meshbuffer[i].pkt+2)<now){
meshbuffer[i].flags=MF_FREE;
};
};
};
};
#define YEAR0 1900 /* the first year */
#define EPOCH_YR 1970 /* EPOCH = Jan 1 1970 00:00:00 */
#define SECS_DAY (24L * 60L * 60L)
#define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
#define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
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) {
static struct tm br_time;
register struct tm *timep = &br_time;
register unsigned long dayclock, dayno;
int year = EPOCH_YR;
dayclock = (unsigned long)time % SECS_DAY;
dayno = (unsigned long)time / SECS_DAY;
timep->tm_sec = dayclock % 60;
timep->tm_min = (dayclock % 3600) / 60;
timep->tm_hour = dayclock / 3600;
timep->tm_wday = (dayno + 4) % 7; /* day 0 was a thursday */
while (dayno >= YEARSIZE(year)) {
dayno -= YEARSIZE(year);
year++;
}
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++;
}
timep->tm_mday = dayno + 1;
timep->tm_isdst = 0;
return timep;
}
void m_recv(void){
__attribute__ ((aligned (4))) uint8_t buf[32];
int len;
int recvend=5000/SYSTICKSPEED+getTimer();
m_cleanup();
nrf_rcv_pkt_start();
getInputWaitRelease();
do{
len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,meshkey);
// Receive
if(len<=0){
delayms_power(10);
continue;
};
int i;
for(i=0;i<MESHBUFSIZE;i++){
if((meshbuffer[i].flags&MF_USED)==0)
break;
};
if(i==MESHBUFSIZE){ // Buffer full. Ah well. Kill a random packet
i=1; // XXX: GetRandom()?
};
memcpy(meshbuffer[i].pkt,buf,MESHPKTSIZE);
meshbuffer[i].flags=MF_USED;
if(buf[0]=='T'){
time_t toff=uint8ptouint32(buf+2)-(getTimer()*SYSTICKSPEED/1000);
if(toff>_timet) // Do not live in the past.
_timet = toff;
lcdPrintln("Got T");
lcdPrintInt(getSeconds());lcdNl();
}else if (buf[0]>='A' && buf[0] <'T'){ // Truncate ascii packets.
meshbuffer[i].pkt[MESHPKTSIZE-3]=0;
};
lcdRefresh();
}while(getTimer()<recvend);
nrf_rcv_pkt_end();
lcdPrintln("Done.");
}
void m_time(void){ void m_time(void){
struct tm* tm; struct tm* tm;
char c[2]={0,0};
getInputWaitRelease(); getInputWaitRelease();
delayms(100); delayms(100);
do{ do{
lcdClear(); lcdClear();
tm= mygmtime(getSeconds()); tm= mygmtime(getSeconds());
lcdPrintInt(tm->tm_hour); lcdPrint(IntToStr(tm->tm_hour,2,F_LONG));
lcdPrint(":"); lcdPrint(":");
lcdPrintInt(tm->tm_min); lcdPrint(IntToStr(tm->tm_min,2,F_LONG|F_ZEROS));
lcdPrint(":"); lcdPrint(":");
lcdPrintInt(tm->tm_sec); lcdPrint(IntToStr(tm->tm_sec,2,F_LONG|F_ZEROS));
lcdNl(); lcdNl();
lcdPrintInt(tm->tm_mday); lcdPrint(IntToStr(tm->tm_mday,2,F_LONG));
lcdPrint("."); lcdPrint(".");
lcdPrintInt(tm->tm_mon+1); lcdPrint(IntToStr(tm->tm_mon+1,2,0));
lcdPrint("."); lcdPrint(".");
lcdPrintInt(tm->tm_year+YEAR0); lcdPrint(IntToStr(tm->tm_year+YEAR0,4,F_LONG|F_ZEROS));
lcdNl();
lcdNl();
lcdPrint("<");
for(int i=0;i<MESHBUFSIZE;i++){
if(!meshbuffer[i].flags&MF_USED){
c[0]='_';
}else{
c[0]=meshbuffer[i].pkt[0];
};
lcdPrint(c);
};
lcdPrintln(">");
lcdPrint("Gen:");
lcdPrintInt(meshgen);
lcdNl(); lcdNl();
lcdRefresh(); lcdRefresh();
delayms(50); delayms_queue(50);
}while ((getInputRaw())==BTN_NONE); }while ((getInputRaw())==BTN_NONE);
}; };
void m_send(void){
int ctr=0;
__attribute__ ((aligned (4))) uint8_t buf[32];
int status;
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;
uint8_t current_offset = 0;
char*p=texts;
do{
lcdPrintln(p);
while(*p)p++;
numentries++;p++;
}while(*p);
numentries--;
visible_lines = (RESY/font->u8Height)-1; // subtract title line
while (1) {
// Display current menu page
lcdClear(); lcdClear();
// Update [T]ime packet lcdPrintln(texts);
p=texts;
while(*p++);
for(int i=0;i<current_offset;i++)
while(*p++);
for (uint8_t i = current_offset; i < (visible_lines + current_offset) && i < numentries; i++) {
if (i == menuselection)
lcdPrint("*");
lcdSetCrsrX(14);
lcdPrintln(p);
while(*p++);
}
lcdRefresh();
uint32touint8p(getSeconds(),meshbuffer[0].pkt+2); switch (getInputWait()) {
case BTN_UP:
menuselection--;
if (menuselection < current_offset) {
if (menuselection < 0) {
menuselection = numentries-1;
current_offset = ((numentries-1)/visible_lines) * visible_lines;
} else {
current_offset -= visible_lines;
}
}
break;
case BTN_DOWN:
menuselection++;
if (menuselection > (current_offset + visible_lines-1) || menuselection >= numentries) {
if (menuselection >= numentries) {
menuselection = 0;
current_offset = 0;
} else {
current_offset += visible_lines;
}
}
break;
case BTN_LEFT:
return -1;
case BTN_RIGHT:
case BTN_ENTER:
return menuselection;
}
getInputWaitRelease();
}
/* NOTREACHED */
}
/***********************************************************************/
char *meshmsgs(void){
static char msgtypes[MESHBUFSIZE+1];
memset(msgtypes,'_',MESHBUFSIZE);
msgtypes[MESHBUFSIZE]=0;
uint8_t lo=0;
uint8_t hi;
for(int o=0;o<MESHBUFSIZE;o++){
hi=0xff;
for(int i=0;i<MESHBUFSIZE;i++){ for(int i=0;i<MESHBUFSIZE;i++){
if(!meshbuffer[i].flags&MF_USED) if(meshbuffer[i].flags&MF_USED){
continue; if(MO_TYPE(meshbuffer[i].pkt)>lo)
ctr++; if(MO_TYPE(meshbuffer[i].pkt)<hi)
memcpy(buf,meshbuffer[i].pkt,MESHPKTSIZE); hi=MO_TYPE(meshbuffer[i].pkt);
status=nrf_snd_pkt_crc_encr(MESHPKTSIZE,buf,meshkey);
//Check status? But what would we do...
}; };
lcdPrint("Pkts: "); lcdPrintInt(ctr); lcdNl(); };
lcdDisplay(); if(hi==0xff){
msgtypes[o]=0;
break;
};
msgtypes[o]=hi;
lo=hi;
};
return msgtypes;
};
extern MPKT meshbuffer[MESHBUFSIZE];
void m_choose(){
char list[99];
int i=0;
while(1){
char *p=list;
strcpy(p,"Note");
while(*p++);
char *mm=meshmsgs();
char *tmm=mm;
while(*mm){
switch(*mm){
case('A'):
strcpy(p,"Message");
break;
case('E'):
strcpy(p,"Kourou");
break;
case('F'):
strcpy(p,"Baikonur");
break;
case('T'):
strcpy(p,"Time");
break;
default:
p[0]=*mm;
p[1]=0;
};
while(*p++);
mm++;
};
p[0]=0;
i=choose(list,i);
if(i<0)
return;
lcdClear();
int j=0;
for(int z=0;z<MESHBUFSIZE;z++)
if(meshbuffer[z].flags&MF_USED)
if(MO_TYPE(meshbuffer[z].pkt)==tmm[i])
j=z;
switch(tmm[i]){
case('A'):
lcdPrintln("Message");
break;
case('E'):
lcdPrintln("Kourou");
break;
case('F'):
lcdPrintln("Baikonur");
break;
case('T'):
lcdPrintln("Time");
break;
};
struct tm *tm= mygmtime(MO_TIME(meshbuffer[j].pkt));
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();
char *foo=(char *)MO_BODY(meshbuffer[j].pkt);
while(strlen(foo)>13){
int q;
for(q=0;q<13;q++){
if(foo[q]==' ')
break;
};
foo[q]=0;
lcdPrintln(foo);
foo[q]=' ';
foo+=q+1;
};
lcdPrintln(foo);
lcdRefresh();
getInputWaitRelease();
};
};
void tick_mesh(void){
mesh_systick();
}; };

View File

@ -37,13 +37,46 @@ void adc_light(void) {
while ((getInputRaw())==BTN_NONE){ while ((getInputRaw())==BTN_NONE){
DoInt(dx,dy,GetLight()); DoInt(dx,dy,GetLight());
DoInt(dx,dy+16,isNight()); DoInt(dx,dy+16,isNight());
DoInt(dx,dy+8,globalconfig.backlighttrigger); DoInt(dx,dy+8,GLOBAL(nighttrigger));
lcdDisplay(); lcdDisplay();
}; };
dy+=8; dy+=8;
dx=DoString(0,dy,"Done."); dx=DoString(0,dy,"Done.");
}; };
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();
lcdRefresh();
delayms_queue(200);
};
lcdPrintln("done.");
};
void gotoISP(void) { void gotoISP(void) {
DoString(0,0,"Enter ISP!"); DoString(0,0,"Enter ISP!");
lcdDisplay(); lcdDisplay();

View File

@ -86,7 +86,7 @@ void dwim(void){
input[inputptr+l]=0; input[inputptr+l]=0;
puts_plus(&input[inputptr]); puts_plus(&input[inputptr]);
for(int i=0;i<l;i++){ for(int i=0;i<l;i++){
if(input[inputptr+i] =='\r'){ if(input[inputptr+i] =='\r' || input[inputptr+i] =='\n'){
input[inputptr+i]=0; input[inputptr+i]=0;
process(input); process(input);
if(i<l) if(i<l)
@ -233,24 +233,41 @@ int process(char * input){
__attribute__ ((aligned (4))) uint8_t buf[32]; __attribute__ ((aligned (4))) uint8_t buf[32];
int status=0; int status=0;
int len; int len;
if (input[1]==' '){ int idx=1;
uint8_t *hex=hextobyte(&input[2],&len); char ctr=1;
if(len<10) len=10; char debug=0;
if(input[idx]=='d'){
debug=1;
idx++;
};
if(input[idx]=='+'){
ctr=10;
idx++;
};
len+=2; if (input[idx]==' '){
puts_plus("S Len:"); uint8_t *hex=hextobyte(&input[idx],&len);
puts_plus(IntToStrX( len,2 )); if(len<10) len=10; // minmal packet length?
puts_plus("\r\n"); len+=2; // Add crc!
status=nrf_snd_pkt_crc_encr(len,hex,thekey); memcpy(buf,hex,len);
status=nrf_snd_pkt_crc_encr(len,buf,thekey);
puts_plus("P "); puts_plus("P ");
puts_plus("[");puts_plus(IntToStrX(len,2));puts_plus("] "); puts_plus("[");puts_plus(IntToStrX(len,2));puts_plus("] ");
if(debug){
for(int i=0;i<len;i++){ for(int i=0;i<len;i++){
puts_plus(IntToStrX( hex[i],2 )); puts_plus(IntToStrX( buf[i],2 ));
puts_plus(" "); puts_plus(" ");
}; };
puts_plus("\r\n"); };
puts("\r\n");
while(--ctr>0){
delayms(23);
memcpy(buf,hex,len);
status=nrf_snd_pkt_crc_encr(len,buf,thekey);
};
}else if (input[1]=='t'){ }else if (input[1]=='t'){
static int ctr=1; static int ctr=1;
int status; int status;

View File

@ -1,38 +0,0 @@
#include <sysinit.h>
#include "basic/basic.h"
#include "lcd/print.h"
#include "lcd/display.h"
#include "filesystem/ff.h"
#include <string.h>
/**************************************************************************/
void readcfg(void) {
readConfig();
};
void savecfg(void){
saveConfig();
};
void applycfg(void){
applyConfig();
};
void show(void){
lcdClear();
lcdPrint("time:"); lcdPrintInt(globalconfig.time); lcdNl();
lcdPrint("btrig:"); lcdPrintInt(globalconfig.backlighttrigger); lcdNl();
lcdPrint("bval:"); lcdPrintInt(globalconfig.backlightvalue); lcdNl();
lcdPrint("lcd:"); lcdPrintInt(globalconfig.lcdstate); lcdNl();
lcdPrint("priv:"); lcdPrintInt(globalconfig.privacy); lcdNl();
lcdRefresh();
};
void lcdmirror(void){
lcdToggleFlag(LCD_MIRRORX);
};

View File

@ -37,13 +37,46 @@ void adc_light(void) {
while ((getInputRaw())==BTN_NONE){ while ((getInputRaw())==BTN_NONE){
DoInt(dx,dy,GetLight()); DoInt(dx,dy,GetLight());
DoInt(dx,dy+16,isNight()); DoInt(dx,dy+16,isNight());
DoInt(dx,dy+8,globalconfig.backlighttrigger); DoInt(dx,dy+8,GLOBAL(nighttrigger));
lcdDisplay(); lcdDisplay();
}; };
dy+=8; dy+=8;
dx=DoString(0,dy,"Done."); dx=DoString(0,dy,"Done.");
}; };
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();
lcdRefresh();
delayms_queue(200);
};
lcdPrintln("done.");
};
void gotoISP(void) { void gotoISP(void) {
DoString(0,0,"Enter ISP!"); DoString(0,0,"Enter ISP!");
lcdDisplay(); lcdDisplay();

View File

@ -20,6 +20,8 @@ OBJS += random.o
OBJS += idle.o OBJS += idle.o
OBJS += config.o OBJS += config.o
OBJS += itoa.o OBJS += itoa.o
OBJS += stringin.o
OBJS += simpletime.o
LIBNAME=basic LIBNAME=basic

View File

@ -153,6 +153,8 @@ uint8_t getInputRaw(void);
uint8_t getInputWait(void); uint8_t getInputWait(void);
void getInputWaitRelease(void); void getInputWaitRelease(void);
// stringin.c
void input(char prompt[], char line[], uint8_t asciistart, uint8_t asciiend, uint8_t maxlength);
//uuid.c //uuid.c
#include "basic/uuid.h" #include "basic/uuid.h"
@ -185,25 +187,44 @@ void handleMenu(const struct MENU *the_menu);
// config.c // config.c
struct config_t {
time_t time;
uint16_t backlighttrigger;
char backlightvalue;
char lcdstate;
char privacy;
} __attribute__((__packed__));
typedef struct config_t CONFIG;
extern CONFIG globalconfig;
int readConfig(void); int readConfig(void);
int saveConfig(void); int saveConfig(void);
int applyConfig(void); void applyConfig(void);
struct CDESC {
char *name;
char value;
char min;
char max;
};
extern struct CDESC the_config[];
#define GLOBALversion (the_config[0].value)
#define GLOBALprivacy (the_config[1].value)
#define GLOBALnighttrigger (the_config[2].value)
#define GLOBALnightinvert (the_config[3].value)
#define GLOBALlcdbacklight (the_config[4].value)
#define GLOBALlcdmirror (the_config[5].value)
#define GLOBALlcdinvert (the_config[6].value)
#define GLOBALlcdcontrast (the_config[7].value)
#define GLOBAL(x) GLOBAL ## x
#define SYSTICKSPEED 10 #define SYSTICKSPEED 10
// itoa.c // itoa.c
#define F_ZEROS (1<<0)
#define F_LONG (1<<1)
#define F_SPLUS (1<<2)
#define F_SSPACE (1<<3)
const char* IntToStrX(unsigned int num, unsigned int mxlen); const char* IntToStrX(unsigned int num, unsigned int mxlen);
const char* IntToStr(int num, unsigned int mxlen, char flag);
// simpletime.c
#include "basic/simpletime.h"
#endif #endif

View File

@ -1,21 +1,38 @@
#include <sysinit.h> #include <sysinit.h>
#include "basic/basic.h" #include "basic/basic.h"
#include "lcd/lcd.h"
#include "lcd/fonts/smallfonts.h"
#include "lcd/print.h" #include "lcd/print.h"
#include "filesystem/ff.h" #include "filesystem/ff.h"
#include "basic/random.h" #include "basic/random.h"
CONFIG globalconfig = { 0,310,50,0,0}; #define CFGVER 23
struct CDESC the_config[]= {
{"version", CFGVER, CFGVER, CFGVER},
{"privacy", 3, 0, 2 },
{"nighttrigger", 310/2, 0, 255},
{"nightinvert", 1, 0, 1 },
{"lcdbacklight", 50, 0, 100},
{"lcdmirror", 0, 0, 1 },
{"lcdinvert", 0, 0, 1 },
{"lcdcontrast", 3, 1, 6 },
{ NULL, 0, 0, 0 },
};
#define CONFFILE "r0ket.cfg" #define CONFFILE "r0ket.cfg"
#define CONF_ITER for(int i=0;the_config[i].name!=NULL;i++)
/**************************************************************************/ /**************************************************************************/
void applyConfig(){
lcdSetContrast(GLOBAL(lcdcontrast));
return 0;
};
int saveConfig(void){ int saveConfig(void){
FIL file; /* File object */ FIL file; /* File object */
UINT writebytes; UINT writebytes;
UINT allwrite=0;
int res; int res;
res=f_open(&file, CONFFILE, FA_OPEN_ALWAYS|FA_WRITE); res=f_open(&file, CONFFILE, FA_OPEN_ALWAYS|FA_WRITE);
@ -25,16 +42,20 @@ int saveConfig(void){
return 1; return 1;
}; };
res = f_write(&file, &globalconfig, sizeof(CONFIG), &writebytes); CONF_ITER{
res = f_write(&file, &the_config[i].value, sizeof(uint8_t), &writebytes);
allwrite+=writebytes;
if(res){
lcdPrint("write:"); lcdPrint("write:");
lcdPrintln(f_get_rc_string(res)); lcdPrintln(f_get_rc_string(res));
if(res){
return 1; return 1;
}; };
};
lcdPrint("wrote:"); lcdPrint("write:");
lcdPrintInt(writebytes); lcdPrintln(f_get_rc_string(res));
lcdPrintln("b"); lcdPrint(" (");
lcdPrintInt(allwrite);
lcdPrintln("b)");
res=f_close(&file); res=f_close(&file);
lcdPrint("close:"); lcdPrint("close:");
@ -48,34 +69,32 @@ int saveConfig(void){
int readConfig(void){ int readConfig(void){
FIL file; /* File object */ FIL file; /* File object */
UINT readbytes; UINT readbytes;
UINT allread;
int res; int res;
lcdFill(0); // clear display buffer
res=f_open(&file, CONFFILE, FA_OPEN_EXISTING|FA_READ); res=f_open(&file, CONFFILE, FA_OPEN_EXISTING|FA_READ);
lcdPrint("open:");
lcdPrintln(f_get_rc_string(res));
if(res){ if(res){
return 1; return 1;
}; };
res = f_read(&file, &globalconfig, sizeof(CONFIG), &readbytes); CONF_ITER{
lcdPrint("read:"); res = f_read(&file, &the_config[i].value, sizeof(uint8_t), &readbytes);
lcdPrintln(f_get_rc_string(res)); allread+=readbytes;
if(res){ if(GLOBAL(version) != CFGVER){
GLOBAL(version) =CFGVER;
return 1;
};
if(res || GLOBAL(version) != CFGVER)
return 1; return 1;
}; };
res=f_close(&file); res=f_close(&file);
lcdPrint("close:");
lcdPrintln(f_get_rc_string(res));
if(res){ if(res){
return 1; return 1;
}; };
applyConfig();
return 0; return 0;
}; };
int applyConfig(){
return 0;
};

View File

@ -1,8 +1,8 @@
#define LEN 32 #include "basic/basic.h"
#define LEN 10
const char* IntToStrX(unsigned int num, unsigned int mxlen){ const char* IntToStrX(unsigned int num, unsigned int mxlen){
static char s[LEN+1]; static char s[LEN+1];
char * o=s;
int len; int len;
s[LEN]=0; s[LEN]=0;
for (len=(LEN-1);len>=(LEN-mxlen);len--){ for (len=(LEN-1);len>=(LEN-mxlen);len--){
@ -13,3 +13,46 @@ const char* IntToStrX(unsigned int num, unsigned int mxlen){
}; };
return &s[len+1]; return &s[len+1];
}; };
#undef LEN
#define LEN 11
const char* IntToStr(int num, unsigned int mxlen, char flag){
static char s[LEN+1];
int len;
s[LEN]=0;
char neg=0;
if(num<0){
num=-num;
neg=1;
};
for (len=(LEN-1);len>=(LEN-mxlen);len--){
s[len]=(num%10)+'0';
num/=10;
};
len++;
if(!(flag&F_LONG)){
while(s[len]=='0' && len < (LEN-1))
len++;
}else if(!(flag&F_ZEROS)){
int x=len;
while(s[x]=='0' && x < (LEN-1)){
s[x]=' ';
x++;
};
}
if(neg==1){
len--;
s[len]='-';
}else if(flag&F_SPLUS){
len--;
s[len]='+';
}else if(flag&F_SSPACE){
s[len]=' ';
};
return &s[len];
};
#undef LEN

View File

@ -7,7 +7,7 @@
uint32_t light=300*HYST; uint32_t light=300*HYST;
char _isnight=1; char _isnight=1;
#define threshold globalconfig.backlighttrigger #define threshold GLOBAL(nighttrigger)
void LightCheck(void){ void LightCheck(void){
int iocon; int iocon;
@ -22,7 +22,7 @@ void LightCheck(void){
gpioSetDir(RB_LED3, gpioDirection_Input); gpioSetDir(RB_LED3, gpioDirection_Input);
IOCON_PIO1_11 = IOCON_PIO1_11_FUNC_AD7|IOCON_PIO1_11_ADMODE_ANALOG; IOCON_PIO1_11 = IOCON_PIO1_11_FUNC_AD7|IOCON_PIO1_11_ADMODE_ANALOG;
light-=light/HYST; light-=light/HYST;
light += adcRead(7); light += (adcRead(7)/2);
gpioSetDir(RB_LED3, iodir); gpioSetDir(RB_LED3, iodir);
IOCON_PIO1_11=iocon; IOCON_PIO1_11=iocon;

View File

@ -0,0 +1,46 @@
#include <sysinit.h>
#include "simpletime.h"
#include "basic/basic.h"
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 }
};
struct tm * mygmtime(register const time_t time) {
static struct tm br_time;
register struct tm *timep = &br_time;
register unsigned long dayclock, dayno;
int year = EPOCH_YR;
dayclock = (unsigned long)time % SECS_DAY;
dayno = (unsigned long)time / SECS_DAY;
timep->tm_sec = dayclock % 60;
timep->tm_min = (dayclock % 3600) / 60;
timep->tm_hour = dayclock / 3600;
timep->tm_wday = (dayno + 4) % 7; /* day 0 was a thursday */
while (dayno >= YEARSIZE(year)) {
dayno -= YEARSIZE(year);
year++;
}
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++;
}
timep->tm_mday = dayno + 1;
timep->tm_isdst = 0;
return timep;
}
// Timezones suck. Currently we only do it all in localtime.
// I know it's broken. Sorry
time_t getSeconds(void){
return _timet+(getTimer()*SYSTICKSPEED/1000);
};

View File

@ -0,0 +1,16 @@
#ifndef __SIMPLETIME_H_
#define __SIMPLETIME_H_
#include <time.h>
#define YEAR0 1900 /* the first year */
#define EPOCH_YR 1970 /* EPOCH = Jan 1 1970 00:00:00 */
#define SECS_DAY (24L * 60L * 60L)
#define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
#define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
extern time_t _timet;
struct tm * mygmtime(register const time_t time);
time_t getSeconds(void);
#endif

107
firmware/basic/stringin.c Normal file
View File

@ -0,0 +1,107 @@
#include "basic/basic.h"
#include "lcd/render.h"
#include "lcd/display.h"
#include "lcd/allfonts.h"
#define CHARWIDTH 12
#define CHARSPACE 0x20
struct in{
char *line, *prompt;
uint8_t pos, dcursor, maxlength, asciistart, asciiend;
bool done;
} s_input;
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;
s_input.maxlength = l;
s_input.asciistart = as;
s_input.asciiend = ae;
s_input.pos = 0;
s_input.dcursor = 0;
s_input.done = false;
}
void inputMove() {
char *cur = s_input.line+s_input.pos+s_input.dcursor;
char key = getInput();
if (key == BTN_LEFT) {
if (s_input.dcursor >0) {
s_input.dcursor --;
} else if (s_input.pos > 0) {
s_input.pos --;
}
} else if (key == BTN_RIGHT) {
if (s_input.dcursor <RESX/CHARWIDTH-1 && s_input.pos + s_input.dcursor < s_input.maxlength) {
if (*cur == 0) {
*cur = CHARSPACE;
}
s_input.dcursor ++;
} else if (s_input.pos + RESX/CHARWIDTH < s_input.maxlength) {
s_input.pos++;
if (*cur == 0) {
*cur = CHARSPACE;
}
}
} else if (key == BTN_DOWN) {
if (*cur <= s_input.asciistart) {
*cur = s_input.asciiend;
} else if (*cur > s_input.asciiend) {
*cur = s_input.asciiend;
} else {
*cur = *cur - 1;
}
} else if (key == BTN_UP) {
if (*cur >= s_input.asciiend) {
*cur = s_input.asciistart;
} else if (*cur < s_input.asciistart) {
*cur = s_input.asciistart;
} else {
*cur = *cur + 1;
}
} else if (key == (BTN_ENTER)) {
s_input.done = true;
}
}
void inputDraw() {
lcdFill(0);
DoString(0,0,s_input.prompt);
for (int dx = 0; dx<= RESX/CHARWIDTH && s_input.pos+dx<s_input.maxlength; dx++){
char tmp[1];
tmp[0] = s_input.line[s_input.pos+dx];
DoString(dx*CHARWIDTH, 30,tmp);
}
DoString(s_input.dcursor * CHARWIDTH, 40, "-");
}
void inputClean() {
for (int x=0;x<=s_input.maxlength;x++) {
if (s_input.line[x] == 0) {
x--;
while (s_input.line[x] == CHARSPACE && x>=0) {
s_input.line[x] = 0;
x--;
}
return;
}
}
}
void input(char prompt[], char line[], uint8_t asciistart, uint8_t asciiend, uint8_t maxlength){
font=&Font_7x8;
inputInit(prompt, line, maxlength, asciistart, asciiend);
lcdFill(0);
while (!s_input.done) {
lcdDisplay();
inputMove();
inputDraw();
}
inputClean();
return;
}

View File

@ -8,6 +8,7 @@ OBJS += nrf24l01p.o
OBJS += rftransfer.o OBJS += rftransfer.o
OBJS += filetransfer.o OBJS += filetransfer.o
OBJS += openbeacon.o OBJS += openbeacon.o
OBJS += mesh.o
LIBNAME=funk LIBNAME=funk

165
firmware/funk/mesh.c Normal file
View File

@ -0,0 +1,165 @@
#include <sysinit.h>
#include <string.h>
#include "basic/basic.h"
#include "funk/mesh.h"
#include "funk/nrf24l01p.h"
#include "basic/byteorder.h"
#include "basic/random.h"
char meshgen=0; // Generation
MPKT meshbuffer[MESHBUFSIZE];
uint32_t const meshkey[4] = {
0x00000000, 0x00000000, 0x00000000, 0x00000000
};
struct NRF_CFG oldconfig;
void initMesh(void){
for(int i=0;i<MESHBUFSIZE;i++){
meshbuffer[i].flags=MF_FREE;
};
memset(meshbuffer[0].pkt,0,MESHPKTSIZE);
meshbuffer[0].pkt[0]='T';
MO_TIME_set(meshbuffer[0].pkt,getSeconds());
meshbuffer[0].flags=MF_USED;
};
inline void blink(char a, char b){
gpioSetValue (a,b, 1-gpioGetValue(a,b));
};
void mesh_cleanup(void){
time_t now=getSeconds();
for(int i=1;i<MESHBUFSIZE;i++){
if(meshbuffer[i].flags&MF_USED){
if (MO_GEN(meshbuffer[i].pkt)<meshgen)
meshbuffer[i].flags=MF_FREE;
if (MO_TIME(meshbuffer[i].pkt)<now)
meshbuffer[i].flags=MF_FREE;
if (MO_TIME(meshbuffer[i].pkt)-now>SECS_DAY)
meshbuffer[i].flags=MF_FREE;
};
};
};
void mesh_recvloop(void){
__attribute__ ((aligned (4))) uint8_t buf[32];
int len;
int recvend=M_RECVTIM/SYSTICKSPEED+getTimer();
int pktctr=0;
nrf_config_get(&oldconfig);
nrf_set_channel(MESH_CHANNEL);
nrf_set_rx_mac(0,MESHPKTSIZE,strlen(MESH_MAC),(uint8_t*)MESH_MAC);
mesh_cleanup();
nrf_rcv_pkt_start();
do{
len=nrf_rcv_pkt_poll_dec(sizeof(buf),buf,meshkey);
// Receive
if(len<=0){
delayms_power(10);
continue;
};
pktctr++;
if(MO_GEN(buf)>meshgen){
if(meshgen)
meshgen++;
else
meshgen=MO_GEN(buf);
_timet=0;
};
if(MO_TYPE(buf)=='T'){
time_t toff=MO_TIME(buf)-((getTimer()-(200/SYSTICKSPEED))/(1000/SYSTICKSPEED));
if (toff>_timet) // Do not live in the past.
_timet = toff;
continue;
};
// Safety: Truncate ascii packets by 0-ing the CRC
if (MO_TYPE(buf) >='A' && MO_TYPE(buf) <='Z'){
buf[MESHPKTSIZE-2]=0;
};
// Store packet in a free slot
int free=-1;
for(int i=0;i<MESHBUFSIZE;i++){
if ( ((meshbuffer[i].flags&MF_USED)==0) && free<0 )
free=i;
if ( (meshbuffer[i].flags&MF_USED) &&
(MO_TYPE(meshbuffer[i].pkt) == MO_TYPE(buf))){
if ( MO_TIME(buf) >
MO_TIME(meshbuffer[i].pkt)){
free=i;
break;
}else{
free=-2;
break;
};
};
};
if(free==-1){ // Buffer full. Ah well. Kill a random packet
free=1; // XXX: GetRandom()?
};
if(free<0)
continue;
memcpy(meshbuffer[free].pkt,buf,MESHPKTSIZE);
meshbuffer[free].flags=MF_USED;
}while(getTimer()<recvend || pktctr>MESHBUFSIZE);
nrf_rcv_pkt_end();
nrf_config_set(&oldconfig);
}
void mesh_sendloop(void){
int ctr=0;
__attribute__ ((aligned (4))) uint8_t buf[32];
int status;
nrf_config_get(&oldconfig);
nrf_set_channel(MESH_CHANNEL);
nrf_set_tx_mac(strlen(MESH_MAC),(uint8_t*)MESH_MAC);
// Update [T]ime packet
MO_TIME_set(meshbuffer[0].pkt,getSeconds());
MO_GEN_set(meshbuffer[0].pkt,meshgen);
for (int i=0;i<MESHBUFSIZE;i++){
if(!meshbuffer[i].flags&MF_USED)
continue;
ctr++;
memcpy(buf,meshbuffer[i].pkt,MESHPKTSIZE);
status=nrf_snd_pkt_crc_encr(MESHPKTSIZE,buf,meshkey);
//Check status? But what would we do...
};
nrf_config_set(&oldconfig);
};
void mesh_systick(void){
static int rcvctr=0;
static int sendctr=0;
if(rcvctr--<0){
push_queue(&mesh_recvloop);
rcvctr=M_RECVINT/SYSTICKSPEED/2;
rcvctr+=getRandom()%(rcvctr*2);
};
if(sendctr--<0){
push_queue(&mesh_sendloop);
sendctr=M_SENDINT/SYSTICKSPEED/2;
sendctr+=getRandom()%(sendctr*2);
};
};

39
firmware/funk/mesh.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef __MESH_H_
#define __MESH_H_
#define MESHBUFSIZE 10
#define MESHPKTSIZE 32
#define M_SENDINT 500
#define M_RECVINT 1000
#define M_RECVTIM 100
#define MESH_CHANNEL 85
#define MESH_MAC "MESHB"
#define MO_TYPE(x) (x[0])
#define MO_TYPE_set(x,y) (x[0]=y)
#define MO_GEN(x) (x[1])
#define MO_GEN_set(x,y) (x[1]=y)
#define MO_TIME(x) (uint8ptouint32(x+2))
#define MO_TIME_set(x,y) (uint32touint8p(y,x+2))
#define MO_BODY(x) (x+6)
typedef struct {
uint8_t pkt[32];
char flags;
} MPKT;
#define MF_FREE (0)
#define MF_USED (1<<0)
extern char meshgen; // Generation
extern MPKT meshbuffer[MESHBUFSIZE];
void initMesh(void);
void mesh_cleanup(void);
void mesh_recvloop(void);
void mesh_sendloop(void);
void mesh_systick(void);
#endif

View File

@ -32,7 +32,7 @@ void openbeaconSave(uint32_t s)
BYTE buf[4]; BYTE buf[4];
UINT readbytes; UINT readbytes;
if( f_open(&file, "beacon", FA_OPEN_ALWAYS|FA_WRITE) ) if( f_open(&file, "beacon.cfg", FA_OPEN_ALWAYS|FA_WRITE) )
return; return;
uint32touint8p(s, buf); uint32touint8p(s, buf);

View File

@ -13,7 +13,6 @@
/**************************************************************************/ /**************************************************************************/
uint8_t lcdBuffer[RESX*RESY_B]; uint8_t lcdBuffer[RESX*RESY_B];
#define lcd_layout globalconfig.lcdstate
uint32_t intstatus; // Caches USB interrupt state uint32_t intstatus; // Caches USB interrupt state
// (need to disable MSC while displaying) // (need to disable MSC while displaying)
@ -153,12 +152,12 @@ void lcdDisplay(void) {
uint16_t i,page; uint16_t i,page;
for(page=0; page<RESY_B;page++) { for(page=0; page<RESY_B;page++) {
for(i=0; i<RESX; i++) { for(i=0; i<RESX; i++) {
if (lcd_layout & LCD_MIRRORX) if (GLOBAL(lcdmirror))
byte=lcdBuffer[page*RESX+RESX-1-(i)]; byte=lcdBuffer[page*RESX+RESX-1-(i)];
else else
byte=lcdBuffer[page*RESX+(i)]; byte=lcdBuffer[page*RESX+(i)];
if (lcd_layout & LCD_INVERTED) if (GLOBAL(lcdinvert))
byte=~byte; byte=~byte;
lcdWrite(TYPE_DATA,byte); lcdWrite(TYPE_DATA,byte);
@ -169,13 +168,36 @@ void lcdDisplay(void) {
} }
inline void lcdInvert(void) { inline void lcdInvert(void) {
lcdToggleFlag(LCD_INVERTED); GLOBAL(lcdinvert)=!GLOBAL(lcdinvert);
} }
void lcdToggleFlag(int flag) { void lcdSetContrast(int c) {
lcd_layout=lcd_layout ^ flag; c+=0x20;
} if(c>0x2e) c=0x24;
lcd_select();
lcdWrite(TYPE_CMD,c);
lcd_deselect();
};
void lcdSetInvert(int c) {
if(c>1)
c=1;
if(c<0)
c=1;
c+=0xa6;
lcd_select();
lcdWrite(TYPE_CMD,c);
lcd_deselect();
};
/* deprecated */
void __attribute__((__deprecated__)) lcdToggleFlag(int flag) {
if(flag==LCD_MIRRORX)
GLOBAL(lcdmirror)=!GLOBAL(lcdmirror);
if(flag==LCD_INVERTED)
GLOBAL(lcdinvert)=!GLOBAL(lcdinvert);
}
void lcdShiftH(bool right, bool wrap) { void lcdShiftH(bool right, bool wrap) {

View File

@ -29,4 +29,5 @@ void lcdSetPixel(char x, char y, bool f);
void lcdSafeSetPixel(char x, char y, bool f); void lcdSafeSetPixel(char x, char y, bool f);
bool lcdGetPixel(char x, char y); bool lcdGetPixel(char x, char y);
void lcdShift(int x, int y, bool wrap); void lcdShift(int x, int y, bool wrap);
void lcdSetContrast(int c);
#endif #endif

View File

@ -34,6 +34,12 @@ void setExtFont(const char *fname){
font=NULL; font=NULL;
}; };
int getFontHeight(void){
if(font)
return font->u8Height;
return 8; // XXX: Should be done right.
};
int _getFontData(int type, int offset){ int _getFontData(int type, int offset){
UINT readbytes; UINT readbytes;

View File

@ -36,6 +36,7 @@ int DoCharX(int sx, int sy, unsigned char num);
int DoShortX(int sx, int sy, uint16_t num); int DoShortX(int sx, int sy, uint16_t num);
void setIntFont(const struct FONT_DEF * font); void setIntFont(const struct FONT_DEF * font);
void setExtFont(const char *file); void setExtFont(const char *file);
int getFontHeight(void);
#define START_FONT 0 #define START_FONT 0
#define SEEK_EXTRAS 1 #define SEEK_EXTRAS 1