Any changes are to allow conversion to BMP

Dependencies:   BaseJpegDecode Camera_LS_Y201 Motordriver Servo mbed

Fork of Bitmap_copy_copy by Eric Wieser

Revision:
1:d721e32cf79c
Parent:
0:ded454e83f81
--- a/main.cpp	Wed Sep 08 18:18:42 2010 +0000
+++ b/main.cpp	Wed Apr 30 14:19:33 2014 +0000
@@ -1,46 +1,326 @@
 #include "mbed.h"
-#include "BitmapFile.h"
-#include "MobileLCD.h"
+#include "Camera_LS_Y201.h"
+#include "SimpleJpegDecode.h"
+#include "bmp24.h"
+#include "Servo.h"
+#include "motordriver.h"
+
+LocalFileSystem fs("local");
+ 
+#define DEBMSG      printf
+#define NEWLINE()   printf("\r\n")
+ 
+#define FILENAME    "/local/IMG_%04d.jpg"
+
+#define INPUT_FILE  "/local/IMG_0000.jpg"
+#define OUTPUT_FILE "/local/IMG_0000.bmp"
+
+Camera_LS_Y201 cam1(p13, p14);
+
+
+SimpleJpegDecode decode;
+
+bmp24 bmp;
+ 
+typedef struct work {
+    FILE *fp;
+} work_t;
+ 
+work_t work;
+
+Motor m(p21, p19, p20, 1);
+Servo Panning(p22);
+Servo Latching(p23);
+
+
+
+void Latch()
+{
+    Latching = 0.4;
+    wait(2);
+}
+
+void Fire()
+{
+    Latching = 0.001;
+    wait(2);
+}
+
+void dcreload()
+{
+    for (float s= 0; s < 1.0 ; s += 0.1) {
+        m.speed(s); // functions in libarary on https://mbed.org/cookbook/Motor
+        wait(0.22);
+    }
+    for (float s= 1; s > 0 ; s -= 0.1) {
+        m.speed(s); // functions in libarary on https://mbed.org/cookbook/Motor
+        wait(0.22);
+    }    
+    m.stop(0.5);
+}
+void dcrelease()
+{
+    for (float s= 0; s > -1.0 ; s -= 0.1) {
+        m.speed(s); // functions in libarary on https://mbed.org/cookbook/Motor
+        wait(0.15);
+    }
+    for (float s= -1; s < 0 ; s += 0.1) {
+        m.speed(s); // functions in libarary on https://mbed.org/cookbook/Motor
+        wait(0.14);
+    }    
+    m.stop(0.5);
+}
+
+
+void callbackRGB(int x, int y, uint8_t* rgb)
+{
+    bmp.point(x, y, rgb);
+}
+
+void convert_to_bmp(){
+    printf("%s\r\n", __FILE__);
+
+    bmp.clear();
+    decode.setOnResult(callbackRGB);
+    decode.clear();
+    //printf("input: %s\r\n", INPUT_FILE);
+    FILE* fp = fopen(INPUT_FILE, "rb");
+    if (fp == NULL) {
+         error("open error\r\n");
+    }
+    while(!feof(fp)) {
+        int c = fgetc(fp);
+        decode.input(c);
+    }
+    fclose(fp);
+    //printf("output: %s\r\n", OUTPUT_FILE);
+    if (!bmp.writeFile(OUTPUT_FILE)) {
+        error("write error\r\n");
+    }
+}    
+
 
