First minimesh code.

This commit is contained in:
Stefan `Sec` Zehl 2011-07-29 19:40:24 +02:00
parent 76ee150b3f
commit 9cf36467e4
4 changed files with 359 additions and 0 deletions

View File

@ -0,0 +1,38 @@
#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

@ -0,0 +1,212 @@
#include <sysinit.h>
#include <string.h>
#include <time.h>
#include "basic/basic.h"
#include "basic/byteorder.h"
#include "lcd/lcd.h"
#include "lcd/print.h"
#include "funk/nrf24l01p.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){
nrf_init();
struct NRF_CFG config = {
.channel= MESH_CHANNEL,
.txmac= MESH_MAC,
.nrmacs=1,
.mac0= MESH_MAC,
.maclen ="\x20", // XXX: MESHPKTSIZE
};
nrf_config_set(&config);
for(int i=0;i<MESHBUFSIZE;i++){
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;
_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=100/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;
};
}while(getTimer()<recvend);
nrf_rcv_pkt_end();
lcdPrintln("Done.");
}
void m_time(void){
struct tm* tm;
getInputWaitRelease();
delayms(100);
do{
lcdClear();
tm= mygmtime(getSeconds());
lcdPrintInt(tm->tm_hour);
lcdPrint(":");
lcdPrintInt(tm->tm_min);
lcdPrint(":");
lcdPrintInt(tm->tm_sec);
lcdNl();
lcdPrintInt(tm->tm_mday);
lcdPrint(".");
lcdPrintInt(tm->tm_mon+1);
lcdPrint(".");
lcdPrintInt(tm->tm_year+YEAR0);
lcdNl();
lcdRefresh();
delayms(50);
}while ((getInputRaw())==BTN_NONE);
};
void m_send(void){
int ctr=0;
__attribute__ ((aligned (4))) uint8_t buf[32];
int status;
lcdClear();
// Update [T]ime packet
uint32touint8p(getSeconds(),meshbuffer[0].pkt+2);
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(16,buf,meshkey);
//Check status? But what would we do...
};
lcdPrint("Pkts: "); lcdPrintInt(ctr); lcdNl();
lcdDisplay();
};

View File

@ -0,0 +1,83 @@
#include <sysinit.h>
#include "basic/basic.h"
#include "lcd/lcd.h"
#include "lcd/print.h"
#include "lcd/allfonts.h"
#include "filesystem/ff.h"
#include "filesystem/select.h"
#include "funk/nrf24l01p.h"
#include "usb/usbmsc.h"
#include <string.h>
/**************************************************************************/
void show_ticks(void) {
int dx=0;
int dy=8;
lcdClear();
dx=DoString(0,dy,"Ticks:");
while ((getInputRaw())==BTN_NONE){
DoInt(0,dy+8,_timectr);
lcdDisplay();
};
dy+=16;
dx=DoString(0,dy,"Done.");
};
void adc_light(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,globalconfig.backlighttrigger);
lcdDisplay();
};
dy+=8;
dx=DoString(0,dy,"Done.");
};
void gotoISP(void) {
DoString(0,0,"Enter ISP!");
lcdDisplay();
ISPandReset();
}
void lcd_mirror(void) {
lcdToggleFlag(LCD_MIRRORX);
};
void lcd_invert(void) {
lcdToggleFlag(LCD_INVERTED);
};
void adc_check(void) {
int dx=0;
int dy=8;
// Print Voltage
dx=DoString(0,dy,"Voltage:");
while ((getInputRaw())==BTN_NONE){
DoInt(dx,dy,GetVoltage());
lcdDisplay();
};
dy+=8;
dx=DoString(0,dy,"Done.");
};
void msc_menu(void){
DoString(0,8,"MSC Enabled.");
lcdDisplay();
usbMSCInit();
getInputWaitRelease();
getInputWait();
DoString(0,16,"MSC Disabled.");
usbMSCOff();
};

View File

@ -0,0 +1,26 @@
#include <sysinit.h>
#include "basic/basic.h"
#include "lcd/lcd.h"
#include "lcd/print.h"
#include "funk/nrf24l01p.h"
#include <string.h>
#include "funk/rftransfer.h"
#include "funk/openbeacon.h"
#include "core/iap/iap.h"
/**************************************************************************/
void f_uuid(void) {
IAP_return_t iap_return;
iap_return = iapReadSerialNumber();
lcdPrintIntHex(iap_return.Result[0]); lcdNl();
lcdPrintIntHex(iap_return.Result[1]); lcdNl();
lcdPrintIntHex(iap_return.Result[2]); lcdNl();
lcdPrintIntHex(iap_return.Result[3]); lcdNl();
}