Merge branch 'master' of github.com:p42/project42

This commit is contained in:
schneider 2011-06-14 12:27:13 +02:00
commit f8e99afd90
4 changed files with 235 additions and 107 deletions

View File

@ -11,20 +11,10 @@
uint8_t lcdBuffer[RESX*RESY_B];
int inverted = 0;
/*
//TODO FIXME why doenst that work ?
#define CS RB_LCD_CS
#define SCK RB_SPI_SCK
#define SDA RB_SPI_MOSI
#define RST RB_LCD_RST
*/
#define CS 2,1
#define SCK 2,11
//#define SCK 2,8
#define SDA 0,9
//#define SDA 2,8
#define RST 2,2
void lcdWrite(uint8_t cd, uint8_t data)
{
@ -136,14 +126,22 @@ void lcdFill(char f){
void lcdSetPixel(char x, char y, bool f){
char y_byte = (RESY-(y+1)) / 8;
char y_off = (RESY-(y+1)) % 8;
char byte = lcdBuffer[y_byte*RESX+(RESX-x)%RESX];
char byte = lcdBuffer[y_byte*RESX+(RESX-(x+1))];
if (f) {
byte |= (1 << y_off);
} else {
byte &= ~(1 << y_off);
}
lcdBuffer[y_byte*RESX+(RESX-x)%RESX] = byte;
lcdBuffer[y_byte*RESX+(RESX-(x+1))] = byte;
}
bool lcdGetPixel(char x, char y){
char y_byte = (RESY-(y+1)) / 8;
char y_off = (RESY-(y+1)) % 8;
char byte = lcdBuffer[y_byte*RESX+(RESX-(x+1))];
return byte & (1 << y_off);
}
void lcdDisplay(uint32_t shift)
{
lcdWrite(0,0xB0);

View File

@ -24,5 +24,6 @@ void lcdFill(char f);
void lcdDisplay(uint32_t shift);
void lcdInvert(void);
void lcdSetPixel(char x, char y, bool f);
bool lcdGetPixel(char x, char y);
#endif

171
modules/mandelbrot.c Normal file
View File

@ -0,0 +1,171 @@
#include <sysinit.h>
#include "basic/basic.h"
#include "lcd/render.h"
#include "lcd/display.h"
#include "lcd/allfonts.h"
#define FIXSIZE 25
#define mul(a,b) ((((long long)a)*(b))>>FIXSIZE)
#define fixpt(a) ((long)(((a)*(1<<FIXSIZE))))
#define integer(a) (((a)+(1<<(FIXSIZE-1)))>>FIXSIZE)
void ReinvokeISP(void);
void EnableWatchdog(uint32_t ms);
void delayms(uint32_t ms);
/**************************************************************************/
void checkISP(void) {
if(gpioGetValue(RB_BTN0)==0){
gpioSetValue (RB_LED1, CFG_LED_ON);
delayms(200);
gpioSetValue (RB_LED1, CFG_LED_OFF);
while(gpioGetValue(RB_BTN0)==0);
EnableWatchdog(1000*5);
ReinvokeISP();
}
}
void cross(char x, char y) {
lcdSetPixel(x,y,false);
lcdSetPixel(x+1,y,true);
lcdSetPixel(x-1,y,true);
lcdSetPixel(x,y+1,true);
lcdSetPixel(x,y-1,true);
lcdSetPixel(x+2,y,false);
lcdSetPixel(x-2,y,false);
lcdSetPixel(x,y+2,false);
lcdSetPixel(x,y-2,false);
lcdDisplay(0);
}
void module_mandelbrot(void) {
gpioSetValue (RB_LED1, CFG_LED_OFF);
backlightInit();
bool toggle = false;
int counter = 0;
long r0,i0,p,q,rn,tot;
//double xmin=-2.5,ymin=-1.5,xmax=1.5,ymax=1.5,xs,ys;
double rmin0=-2.2*0.9, imin0=-2.0*0.9, rmax0=1.0*0.9, imax0=2.0*0.9;
double i_center=0, r_center=0;
double rmin=rmin0,imin=imin0,rmax=rmax0,imax=imax0,rs,is;
int iteration,r,i;
double zoom = 1;
int iteration_max = 300;
int x_center = 45;
int y_center= 40;
while (1) {
checkISP();
lcdDisplay(0);
delayms(100);
rs=(rmax-rmin)/68.0;
is=(imax-imin)/96.0;
for (r=0;r<RESY;r++) {
for (i=0;i<RESX;i++) {
p=fixpt(rmin+r*rs);
q=fixpt(imin+i*is);
rn=0;
r0=0;
i0=0;
iteration=0;
while ((mul(rn,rn)+mul(i0,i0))<fixpt(4) && ++iteration<iteration_max) {
rn=mul((r0+i0),(r0-i0)) +p;
i0=mul(fixpt(2),mul(r0,i0)) +q;
r0=rn;
}
tot+=iteration;
if (iteration==iteration_max) iteration=1;
bool pixel = ( iteration>1);
lcdSetPixel (i,r,pixel);
//lcdSetPixel ((RESX-1)-i,r,pixel);
checkISP();
}
}
lcdDisplay(0);
//for (int x=0; x<RESX;x++){
// for(int y=0; y<RESY;y++){
// bool p = lcdGetPixel(x,y);
// lcdSetPixel(x,y,~p);
// lcdDisplay(0);
// }
//}
bool selected_val = lcdGetPixel(x_center, y_center);
for (int delta = 0; delta<100; delta++){
double test_i = imin + (imax-imin)*x_center/96.0;
double test_r = rmin +(imax-rmin)*y_center/68.0;
//double dist = test_i * test_i + test_r * test_r;
//double dist = ((i_center - test_i)*(i_center - test_i) + (r_center - test_r)*(r_center - test_r))*1.0/zoom;
double dist = 0;
if (x_center + delta < RESX && lcdGetPixel(x_center+delta,y_center) != selected_val && dist <=2){
x_center +=delta-1;
break;
}
if (x_center - delta >= 0 && lcdGetPixel(x_center-delta,y_center) != selected_val && dist <=2){
x_center -=delta+1;
break;
}
if (y_center + delta < RESY && lcdGetPixel(x_center, y_center+delta) != selected_val && dist <=2){
y_center +=delta-1;
break;
}
if (y_center -delta >= 0 && lcdGetPixel(x_center, y_center-delta) != selected_val && dist <=2) {
y_center -= delta+1;
break;
}
}
cross(x_center, y_center);
i_center = x_center/96.0 -0.5;
r_center = y_center/68.0 -0.5;
double i_off = (imax-imin)*i_center;
double r_off = (rmax-rmin)*r_center;
imin += i_off*1.0;
imax += i_off*1.0;
rmin += r_off*1.0;
rmax += r_off*1.0;
imin = imin + (imax-imin)*(1-zoom);
imax = imax - (imax-imin)*(1-zoom);
rmin = rmin +(rmax-rmin)*(1-zoom);
rmax = rmax -(rmax-rmin)*(1-zoom);
//imin*=zoom;
//imax*=zoom;
//rmin*=zoom;
//rmax*=zoom;
zoom *= 0.995;
iteration_max = iteration_max*1.02;
x_center = RESX/2;
y_center= RESY/2;
cross(x_center,y_center);
if (counter > 24) {
if (toggle) {
toggle = false;
} else {
toggle = true;
}
counter = 0;
} else {
counter ++;
}
}
return;
}

View File

@ -5,21 +5,14 @@
#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 module_sec(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;
@ -27,105 +20,70 @@ void module_sec(void) {
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
trigger=20;
uint32_t ctr=0;
char key;
while (1) {
ctr++;
uint32_t results;
lcdDisplay(j);
lcdDisplay(0);
delayms(10);
font=fonts[fontctr];
key= getInput();
if(key==BTN_UP){
trigger +=1;
}else if (key ==BTN_DOWN){
trigger -=1;
};
if(gpioGetValue(RB_BTN3)==0){
while(gpioGetValue(RB_BTN3)==0);
trigger +=10;
};
if(gpioGetValue(RB_BTN2)==0){
while(gpioGetValue(RB_BTN2)==0);
trigger -=10;
};
font=&Font_7x8;
dx=DoString(0,0,"Trig:");
dx=DoInt(dx,0,trigger);
DoString(dx,0," ");
if(gpioGetValue(RB_BTN0)==0){
while(gpioGetValue(RB_BTN0)==0);
// Easy flashing
if(key==BTN_LEFT){
DoString(0,8,"Enter ISP!");
lcdDisplay(0);
EnableWatchdog(1000*5);
ReinvokeISP();
ISPandReset(5);
};
// Display nickname
font = &Font_Ubuntu36pt;
dx=DoString(0,0,"Sec");
#ifdef SEND
if(ctr++>trigger/10){
// Blink LED
if(ctr++>trigger){
ctr=0;
if (gpioGetValue(RB_LED0) == CFG_LED_OFF){
gpioSetValue (RB_LED0, CFG_LED_ON);
// DoString(dx,14,"ON!");
if (gpioGetValue(RB_LED2) == CFG_LED_OFF){
gpioSetValue (RB_LED2, CFG_LED_ON);
} else {
gpioSetValue (RB_LED0, CFG_LED_OFF);
// DoString(dx,14,"off");
gpioSetValue (RB_LED2, CFG_LED_OFF);
};
};
#else
results = adcRead(0);
DoInt(dx,20,results);
if(results>trigger){
DoString(dx,30,"YES!");
}else{
DoString(dx,30," no ");
};
#endif
// Print Voltage
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 ");
;
}
DoInt(dx,yctr+28,GetVoltage());
}
return;
}
void tick_sec(void){
static int foo=0;
static int toggle=0;
if(foo++>50){
toggle=1-toggle;
foo=0;
gpioSetValue (RB_LED0, toggle);
};
return;
};