-LocalFileSystem local("local");
-MobileLCD lcd(5, 6, 7, 8, 9);
-Serial pc(USBTX,USBRX);
+ 
+/**
+ * Callback function for readJpegFileContent.
+ *
+ * @param buf A pointer to a buffer.
+ * @param siz A size of the buffer.
+ */
+void callback_func(int done, int total, uint8_t *buf, size_t siz) {
+    fwrite(buf, siz, 1, work.fp);
+ 
+    static int n = 0;
+    int tmp = done * 100 / total;
+    if (n != tmp) {
+        n = tmp;
+        //DEBMSG("Writing...: %3d%%", n);
+        //NEWLINE();
+    }
+}
+ 
+/**
+ * Capture.
+ *
+ * @param cam A pointer to a camera object.
+ * @param filename The file name.
+ *
+ * @return Return 0 if it succeed.
+ */
+int capture(Camera_LS_Y201 *cam, char *filename) {
+    /*
+     * Take a picture.
+     */
+    if (cam->takePicture() != 0) {
+        return -1;
+    }
+    //DEBMSG("Captured.");
+    //NEWLINE();
+ 
+    /*
+     * Open file.
+     */
+    work.fp = fopen(filename, "wb");
+    if (work.fp == NULL) {
+        return -2;
+    }
+ 
+    /*
+     * Read the content.
+     */
+    //DEBMSG("%s", filename);
+   // NEWLINE();
+    if (cam->readJpegFileContent(callback_func) != 0) {
+        fclose(work.fp);
+        return -3;
+    }
+    fclose(work.fp);
+ 
+    /*
+     * Stop taking pictures.
+     */
+    cam->stopTakingPictures();
+ 
+    return 0;
+}
+ 
+ 
+int read_picture()
+{
+	 // super-simplified BMP read algorithm to pull out RGB data
+	 // read image for coloring scheme
+	 
+	 int width = 64;	 
+	 int height = 48;
+
+	 //char image[height][width][3]; // first number here is 1024 pixels in my image, 3 is for RGB values
+	 FILE *streamIn;
+	 streamIn = fopen("/local/IMG_0000.bmp", "r");
+	 if (streamIn == (FILE *)0){
+	   printf("File opening error ocurred. Exiting program.\n");
+	   exit(0);
+	 }
+	 int i = 0;
+	 int byte = 0;
+	 int count = 0;
+
+     int red = 0;
+     int green = 0;
+     int blue = 0;
+	 
+	 int left = 0;
+	 int middle = 0;
+	 int right = 0;
+	 
+	 int sum = 0;
+	 
+	 
+	 for(i=0;i<54;i++) byte = getc(streamIn);  // strip out BMP header THIS IS THE KEY
+	
+	 for(int y=0;y<height;y++){
+ 		 for(int x=0;x<width;x++){
+		    
+		    blue = getc(streamIn);  // use BMP 24bit with no alpha channel
+		    green = getc(streamIn);  // BMP uses BGR but we want RGB, grab byte-by-byte
+		    red = getc(streamIn);  // reverse-order array indexing fixes RGB issue...
+		    		    
+			
+			if ((red > 150) && (green < 150) && (blue < 150)) // IS the pixel red??
+			{
+		    	//printf("red ");
+		    	if (x < 21) left++;
+		    	else if ((x > 21) && (x < 42)) middle++;
+		    	else right++;	 
+	    	}  
+	    	count++; 
+		 }
+	 }
+
+	fclose(streamIn);
+	
+	sum = left + right + middle;
+
+	printf("Total: %i\r\n", sum);
+
+	return sum;
+	
+}
+
+void camera_init(){
+	   
+  	printf("\r\n\n\n");
+    cam1.setImageSize(ImageSize160x120);
+    DEBMSG("Camera module");
+    NEWLINE();
+    DEBMSG("Resetting...");
+    NEWLINE();
+    wait(1);
+ 
+    if (cam1.reset() == 0) {
+        DEBMSG("Reset OK.");
+        NEWLINE();
+    } else {
+        DEBMSG("Reset fail.");
+        NEWLINE();
+        error("Reset fail.");
+    }
+    //wait(1);
+}
+
+void take_picture(){
+    char fname[64];
+	int cnt = 0;    
+    snprintf(fname, sizeof(fname) - 1, FILENAME, cnt);
+    int r = capture(&cam1, fname);
+    if (r == 0) {
+        ;
+        //DEBMSG("[%04d]:OK.", cnt);
+        //NEWLINE();
+    } else {
+        DEBMSG("[%04d]:NG. (code=%d)", cnt, r);
+        NEWLINE();
+        error("Failure.");
+	}	
+}
+
 
 int main()
 {
-    BitmapFile MyBitmap("/local/mbed.bmp");
-    int x = 3;
-    int y = 3;
-    unsigned int color = MyBitmap.getPixel(x,y);
-    lcd.background(0xFF0000);
-    lcd.cls();
-    lcd.printf("Offset = %X",MyBitmap.getOffset());
-    lcd.newline();
-    lcd.printf("Header = %d",MyBitmap.getHeaderType());
-    lcd.newline();
-    lcd.printf("Color depth = %d",MyBitmap.getColorDepth());
-    lcd.newline();
-    lcd.printf("Size = %d x %d",MyBitmap.getWidth(),MyBitmap.getHeight());
-    lcd.newline();
-    lcd.printf("Rowlength = %d",MyBitmap.getRowSize());
-    lcd.newline();
-    lcd.printf("(%d,%d) = %06X",x,y,MyBitmap.getPixel(x,y));//<----
-    lcd.newline();
-    wait(3);
-    lcd.background(0x8f8f8f);
-    lcd.cls();
-	lcd.background(0x000000);
-	lcd.cls();
-	
-	for(int row = 0; row < MyBitmap.getHeight(); row++)
-	{
+	int sum = 0;
+ 	camera_init();
+
+	int fired = 0;
+
+	Latch();
+
+
+	while(1){
+		fired = 0;
+		sum = 0;
+		for(float i = 0.1; i< .9; i+= .10){ //Scans left to right
+	        Panning = i;
+	        wait(.01);
+	        
+	     	take_picture();
+			convert_to_bmp();   
+			sum = read_picture(); // Returns total number of red pixels
+	     	printf("sum that matters: %i\r\n", sum);
+	     	if (sum > 400){ // If the number of red pixels is above the threshold: Fire and reload
+				i-=.1;
+				Panning = i;
+				Fire();
+				dcreload();
+				Latch();
+				dcrelease();
+				fired = 1;
+				break;
+			}   
+   		}
 		
-		//int *colors = MyBitmap.getRow(row,false);
-		//lcd.blit(0,MyBitmap.getHeight()-row-1,MyBitmap.getWidth(),1,colors);
-		//delete [] colors;
-		char *bitstream = MyBitmap.getRowBitstream(row,false);
-		lcd.bitblit(0,MyBitmap.getHeight()-row-1,MyBitmap.getWidth(),1,bitstream);
-		delete [] bitstream;
+		
+		
+		for(float i = 0.9; i> .1; i-= .10){ //Scans right to left
+	        if (fired == 1)
+	        	break;
+	        	
+	        Panning = i;
+	        wait(.01);
+	        
+	     	take_picture();
+			convert_to_bmp();   
+			sum = read_picture();   // Returns total number of red pixels
+	     	
+	     	if (sum > 400){ // If the number of red pixels is above the threshold: Fire and reload
+				i+=.1;
+				Panning = i;
+				Fire();
+				dcreload();
+				Latch();
+				dcrelease();
+				break;
+			}   
+   		}		
+			   
 	}
-	MyBitmap.close();
+
 }