![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Any changes are to allow conversion to BMP
Dependencies: BaseJpegDecode Camera_LS_Y201 Motordriver Servo mbed
Fork of Bitmap_copy_copy by
Revision 1:d721e32cf79c, committed 2014-04-30
- Comitter:
- kylepost3
- Date:
- Wed Apr 30 14:19:33 2014 +0000
- Parent:
- 0:ded454e83f81
- Commit message:
- Final design project for ECE 4180. The device takes scans left to right taking pictures (In JPEG), converts the picture to BMP, and scans for red pixels. When the threshold of red pixels is reached, it fires the catapult and reloads.
Changed in this revision
diff -r ded454e83f81 -r d721e32cf79c BaseJpegDecode.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BaseJpegDecode.lib Wed Apr 30 14:19:33 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/kylepost3/code/BaseJpegDecode/#5010d93af0b6
diff -r ded454e83f81 -r d721e32cf79c BitmapFile.cpp --- a/BitmapFile.cpp Wed Sep 08 18:18:42 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,288 +0,0 @@ -#include "BitmapFile.h" - - -BitmapFile::BitmapFile(char* fname) : m_pFile(NULL) -{ - m_fileName = fname; - Initialize(); -} - -BitmapFile::~BitmapFile() -{ - delete[] m_fileName; -} - -bool BitmapFile::Initialize() -{ - bool success = true; - open(); - fread(&BMPHeader,sizeof(BMPHeader),1,m_pFile); - success = (BMPHeader.b == 'B' && BMPHeader.m == 'M'); - - fread(&m_headerlength,sizeof(m_headerlength),1,m_pFile); - fread(&DIBHeader,m_headerlength,1,m_pFile); - - /*Debugging code - - Serial pc2(USBTX,USBRX); - - pc2.printf("\n\rFile = %s", m_fileName); - - pc2.printf("\n\rBMPHeader - Size = %d:\n\r",sizeof(BMPHeader)); - pc2.printf("\tbm:\t\t%c%c\n\r",BMPHeader.b,BMPHeader.m); - pc2.printf("\tfilesize:\t%d\n\r",BMPHeader.filesize); - pc2.printf("\treserved:\t%d,%d\n\r",BMPHeader.reserved1,BMPHeader.reserved2); - pc2.printf("\toffset:\t\t%d\n\r",BMPHeader.offset); - - pc2.printf("\n\rDIBHeader - Size = %d:\n\r",sizeof(DIBHeader)); - pc2.printf("\theaderLength:\t%d\n\r",DIBHeader.headerLength); - pc2.printf("\theight:\t\t\t%d\n\r",DIBHeader.height); - pc2.printf("\twidth:\t\t%d\n\r",DIBHeader.width); - pc2.printf("\tcplanes:\t\t%d\n\r",DIBHeader.cplanes); - pc2.printf("\tcolordepth:\t\t%d\n\r",DIBHeader.colordepth); - pc2.printf("\tcompression:\t%d\n\r",DIBHeader.compression); - pc2.printf("\tdatasize:\t\t%d\n\r",DIBHeader.datasize); - pc2.printf("\tvres:\t\t%d\n\r",DIBHeader.vres); - pc2.printf("\thres:\t\t%d\n\r",DIBHeader.hres); - - - */ - - m_rowsize = 4*((getColorDepth()*getWidth()+31)/32); - - close(); - return success; -} - -void BitmapFile::open() -{ - if(m_pFile==NULL) - { - m_pFile = fopen(m_fileName, "r"); - } -} - -void BitmapFile::close() -{ - if(m_pFile!=NULL) - { - fclose(m_pFile); - m_pFile = NULL; - } -} - -/**********************************************************/ -/*BMP Header Gets */ -/**********************************************************/ - -int BitmapFile::getFileSize() -{ - return BMPHeader.filesize; -} - -int BitmapFile::getReserved1() -{ - return BMPHeader.reserved1; -} - -int BitmapFile::getReserved2() -{ - return BMPHeader.reserved2; -} - -int BitmapFile::getOffset() -{ - return BMPHeader.offset; -} - -/**********************************************************/ -/*DIB Header Gets */ -/**********************************************************/ - -int BitmapFile::getHeaderType() -{ - return m_headerlength; -} - -int BitmapFile::getHeight() -{ - return DIBHeader.height; -} - -int BitmapFile::getWidth() -{ - return DIBHeader.width; -} - -int BitmapFile::getCPlanes() -{ - return DIBHeader.cplanes; -} - -int BitmapFile::getColorDepth() -{ - return DIBHeader.colordepth; -} - -int BitmapFile::getCompression() -{ - return DIBHeader.compression; -} - -int BitmapFile::getDataSize() -{ - return DIBHeader.datasize; -} - -int BitmapFile::getHRes() -{ - return DIBHeader.hres; -} - -int BitmapFile::getVRes() -{ - return DIBHeader.vres; -} - -int BitmapFile::getNumPaletteColors() -{ - return DIBHeader.numpalettecolors; -} - -int BitmapFile::getImportantColors() -{ - return DIBHeader.importantcolors; -} - -/**********************************************************/ -/*Data Gets */ -/**********************************************************/ - -int BitmapFile::getRowSize() -{ - return m_rowsize; -} - -int BitmapFile::getPixel(int row, int col, bool closefile) -{ - int color = -1; - if(row>=0 && row < getHeight() && col>=0 && col< getWidth()) - { - if(getColorDepth() == 24) - { - open(); - color = 0; //make sure the last byte is 00 - - int index = getOffset(); - index += col*3; - index += row*4*ceil(getWidth()*3/4.0); - fseek(m_pFile, index, SEEK_SET); - - fread (&color, 3,1,m_pFile); - - if(closefile) - { - close(); - } - } - } - return color; -} - -int *BitmapFile::getRow(int row, bool closefile) -{ - open(); - int *colors = new int[getWidth()]; - int index = getOffset() + m_rowsize*row; - fseek(m_pFile, index, SEEK_SET); - if(getColorDepth() == 24) - { - for(int i=0; i<getWidth(); i++) - { - fread(&colors[i],3,1,m_pFile); - } - } - else if(getColorDepth() == 1) - { - char *temp = new char[m_rowsize]; - for(int i=0; i<m_rowsize; i++) - { - fread(&temp[i],sizeof(char),1,m_pFile); - } - for(int i=0; i<getWidth(); i++) - { - int byte = i / 8; - int bit = i % 8; - colors[i] = ((temp[byte] << bit) & 0x80) ? 0xFFFFFF : 0x000000; - } - delete [] temp; - } - if(closefile) - { - close(); - } - return colors; -} - -int *BitmapFile::getRowBW(int row, bool closefile) -{ - open(); - int *colors = new int[getWidth()]; - int index = getOffset() + m_rowsize*row; - fseek(m_pFile, index, SEEK_SET); - if(getColorDepth() == 24) - { - for(int i=0; i<getWidth(); i++) - { - char temp[3]; - fread(temp,sizeof(char),3,m_pFile); - int average = (temp[0]+temp[1]+temp[2])/3; - colors[i] = average>128 ? 0xFFFFFF : 0x000000; - } - } - else if(getColorDepth() == 1) - { - delete [] colors; - colors = getRow(row, closefile); - } - if(closefile) - { - close(); - } - return colors; -} - -char *BitmapFile::getRowBitstream(int row, bool closefile) -{ - open(); - int bitsperrow = (getWidth()+7)/8; - char *data = new char[bitsperrow]; - for(int i = 0; i<bitsperrow; i++) - { - data[i] = 0; - } - int index = getOffset() + m_rowsize*row; - fseek(m_pFile, index, SEEK_SET); - - if(getColorDepth() == 24) - { - for(int i=0; i<getWidth(); i++) - { - char temp[3]; - fread(temp,sizeof(char),3,m_pFile); - int average = (temp[0]+temp[1]+temp[2])/3; - int val = average<128?0:1; - data[i/8] |= (val*0x80) >> (i%8); - } - } - else if(getColorDepth() == 1) - { - fread(data,sizeof(char),bitsperrow,m_pFile); - } - - if(closefile) - { - close(); - } - return data; -} \ No newline at end of file
diff -r ded454e83f81 -r d721e32cf79c BitmapFile.h --- a/BitmapFile.h Wed Sep 08 18:18:42 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,158 +0,0 @@ -/**********************************************************/ -/*BitmapFile.h */ -/**********************************************************/ - -#include "mbed.h" - -/* Class: BitmapFile - * A parser for bitmap files. -*/ -class BitmapFile -{ -private: - FILE *m_pFile; - char *m_fileName; - int m_rowsize; - - enum headerType { BITMAPCOREHEADER = 12, - BITMAPCOREHEADER2 = 64, - BITMAPINFOHEADER = 40, - BITMAPV4HEADER = 108, - BITMAPV5HEADER = 124}; - - enum compressType { BI_RGB, - BI_RLE8, - BI_RLE4, - BI_BITFIELDS, - BI_JPEG, - BI_PNG}; - - /* Struct: BMPHeader - * - * The BMP header of the bitmap is read into this. - * - * b - the first byte of the header. Should equal 'B'. - * m - the second byte of the header. Should equal 'M'. - * filesize - the size of the whole file, in bytes. - * reserved 1 and 2 - data specific to the applicaton which created the bitmap. - * offset - the offset at which the actual bitmap begins - */ - - __packed struct - { - char b:8; - char m:8; - int filesize:32; - int reserved1:16; - int reserved2:16; - int offset:32; - - } BMPHeader; - - - /* Struct: DIBHeader - * - * The DIB header of the bitmap is read into this. - * - * headerlength - the length of the header. Should equal 40. - * height - the height of the bitmap. - * width - the width of the bitmap. - * cplanes - the number of color planes. Should equal 1. - * colordepth - the number of bits per pixel. - * compression - the compression method used. - * datasize - the size of the bitmap data, in bytes. - */ - - int m_headerlength; - __packed struct - { - int width:32; - int height:32; - int cplanes:16; - int colordepth:16; - int compression:32; - int datasize:32; - int hres:32; - int vres:32; - int numpalettecolors:32; - int importantcolors:32; - } DIBHeader; - - -public: - /* Constructor: BitmapFile - * Create the BitmapFile class, and call <Initialize> - * - * Parameters: - * fname - The path of the file to open. - */ - BitmapFile(char* fname); - ~BitmapFile(); - - /* Function: Initialize - * Parses the headers of the bitmap. - * - * Returns: - * Whether the bitmap is valid. - */ - bool Initialize(); //parses the header - - /* Function: open - * Opens the bitmap for reading, if not already open. - */ - void open(); - - /* Function: close - * Closes the bitmap. - */ - void close(); - - /***BMP Header gets begin***/ - int getFileSize(); - int getReserved1(); - int getReserved2(); - int getOffset(); - - /***DIB Header gets begin***/ - int getHeaderType(); - int getHeight(); - int getWidth(); - int getCPlanes(); - int getColorDepth(); - int getCompression(); - int getDataSize(); - int getHRes(); - int getVRes(); - int getNumPaletteColors(); - int getImportantColors(); - /****DIB Header gets end****/ - - /******Data gets begin******/ - /* Function: getPixel - * Gets the color of a pixel - * - * Parameters: - * x - The x coordinate of the pixel. - * y - The y coordinate of the pixel. - * closefile - if specified, close the file after reading - * - * Returns: - * the color of the pixel, in hexadecimal. - */ - int getPixel(int x, int y, bool closefile = true); - /* Function: getRow - * Gets the colors of a row - * - * Parameters: - * row - The number of the row.. - * closefile - if specified, close the file after reading - * - * Returns: - * An array of the colors of the pixels, in hexadecimal. - */ - int *getRow(int row, bool closefile = true); - int *getRowBW(int row, bool closefile = true); - char *getRowBitstream(int row, bool closefile = true); - /*******Data gets end*******/ - int getRowSize(); -}; \ No newline at end of file
diff -r ded454e83f81 -r d721e32cf79c Camera_LS_Y201.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Camera_LS_Y201.lib Wed Apr 30 14:19:33 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/kylepost3/code/Camera_LS_Y201/#2eedbc23d0b9
diff -r ded454e83f81 -r d721e32cf79c MobileLCD.cpp --- a/MobileLCD.cpp Wed Sep 08 18:18:42 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,370 +0,0 @@ -/* mbed Library - MobileLCD - * Copyright (c) 2007, sford - */ - -#include "MobileLCD.h" - -#include "mbed.h" - -using namespace mbed; - -MobileLCD::MobileLCD(int mosi, int miso, int clk, int cs, int rst) - : _spi(mosi, miso, clk) - , _rst(rst) - , _cs(cs) { - - _row = 0; - _column = 0; - _rows = 16; - _columns = 16; - _width = 130; - _height = 130; - foreground(0x00FFFFFF); - background(0x00000000); - reset(); -} - -void MobileLCD::reset() { - _cs = 1; - _rst = 0; - _spi.format(9); - _spi.frequency(5000000); - wait(0.001); - _rst = 1; - wait(0.001); - _select(); - command(0xCA); // display control - data(0); - data(32); - data(0); - command(0xBB); - data(1); - - command(0xD1); // oscillator on - command(0x94); // sleep out - command(0x20); // power control - data(0x0F); - - command(0xA7); // invert display - - command(0x81); // Voltage control - data(39); // contrast setting: 0..63 - data(3); // resistance ratio - wait(0.001); - - orientation(); - - // clear screen, including surrounding border - _window(0, 0, 130, 130); - for(int i=0; i<130*130; i++) { - _putp(0x000000); - } - command(0xAF); // turn on the display - - _deselect(); - -} - -void MobileLCD::command(int value) { - _spi.write(value & 0xFF); -} - -void MobileLCD::data(int value) { - _spi.write(value | 0x100); -} - -void MobileLCD::_select() { - _cs = 0; -} - -void MobileLCD::_deselect() { - _cs = 1; -} - -void MobileLCD::_window(int x, int y, int width, int height) { - int x1, x2, y1, y2; - switch(0) { //_rotation) { - default: - case 0: - x1 = x + 2; - y1 = y + 0; - x2 = x1 + width - 1; - y2 = y1 + height - 1; - break; - case 1: - x1 = y + 1; - y1 = x + 1; - x2 = x1 + height - 1; - y2 = y1 + width - 1; - break; - case 2: - x1 = x + 1; - y1 = y + 3; - x2 = x1 + width - 1; - y2 = y1 + height - 1; - break; - case 3: - x1 = y + 3; - y1 = x + 3; - x2 = x1 + height - 1; - y2 = y1 + width - 1; - break; - } - command(0x15); // column - data(x1); - data(x2); - command(0x75); // page - data(y1); - data(y2); - command(0x5C); // start write to ram -} - -void MobileLCD::_putp(int colour) { - int gr = ((colour >> 20) & 0x0F) - | ((colour >> 8 ) & 0xF0); - int nb = ((colour >> 4 ) & 0x0F); - data(nb); - data(gr); -} - -void MobileLCD::orientation() { - int m; - switch(0) { //_rotation) { - case 0: - default: - m = 3; - break; - case 2: - m = 0; - break; - case 1: - m = 5; - break; - case 3: - m = 6; - break; - } - - command(0xBC); // data control - data(m); // scan dirs - data(1); // RGB - data(4); // grayscale -} - - -const unsigned char FONT8x8[97][8] = { -0x08,0x08,0x08,0x00,0x00,0x00,0x00,0x00, // columns, rows, num_bytes_per_char -0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // space 0x20 -0x30,0x78,0x78,0x30,0x30,0x00,0x30,0x00, // ! -0x6C,0x6C,0x6C,0x00,0x00,0x00,0x00,0x00, // " -0x6C,0x6C,0xFE,0x6C,0xFE,0x6C,0x6C,0x00, // # -0x18,0x3E,0x60,0x3C,0x06,0x7C,0x18,0x00, // $ -0x00,0x63,0x66,0x0C,0x18,0x33,0x63,0x00, // % -0x1C,0x36,0x1C,0x3B,0x6E,0x66,0x3B,0x00, // & -0x30,0x30,0x60,0x00,0x00,0x00,0x00,0x00, // ' -0x0C,0x18,0x30,0x30,0x30,0x18,0x0C,0x00, // ( -0x30,0x18,0x0C,0x0C,0x0C,0x18,0x30,0x00, // ) -0x00,0x66,0x3C,0xFF,0x3C,0x66,0x00,0x00, // * -0x00,0x30,0x30,0xFC,0x30,0x30,0x00,0x00, // + -0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x30, // , -0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00, // - -0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00, // . -0x03,0x06,0x0C,0x18,0x30,0x60,0x40,0x00, // / (forward slash) -0x3E,0x63,0x63,0x6B,0x63,0x63,0x3E,0x00, // 0 0x30 -0x18,0x38,0x58,0x18,0x18,0x18,0x7E,0x00, // 1 -0x3C,0x66,0x06,0x1C,0x30,0x66,0x7E,0x00, // 2 -0x3C,0x66,0x06,0x1C,0x06,0x66,0x3C,0x00, // 3 -0x0E,0x1E,0x36,0x66,0x7F,0x06,0x0F,0x00, // 4 -0x7E,0x60,0x7C,0x06,0x06,0x66,0x3C,0x00, // 5 -0x1C,0x30,0x60,0x7C,0x66,0x66,0x3C,0x00, // 6 -0x7E,0x66,0x06,0x0C,0x18,0x18,0x18,0x00, // 7 -0x3C,0x66,0x66,0x3C,0x66,0x66,0x3C,0x00, // 8 -0x3C,0x66,0x66,0x3E,0x06,0x0C,0x38,0x00, // 9 -0x00,0x18,0x18,0x00,0x00,0x18,0x18,0x00, // : -0x00,0x18,0x18,0x00,0x00,0x18,0x18,0x30, // ; -0x0C,0x18,0x30,0x60,0x30,0x18,0x0C,0x00, // < -0x00,0x00,0x7E,0x00,0x00,0x7E,0x00,0x00, // = -0x30,0x18,0x0C,0x06,0x0C,0x18,0x30,0x00, // > -0x3C,0x66,0x06,0x0C,0x18,0x00,0x18,0x00, // ? -0x3E,0x63,0x6F,0x69,0x6F,0x60,0x3E,0x00, // @ 0x40 -0x18,0x3C,0x66,0x66,0x7E,0x66,0x66,0x00, // A -0x7E,0x33,0x33,0x3E,0x33,0x33,0x7E,0x00, // B -0x1E,0x33,0x60,0x60,0x60,0x33,0x1E,0x00, // C -0x7C,0x36,0x33,0x33,0x33,0x36,0x7C,0x00, // D -0x7F,0x31,0x34,0x3C,0x34,0x31,0x7F,0x00, // E -0x7F,0x31,0x34,0x3C,0x34,0x30,0x78,0x00, // F -0x1E,0x33,0x60,0x60,0x67,0x33,0x1F,0x00, // G -0x66,0x66,0x66,0x7E,0x66,0x66,0x66,0x00, // H -0x3C,0x18,0x18,0x18,0x18,0x18,0x3C,0x00, // I -0x0F,0x06,0x06,0x06,0x66,0x66,0x3C,0x00, // J -0x73,0x33,0x36,0x3C,0x36,0x33,0x73,0x00, // K -0x78,0x30,0x30,0x30,0x31,0x33,0x7F,0x00, // L -0x63,0x77,0x7F,0x7F,0x6B,0x63,0x63,0x00, // M -0x63,0x73,0x7B,0x6F,0x67,0x63,0x63,0x00, // N -0x3E,0x63,0x63,0x63,0x63,0x63,0x3E,0x00, // O -0x7E,0x33,0x33,0x3E,0x30,0x30,0x78,0x00, // P 0x50 -0x3C,0x66,0x66,0x66,0x6E,0x3C,0x0E,0x00, // Q -0x7E,0x33,0x33,0x3E,0x36,0x33,0x73,0x00, // R -0x3C,0x66,0x30,0x18,0x0C,0x66,0x3C,0x00, // S -0x7E,0x5A,0x18,0x18,0x18,0x18,0x3C,0x00, // T -0x66,0x66,0x66,0x66,0x66,0x66,0x7E,0x00, // U -0x66,0x66,0x66,0x66,0x66,0x3C,0x18,0x00, // V -0x63,0x63,0x63,0x6B,0x7F,0x77,0x63,0x00, // W -0x63,0x63,0x36,0x1C,0x1C,0x36,0x63,0x00, // X -0x66,0x66,0x66,0x3C,0x18,0x18,0x3C,0x00, // Y -0x7F,0x63,0x46,0x0C,0x19,0x33,0x7F,0x00, // Z -0x3C,0x30,0x30,0x30,0x30,0x30,0x3C,0x00, // [ -0x60,0x30,0x18,0x0C,0x06,0x03,0x01,0x00, // \ (back slash) -0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00, // ] -0x08,0x1C,0x36,0x63,0x00,0x00,0x00,0x00, // ^ -0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF, // _ -0x18,0x18,0x0C,0x00,0x00,0x00,0x00,0x00, // ` 0x60 -0x00,0x00,0x3C,0x06,0x3E,0x66,0x3B,0x00, // a -0x70,0x30,0x3E,0x33,0x33,0x33,0x6E,0x00, // b -0x00,0x00,0x3C,0x66,0x60,0x66,0x3C,0x00, // c -0x0E,0x06,0x3E,0x66,0x66,0x66,0x3B,0x00, // d -0x00,0x00,0x3C,0x66,0x7E,0x60,0x3C,0x00, // e -0x1C,0x36,0x30,0x78,0x30,0x30,0x78,0x00, // f -0x00,0x00,0x3B,0x66,0x66,0x3E,0x06,0x7C, // g -0x70,0x30,0x36,0x3B,0x33,0x33,0x73,0x00, // h -0x18,0x00,0x38,0x18,0x18,0x18,0x3C,0x00, // i -0x06,0x00,0x06,0x06,0x06,0x66,0x66,0x3C, // j -0x70,0x30,0x33,0x36,0x3C,0x36,0x73,0x00, // k -0x38,0x18,0x18,0x18,0x18,0x18,0x3C,0x00, // l -0x00,0x00,0x66,0x7F,0x7F,0x6B,0x63,0x00, // m -0x00,0x00,0x7C,0x66,0x66,0x66,0x66,0x00, // n -0x00,0x00,0x3C,0x66,0x66,0x66,0x3C,0x00, // o -0x00,0x00,0x6E,0x33,0x33,0x3E,0x30,0x78, // p -0x00,0x00,0x3B,0x66,0x66,0x3E,0x06,0x0F, // q -0x00,0x00,0x6E,0x3B,0x33,0x30,0x78,0x00, // r -0x00,0x00,0x3E,0x60,0x3C,0x06,0x7C,0x00, // s -0x08,0x18,0x3E,0x18,0x18,0x1A,0x0C,0x00, // t -0x00,0x00,0x66,0x66,0x66,0x66,0x3B,0x00, // u -0x00,0x00,0x66,0x66,0x66,0x3C,0x18,0x00, // v -0x00,0x00,0x63,0x6B,0x7F,0x7F,0x36,0x00, // w -0x00,0x00,0x63,0x36,0x1C,0x36,0x63,0x00, // x -0x00,0x00,0x66,0x66,0x66,0x3E,0x06,0x7C, // y -0x00,0x00,0x7E,0x4C,0x18,0x32,0x7E,0x00, // z -0x0E,0x18,0x18,0x70,0x18,0x18,0x0E,0x00, // { -0x0C,0x0C,0x0C,0x00,0x0C,0x0C,0x0C,0x00, // | -0x70,0x18,0x18,0x0E,0x18,0x18,0x70,0x00, // } -0x3B,0x6E,0x00,0x00,0x00,0x00,0x00,0x00, // ~ -0x1C,0x36,0x36,0x1C,0x00,0x00,0x00,0x00}; // DEL - -void MobileLCD::locate(int column, int row) { - _row = row; - _column = column; -} - -void MobileLCD::newline() { - _column = 0; - _row++; - if(_row >= _rows) { - _row = 0; - } -} - -int MobileLCD::_putc(int value) { - int x = _column * 8; // FIXME: Char sizes - int y = _row * 8; - bitblit(x + 1, y + 1, 8, 8, (char*)&(FONT8x8[value - 0x1F][0])); - - _column++; - - if(_column >= _columns) { - _row++; - _column = 0; - } - - if(_row >= _rows) { - _row = 0; - } - - return value; -} - -void MobileLCD::cls() { - fill(0, 0, _width, _height, _background); - _row = 0; - _column = 0; -} - -int MobileLCD::width() { - return _width; -} - -int MobileLCD::height() { - return _height; -} - -int MobileLCD::columns() { - return _columns; -} - -int MobileLCD::rows() { - return _rows; -} - -void MobileLCD::window(int x, int y, int width, int height) { - _select(); - _window(x, y, width, height); - _deselect(); -} - -void MobileLCD::putp(int colour) { - _select(); - _putp(colour); - _deselect(); -} - -void MobileLCD::pixel(int x, int y, int colour) { - _select(); - _window(x, y, 1, 1); - _putp(colour); - _deselect(); -} - -void MobileLCD::fill(int x, int y, int width, int height, int colour) { - _select(); - _window(x, y, width, height); - for(int i=0; i<width*height; i++) { - _putp(colour); - } - _window(0, 0, _width, _height); - _deselect(); -} - - -void MobileLCD::blit(int x, int y, int width, int height, const int* colour) { - _select(); - _window(x, y, width, height); - for(int i=0; i<width*height; i++) { - _putp(colour[i]); - } - _window(0, 0, _width, _height); - _deselect(); -} - -void MobileLCD::foreground(int v) { - _foreground = v; -} - -void MobileLCD::background(int v) { - _background = v; -} - -void MobileLCD::bitblit(int x, int y, int width, int height, const char* bitstream) { - _select(); - _window(x, y, width, height); - for(int i=0; i<height*width; i++) { - int byte = i / 8; - int bit = i % 8; - int colour = ((bitstream[byte] << bit) & 0x80) ? _foreground : _background; - _putp(colour); - } - _window(0, 0, _width, _height); - _deselect(); -} - - - \ No newline at end of file
diff -r ded454e83f81 -r d721e32cf79c MobileLCD.h --- a/MobileLCD.h Wed Sep 08 18:18:42 2010 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -/* mbed Library - MobileLCD - * Copyright (c) 2007/8, sford - */ - -#ifndef MBED_MOBILELCD_H -#define MBED_MOBILELCD_H - -#include "mbed.h" - -namespace mbed { - -class MobileLCD : public Stream { - -public: - - MobileLCD(int mosi, int miso, int clk, int cs, int rst); - - virtual void reset(); - virtual void _select(); - virtual void _deselect(); - virtual void _window(int x, int y, int width, int height); - virtual void _putp(int colour); - virtual void orientation(); - - void command(int value); - void data(int value); - void foreground(int v); - void background(int v); - void locate(int column, int row); - void newline(); - virtual int _putc(int c); - virtual int _getc() { return 0; } - SPI _spi; - DigitalOut _rst; - DigitalOut _cs; - void bitblit(int x, int y, int width, int height, const char* bitstream); - void fill(int x, int y, int width, int height, int colour); - void blit(int x, int y, int width, int height, const int* colour); - void cls(); - int width(); - int height(); - int columns(); - int rows(); - void putp(int v); - void window(int x, int y, int width, int height); - void pixel(int x, int y, int colour); - int _row, _column, _rows, _columns, _foreground, _background, _width, _height; -}; - -} - -#endif - -
diff -r ded454e83f81 -r d721e32cf79c Motordriver.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Motordriver.lib Wed Apr 30 14:19:33 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/littlexc/code/Motordriver/#3110b9209d3c
diff -r ded454e83f81 -r d721e32cf79c Servo.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Servo.lib Wed Apr 30 14:19:33 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/simon/code/Servo/#36b69a7ced07
diff -r ded454e83f81 -r d721e32cf79c SimpleJpegDecode/SimpleJpegDecode.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SimpleJpegDecode/SimpleJpegDecode.cpp Wed Apr 30 14:19:33 2014 +0000 @@ -0,0 +1,161 @@ +#include "SimpleJpegDecode.h" + +#define DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);} while(0); +#define ASSERT(A) while(!(A)){fprintf(stderr,"\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; + + +#define _1_4020 45 +#define _0_3441 11 +#define _0_7139 23 +#define _1_7718 57 +#define _0_0012 0 + +int adjust(int r) { + if (r >= 0) { + if (r <= 255) { + return r; + } else { + return 255; + } + } else { + return 0; + } +} + +void convYUVtoRGB(uint8_t rgb[], int y, int u, int v) +{ + rgb[0] = adjust((y*32 + v*_1_4020)/32 + 128); + rgb[1] = adjust((y*32 - u*_0_3441 - v*_0_7139)/32 + 128); + rgb[2] = adjust((y*32 + u*_1_7718 - v*_0_0012)/32 + 128); +} + +SimpleJpegDecode::SimpleJpegDecode(uint8_t output_mode) +{ + m_output_mode = output_mode; + clearOnResult(); +} + + +void SimpleJpegDecode::output(int mcu, int block, int scan, int value) +{ + int sc = (block < yblock) ? 0 : 1; + inputBLOCK(mcu, block, scan, value * qt[sc][scan]); +} + +void SimpleJpegDecode::outputDC(int mcu, int block, int value) +{ + output(mcu, block, 0, value); + DC_count++; +} + +void SimpleJpegDecode::outputAC(int mcu, int block, int scan, int value) +{ + output(mcu, block, scan, value); + AC_count++; +} + +void SimpleJpegDecode::outputMARK(uint8_t c) +{ +} + +void SimpleJpegDecode::format_YUV(int mcu, int block, int8_t* values) +{ + if (block < yblock+1) { + memcpy(m_block_data[block], values, 64); + return; + } + int mcu_x_count = (width+15)/16; + int mcu_x = mcu % mcu_x_count; + int mcu_y = mcu / mcu_x_count; + uint8_t yuv[3]; + if (yblock == 2) { + for(int y = 0; y < 8; y++) { + for(int x = 0; x < 16; x++) { + yuv[0] = m_block_data[x/8][y*8+x%8] + 128; + yuv[1] = m_block_data[2][y*8+x/2] + 128; + yuv[2] = values[y*8+x/2] + 128; + onResult(mcu_x * 16 + x, mcu_y * 8 + y, yuv); + } + } + } else if (yblock == 4) { + for(int y = 0; y < 16; y++) { + for(int x = 0; x < 16; x++) { + yuv[0] = m_block_data[(y/8)*2+x/8][(y%8)*8+x%8] + 128; + yuv[1] = m_block_data[4][(y/2)*8+x/2] + 128; + yuv[2] = values[(y/2)*8+x/2] + 128; + onResult(mcu_x * 16 + x, mcu_y * 16 + y, yuv); + } + } + } else { + ASSERT(yblock == 2 || yblock == 4); + } +} + +void SimpleJpegDecode::format_RGB24(int mcu, int block, int8_t* values) +{ + if (block < yblock+1) { + memcpy(m_block_data[block], values, 64); + return; + } + int mcu_x_count = (width+15)/16; + int mcu_x = mcu % mcu_x_count; + int mcu_y = mcu / mcu_x_count; + uint8_t rgb[3]; + if (yblock == 2) { + for(int y = 0; y < 8; y++) { + for(int x = 0; x < 16; x++) { + int8_t yuv_y = m_block_data[x/8][y*8+x%8]; + int8_t yuv_u = m_block_data[2][y*8+x/2]; + int8_t yuv_v = values[y*8+x/2]; + convYUVtoRGB(rgb, yuv_y, yuv_u, yuv_v); + onResult(mcu_x * 16 + x, mcu_y * 8 + y, rgb); + } + } + } else if (yblock == 4) { + for(int y = 0; y < 16; y++) { + for(int x = 0; x < 16; x++) { + int8_t yuv_y = m_block_data[(y/8)*2+x/8][(y%8)*8+x%8]; + int8_t yuv_u = m_block_data[4][(y/2)*8+x/2]; + int8_t yuv_v = values[(y/2)*8+x/2]; + convYUVtoRGB(rgb, yuv_y, yuv_u, yuv_v); + onResult(mcu_x * 16 + x, mcu_y * 16 + y, rgb); + } + } + } else { + ASSERT(yblock == 2 || yblock == 4); + } +} + +void SimpleJpegDecode::outputBLOCK(int mcu, int block, int8_t* values) +{ + BLOCK_count++; + if (m_output_mode == YUV) { + format_YUV(mcu, block, values); + } else if (m_output_mode == RGB24) { + format_RGB24(mcu, block, values); + } else { + ASSERT(m_output_mode == YUV || m_output_mode == RGB24); + } +} + +void SimpleJpegDecode::onResult(int x, int y, uint8_t* yuv) +{ + if(m_pCbItem && m_pCbMeth) + (m_pCbItem->*m_pCbMeth)(x, y, yuv); + else if(m_pCb) + m_pCb(x, y, yuv); +} + +void SimpleJpegDecode::setOnResult( void (*pMethod)(int, int, uint8_t*) ) +{ + m_pCb = pMethod; + m_pCbItem = NULL; + m_pCbMeth = NULL; +} + +void SimpleJpegDecode::clearOnResult() +{ + m_pCb = NULL; + m_pCbItem = NULL; + m_pCbMeth = NULL; +}
diff -r ded454e83f81 -r d721e32cf79c SimpleJpegDecode/SimpleJpegDecode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SimpleJpegDecode/SimpleJpegDecode.h Wed Apr 30 14:19:33 2014 +0000 @@ -0,0 +1,56 @@ +#ifndef SIMPLE_JPEG_DECODE_H +#define SIMPLE_JPEG_DECODE_H + +#include "BaseJpegDecode.h" +#include "inverseDCT.h" + +#define YUV 0 +#define RGB24 1 + +class SimpleJpegDecode : public BaseJpegDecode, public inverseDCT { +public: + SimpleJpegDecode(uint8_t output_mode=RGB24); + + void format_YUV(int mcu, int block, int8_t* values); + void format_RGB24(int mcu, int block, int8_t* values); + + void output(int mcu, int block, int scan, int value); + virtual void outputDC(int mcu, int block, int value); + virtual void outputAC(int mcu, int block, int scan, int value); + virtual void outputMARK(uint8_t c); + virtual void outputBLOCK(int muc, int block, int8_t* values); // iDCT + + int8_t m_block_data[5][64]; + int DC_count; + int AC_count; + int BLOCK_count; + + ///Setups the result callback + /** + @param pMethod : callback function + */ + void setOnResult( void (*pMethod)(int, int, uint8_t*) ); + + ///Setups the result callback + /** + @param pItem : instance of class on which to execute the callback method + @param pMethod : callback method + */ + class CDummy; + template<class T> + void setOnResult( T* pItem, void (T::*pMethod)(int, int, uint8_t*) ) + { + m_pCb = NULL; + m_pCbItem = (CDummy*) pItem; + m_pCbMeth = (void (CDummy::*)(int, int, uint8_t*)) pMethod; + } + void clearOnResult(); +protected: + void onResult(int x, int y, uint8_t* yuv); + CDummy* m_pCbItem; + void (CDummy::*m_pCbMeth)(int, int, uint8_t*); + void (*m_pCb)(int, int, uint8_t*); + uint8_t m_output_mode; +}; + +#endif // SIMPLE_JPEG_DECODE_H \ No newline at end of file
diff -r ded454e83f81 -r d721e32cf79c bmp24.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bmp24.h Wed Apr 30 14:19:33 2014 +0000 @@ -0,0 +1,66 @@ +#ifndef BMP24_H +#define BMP24_H + +#define BMP24_WIDTH (80) //(16*4) +#define BMP24_HEIGHT (60) //(16*3) + +class bmp24 { + uint8_t m_bitmap[BMP24_WIDTH*BMP24_HEIGHT*3]; +public: + int width; + int height; + + bmp24() { + width = BMP24_WIDTH; + height = BMP24_HEIGHT; + } + + void clear() { + memset(m_bitmap, 0, sizeof(m_bitmap)); + } + + void point(int x, int y, uint8_t* rgb) { + if (x >= 0 && x < width && y >= 0 && y < height) { + int pos = y*width*3+x*3; + m_bitmap[pos++] = rgb[0]; + m_bitmap[pos++] = rgb[1]; + m_bitmap[pos] = rgb[2]; + } + } + + void LE32write(uint8_t* buf, int value) { + *buf++ = value & 0xff; + *buf++ = (value>>8) & 0xff; + *buf++ = (value>>16) & 0xff; + *buf = (value>>24) & 0xff; + } + + bool writeFile(const char *path) { + FILE *fp = fopen(path, "wb"); + if (fp == NULL) { + return false; + } + uint8_t header[] = { +0x42,0x4d,0x36,0xe1,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x00,0x00,0x00,0x28,0x00, +0x00,0x00,0xa0,0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x01,0x00,0x18,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, +0x00,0x00,0x00,0x00,0x00,0x00}; + int file_size = sizeof(header) + sizeof(m_bitmap); + LE32write(header+2, file_size); + LE32write(header+18, width); + LE32write(header+22, height); + + fwrite(header, 1, sizeof(header), fp); + for(int y = height-1; y >=0; y--) { + for(int x = 0; x < width; x++) { + fputc(m_bitmap[y*width*3+x*3+2], fp); + fputc(m_bitmap[y*width*3+x*3+1], fp); + fputc(m_bitmap[y*width*3+x*3+0], fp); + } + } + fclose(fp); + return true; + } +}; + +#endif // BMP24_H
diff -r ded454e83f81 -r d721e32cf79c main.cpp --- 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(); + }
diff -r ded454e83f81 -r d721e32cf79c mbed.bld --- a/mbed.bld Wed Sep 08 18:18:42 2010 +0000 +++ b/mbed.bld Wed Apr 30 14:19:33 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/fcb9359f0959 +http://mbed.org/users/mbed_official/code/mbed/builds/0954ebd79f59 \ No newline at end of file