Skip to content

Instantly share code, notes, and snippets.

@fenrig
Created March 26, 2012 23:22
Show Gist options
  • Save fenrig/2210574 to your computer and use it in GitHub Desktop.
Save fenrig/2210574 to your computer and use it in GitHub Desktop.
//MASTER BMP DECODER
//Requests data from slave (2bytes)
#include <Wire.h>
#include <TFT.h>
#define SLAVE 0x02
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A1 // must be an analog pin, use "An" notation!
#define YM 14 // can be a digital pin, this is A0
#define XP 17 // can be a digital pin, this is A3
int x = 0;
int y = 0;
int decode = 1;
void setup(void){
Serial.begin(9600);
Wire.begin();
Tft.init();
}
void loop(void){
if(decode == 1){
photo_bytes();
}else{
change_photo();
decode = 1;
}
}
void change_photo(){
Wire.beginTransmission(SLAVE);
Wire.write(2);
Wire.endTransmission();
}
void photo_bytes(){
Wire.requestFrom(SLAVE,3);
delay(1);
while(Wire.available() && decode){
Serial.println(Wire.available());
unsigned int color_low = Wire.read();
unsigned int color_high = Wire.read();
Serial.print(" - ");
Serial.print(color_high);
Serial.print(" - ");
Serial.println(color_low);
//color_high = (color_high<<8);
color_high += color_low;
Tft.setPixel(x,y,color_high);
x++;
if(x == 241){
x = 0;
y++;
if(y == 321){
y = 0;
decode = 0;
}
}
//Wire.requestFrom(SLAVE,2); //maakt proces veeeeel sneller
delay(1);
}
}
//slave
//Decodes 24 bit bmp to 16 bit colors (hence the 2 bytes)
//Every request is a pixel
#include <SD.h>
#include <Wire.h>
#define SLAVE 0x02
#define RGB16(red, green, blue) ( ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3))
void(* resetFunc) (void) = 0;
unsigned char blue,green,red,color_high,color_low;
unsigned int color;
int no_pics; //aantal fotos
int no_off_pic = 0; //stand van de foto
File pic;
void setup(){
Serial.begin(9600);
Serial.println("begin");
pinMode(13,OUTPUT); //nodig voor SD kaart
if (! SD.begin(10)){
Serial.println("init SD failed\nREBOOT\n--------");
delay(40);
resetFunc();
}
Serial.println("jep");
pinMode(13, OUTPUT); //COMMUNICATIE LED
//picture
no_pics = listpics(); //tel alle bmp files in /pictures en delete alle niet bmp's
openpic(); //open eerste foto
//i²c communicatie
Wire.onRequest(requestEvent);
Wire.onReceive(receiveEvent);
Wire.begin(SLAVE);
}
void loop(void){
int i = 0;
i++;
i = i + i;
if(i > 200) i = 0;
if(i < 0) Serial.println("jaja");
}
int readfile(){
pic.seek(54);
}
int listpics(){
File dir = SD.open("/pictures");
int files = 0;
int lengte;
char name[255];
while(1){
File entry = dir.openNextFile(); //open volgende file
if(! entry) break; //geen files meer
strcpy(name," ");
strcpy(name,entry.name());
lengte = strlen(name);
if(name[lengte-1] == 'P' && name[lengte-2] == 'M' && name[lengte-3] == 'B' && name[lengte-4] == '.'){
files++;
}
else{ //delete niet ".bmp" files :D
strcpy(name,"/pictures/");
strcat(name,entry.name());
SD.remove(name);
}
}
return files;
}
void openpic(){
File dir = SD.open("/pictures");
char name[255] = "/pictures/";
int i;
for(i = 0; i < no_off_pic+1;i++){
pic = dir.openNextFile();
}
strcat(name,pic.name());
pic = SD.open(name, FILE_READ);
pic.seek(54);
}
void requestEvent(){
digitalWrite(13, HIGH); // set the LED on
if(pic.position() <= pic.size()){
blue = pic.read();
green = pic.read();
red = pic.read();
color = RGB16(red,green,blue);
color=0x55AA;
Wire.write(lowByte(color));
Wire.write(highByte(color));
}
else{
Serial.println("Error");
}
digitalWrite(13, LOW);
}
void receiveEvent(int numBytes){
int c = Wire.read();
Serial.write(c);
if(c == 2){
no_off_pic++;
if(no_off_pic > no_pics) no_off_pic = 1;
}else if(c == 3){
no_off_pic--;
if(no_off_pic < 1) no_off_pic = no_pics;
}
openpic();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment