Browse files

Merge branch 'master' of github.com:kevincon/picture-portal

  • Loading branch information...
2 parents 353a9dc + 23ce678 commit 56eedc2b704ff59d1868ec230b098d536a45c299 @kevincon committed Apr 29, 2012
View
3 android/AndroidManifest.xml
@@ -7,7 +7,8 @@
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.CAMERA" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
+ <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+ <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
View
108 android/src/com/pictureportal/app/AndroidActivity.java
@@ -29,6 +29,7 @@
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;
private static String img_path;
+ private static String img_name;
private static byte b[];
private static long fsize;
@@ -43,14 +44,13 @@ public void onCreate(Bundle savedInstanceState) {
}
public void takePicture(View v) {
- //TODO
server = ((EditText)findViewById(R.id.IP)).getText().toString();
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
if (fileUri == null) {
- //TODO let user know we can't take a picture because we can't save it
+ // let user know we can't take a picture because we can't save it
Toast.makeText(this, "Sorry, couldn't create a file to save the picture.\n", Toast.LENGTH_SHORT).show();
return;
}
@@ -71,33 +71,21 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
OutputStream outToServer = serverSocket.getOutputStream();
int i;
- //send image size
+ // send image size
File file = new File(img_path);
fsize = file.length();
b = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(fsize).array();
for(i= 0; i < 8; i++) {
outToServer.write(b[i]);
}
- //send image
- FileInputStream fin;
- fin = new FileInputStream(img_path);
- BufferedInputStream bis = new BufferedInputStream(fin);
- DataInputStream dis = new DataInputStream(bis);
-
- try {
- for (i=0; i < fsize; i++) {
- byte buf = dis.readByte();
- outToServer.write(buf);
- }
- }
- catch(Exception e)
- {
- Toast.makeText(this, "Could not send image.\n", Toast.LENGTH_SHORT).show();
- return;
- }
- /*
- //send image location
+ // send image name
+ char iname[] = img_name.toCharArray();
+ for (i=0; i < iname.length; i++){
+ outToServer.write(iname[i]);
+ }
+
+ // send image location
ExifInterface exif = new ExifInterface(img_path);
float[] latlong = new float[2];
char loc[];
@@ -106,69 +94,46 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Address addr = geocoder.getFromLocation(latlong[0], latlong[1], 1).get(0);
loc = addr.getLocality().toCharArray();
}
- else
- loc = "Universe".toCharArray();
- //byte loc_len = (byte)loc.length;
- //outToServer.write(loc_len);
- for (i = 0; i<loc.length; i++){
- //outToServer.write(loc[i]);
- //Log.d("sdfs","asd");
+ else {
+ loc = "Milky Way".toCharArray();
}
- */
+ int loc_len = loc.length;
+ outToServer.write(loc_len);
+ for (i = 0; i<loc_len; i++){
+ outToServer.write(loc[i]);
+ }
+
+ // send image
+ FileInputStream fis = new FileInputStream(img_path);
+ BufferedInputStream in = new BufferedInputStream(fis);
+
+ try {
+ while ((i = in.read()) != -1) {
+ outToServer.write(i);
+ }
+ }
+ catch(Exception e){
+ Toast.makeText(this, "Could not send image.\n", Toast.LENGTH_SHORT).show();
+ return;
+ }
+
serverSocket.close();
} catch (Exception e) {
Toast.makeText(this, "Could not connect to server.\n", Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(this, Long.toString(fsize), Toast.LENGTH_SHORT).show();
- //Toast.makeText(this, "Image saved to:\n" +
- //data.getData(), Toast.LENGTH_SHORT).show();
+
} else if (resultCode == RESULT_CANCELED) {
- //let user know they cancelled the image capture
+ // let user know they canceled the image capture
Toast.makeText(this, "Image capture canceled.\n", Toast.LENGTH_SHORT).show();
} else {
// Image capture failed, advise user
Toast.makeText(this, "Image capture failed\n", Toast.LENGTH_SHORT).show();
}
}
}
-
- // Returns the contents of the file in a byte array.
- public static byte[] getBytesFromFile(File file) throws IOException {
- InputStream is = new FileInputStream(file);
-
- // Get the size of the file
- long length = file.length();
-
- // You cannot create an array using a long type.
- // It needs to be an int type.
- // Before converting to an int type, check
- // to ensure that file is not larger than Integer.MAX_VALUE.
- if (length > Integer.MAX_VALUE) {
- // File is too large
- }
-
- // Create the byte array to hold the data
- byte[] bytes = new byte[(int)length];
-
- // Read in the bytes
- int offset = 0;
- int numRead = 0;
- while (offset < bytes.length
- && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
- offset += numRead;
- }
-
- // Ensure all the bytes have been read in
- if (offset < bytes.length) {
- throw new IOException("Could not completely read file " + file.getName());
- }
-
- // Close the input stream and return bytes
- is.close();
- return bytes;
- }
/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
@@ -206,8 +171,9 @@ private static File getOutputMediaFile(int type){
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE){
- mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg");
- img_path = mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".jpg";
+ img_name = timeStamp + ".jpg";
+ img_path = mediaStorageDir.getPath() + File.separator + "IMG_"+ img_name;
+ mediaFile = new File(img_path);
} else {
return null;
}
View
189 arduino_picture_portal/arduino_picture_portal.ino
@@ -9,7 +9,7 @@
/* University of Pennsylvania */
/* CIS-542 - Embedded Systems Programming (Spring 2012) */
/* Date : March 23, 2011 */
-/* Version : 1.0 */
+/* Version : 1.1 */
/* Hardware : Arduino UNO, 2.8" TFT Touch Screen (ILI9325) */
/* ********************************************************************** */
@@ -51,14 +51,14 @@ char alternate;
/************************* SERIAL *************************/
// PACKET STRUCTURE = (P1) + (P2) + (P3) + (PACKET TYPE) + (DATA) + (CHECKSUM)
-#define USBBAUD 38400 // USBCommunication Baud Rate // 38400 at 500us works well
-#define IMAGE_LOCATION_LENGTH 30 // Size of Image Location Data in bytes
+#define USBBAUD 115200 // USBCommunication Baud Rate // 38400 at 500us works well
+#define IMAGE_LOCATION_LENGTH 19 // Size of Image Location Data in bytes
char location[IMAGE_LOCATION_LENGTH]; // Char array to hold Image Location Data
-typedef struct{ // Struct for holding Image Row Data
- uint16_t row; // Row numbers
- uint16_t imagedata[240]; // Row Data
+typedef struct{ // Struct for holding Image Row Data
+ uint16_t row; // Row numbers
+ uint16_t imagedata[240]; // Row Data
} rowpacket;
-rowpacket imagerowdata; // Struct
+rowpacket imagerowdata; // Struct
#define P1 0xA1 // Preamble Byte 1
#define P2 0xB2 // Preamble Byte 2
#define P3 0xC3 // Preamble Byte 3
@@ -75,7 +75,7 @@ uint8_t tempChar; // Temporary Char Data
uint8_t commandChar; // Command Char Data
uint8_t dataTypeReceived; // Data Type (Image Data, Location Data, Command Data)
uint8_t dataReturn; // Data Read Correctly (1,2,3) or Not yet (0)
-uint16_t rx_index; // Received data index
+uint16_t rx_index; // Received data index
uint8_t CS; // Checksum data
uint8_t receivedCS; // Received Checksum
#define IMAGE_ROW_PACKET_SIZE 482 // Image+Row Struct Size in Bytes
@@ -143,6 +143,53 @@ void PPgetPoint(void){
}
}
+void drawLogoText(void){
+ tft.fillRect(4,244,232, 32, WHITE); // White Background
+ char picportal[15] = {'P','i','c','t','u','r','e',' ','P','o','r','t','a','l','\0'}; // "Picture Portal" Text
+ tft.drawString(43, 254, picportal, BLACK, 2);// Picture Portal Text Render
+ tft.fillCircle(20, 262, 10,BLUE);
+ tft.fillCircle(20, 257, 10,BLUE);
+ tft.fillCircle(20, 262, 7, BLACK);
+ tft.fillCircle(20, 257, 7, BLACK);
+
+ tft.fillCircle(220, 262, 10,ORANGE);
+ tft.fillCircle(220, 257, 10,ORANGE);
+ tft.fillCircle(220, 262, 7, BLACK);
+ tft.fillCircle(220, 257, 7, BLACK);
+
+ tft.drawHorizontalLine(23, 262, 20, BLUE);
+ tft.drawHorizontalLine(22, 260, 21, BLUE);
+ tft.drawHorizontalLine(21, 258, 22, BLUE);
+ tft.drawHorizontalLine(23, 264, 20, BLUE);
+}
+
+void drawLoadingText(void){
+ tft.fillRect(4,244,232, 32, WHITE); // White Background
+ char picportal[12] = {' ',' ',' ',' ','L','O','A','D','I','N','G','\0'}; // "Picture Portal" Text
+ tft.drawString(43, 254, picportal, BLACK, 2);// Picture Portal Text Render
+ tft.fillCircle(20, 262, 10,BLUE);
+ tft.fillCircle(20, 257, 10,BLUE);
+ tft.fillCircle(20, 262, 7, BLACK);
+ tft.fillCircle(20, 257, 7, BLACK);
+
+ tft.fillCircle(220, 262, 10,ORANGE);
+ tft.fillCircle(220, 257, 10,ORANGE);
+ tft.fillCircle(220, 262, 7, BLACK);
+ tft.fillCircle(220, 257, 7, BLACK);
+
+ tft.drawHorizontalLine(23, 262, 68, BLUE);
+ tft.drawHorizontalLine(22, 260, 69, BLUE);
+ tft.drawHorizontalLine(21, 258, 70, BLUE);
+ tft.drawHorizontalLine(23, 264, 68, BLUE);
+}
+
+
+void dispLocation(void){
+ tft.fillRect(4,244,232, 32, WHITE); // White Background
+ tft.drawString(10, 254, location, BLACK, 2);// Picture Portal Text Render
+}
+
+
// USB Cable At Top = X is from Left to Right, Y is from Top to Bottom
void dispImageRow(void){
uint16_t rownum = (uint16_t)imagerowdata.row;
@@ -155,6 +202,18 @@ void dispImageRow(void){
for(uint8_t col = 0; col < 240; col++){
tft.drawPixel((uint16_t)col, rownum,imagerowdata.imagedata[col]);
}
+
+ if(rownum > 0 && canPress){
+ // Blank Screen
+ tft.fillRect(0,0,240,240,BLACK);
+
+ // Gray out buttons
+ tft.fillRect(4,280,114, 36, GRAY);
+ tft.fillRect(122,280,114,36,GRAY);
+ drawLoadingText();
+ canPress = false;
+ }
+
if(rownum < 239){
if(rownum%2 == 0){
tft.drawHorizontalLine(0, rownum+1, 240, BLUE);
@@ -163,7 +222,7 @@ void dispImageRow(void){
}
}
- if(rownum = 239){
+ if(rownum == 239){
// User can press buttons again
canPress = true;
// UN-GRAY OUT BUTTONS
@@ -176,10 +235,10 @@ void dispImageRow(void){
tft.fillRect(122,280,114,36,WHITE); // White Background
tft.fillRect(135,292,55,10, BLACK); // Black Bar
tft.fillTriangle(190,310,190,284,220,297, BLACK); // Black Triangle
+ drawLogoText();
}
}
-
/* ****************************************************************************** */
/* ********************** System Serial Functions *************************** */
/* ****************************************************************************** */
@@ -213,9 +272,6 @@ void sendCommand(uint8_t command){
char receiveData(void){
// Check if found Preamble already
if(dataTypeReceived == 0){
-
- //rx_counter++;
-
// Haven't found data Preamble yet
if(Serial.available() >= 4){
// Enough bytes present for the Preamble
@@ -226,65 +282,31 @@ char receiveData(void){
return 0;
}
}
- //rx_counter = 0;
// Found first byte of Preamble
if(Serial.read() == P2){
if(Serial.read() == P3){
// Found entire preamble, next byte is the packet data size
dataTypeReceived = Serial.read();
- //rx_counter = 0;
}
- }
-
- /*
-
- if(rx_counter > (IMAGE_ROW_PACKET_SIZE+2)){
- dataTypeReceived = 0;
- rx_counter = 0;
- sendCommand(NACK);
- }
- */
-
+ }
}
}
if(dataTypeReceived == IMAGE_ROW_PACKET_TYPE){
// Found Image+Row Packet, read in data
-
- // Debugging
- tft.drawHorizontalLine(0, 240, 240, RED);
rx_counter++;
while(Serial.available() && rx_index<=IMAGE_ROW_PACKET_SIZE){
rx_buf_imagerow[rx_index++] = Serial.read();
}
- //Serial.print("Image row size = ");
- //Serial.println(IMAGE_ROW_PACKET_SIZE);
-
- //Serial.print("Index = ");
- //Serial.println(rx_index);
- //tft.fillRect(300,100,40, 40,BLACK);
- //tester = String(rx_index);
- //tester.toCharArray(strtest, 50);
- //tft.drawString(300, 100, strtest, BLUE, 2);
-
// To exit out if not recieved;
-
- if(rx_counter > (IMAGE_ROW_PACKET_SIZE+2)){
+ if(rx_counter > (60)){ //60 at 57600baud
dataTypeReceived = 0;
rx_counter = 0;
sendCommand(NACK);
}
-
-
-
if(IMAGE_ROW_PACKET_SIZE <= (rx_index - 1)){
-
- // Debugging
- tft.drawHorizontalLine(0, 240, 240, BLACK);
-
// Received the entire message, now check checksum
- //Serial.println("Checking checksum");
receivedCS = dataTypeReceived;
for(int i = 0; i < IMAGE_ROW_PACKET_SIZE; i++){
receivedCS^=rx_buf_imagerow[i];
@@ -296,9 +318,8 @@ char receiveData(void){
dataTypeReceived = 0;
rx_index = 0;
-
- //rx_counter = 0;
// SEND POSITIVE ACKNOWLEDGEMENT
+ Serial.flush();
sendCommand(ACK);
return 1;
@@ -307,17 +328,37 @@ char receiveData(void){
dataTypeReceived = 0;
rx_index = 0;
-
- //rx_counter = 0;
// SEND NEGATIVE ACKNOWLEDGEMENT
+ Serial.flush();
sendCommand(NACK);
return 0;
}
}
}else if(dataTypeReceived == LOCATION_PACKET_TYPE){
// Found Location Packet, read in data
-
-
+ while(Serial.available() && rx_index<=IMAGE_LOCATION_LENGTH){
+ rx_buf_location[rx_index++] = Serial.read();
+ }
+ if(IMAGE_LOCATION_LENGTH <= (rx_index - 1)){
+ // Received the entire message, now check checksum
+ receivedCS = dataTypeReceived;
+ for(int i = 0; i < IMAGE_LOCATION_LENGTH; i++){
+ receivedCS^=rx_buf_location[i];
+ }
+
+ if(receivedCS == rx_buf_location[rx_index-1]){
+ // Found correct packet!
+ memcpy(&location, rx_buf_location, IMAGE_LOCATION_LENGTH);
+ dataTypeReceived = 0;
+ rx_index = 0;
+ return 2;
+ }else{
+ // Incorrect packet!
+ dataTypeReceived = 0;
+ rx_index = 0;
+ return 0;
+ }
+ }
}else if(dataTypeReceived == COMMAND_PACKET_TYPE){
// Found Command Packet, read in data
commandChar = Serial.read();
@@ -328,11 +369,9 @@ char receiveData(void){
rx_index = 0;
if(receivedCS == CS){
// Correct Packet Found
- //sendCommand(ACK);
return 3;
}else{
// Incorrect Packet Found
- //sendCommand(NACK);
return 0;
}
}else{
@@ -361,31 +400,14 @@ void setup(void) {
WIDTH = tft.width();
HEIGHT = tft.height();
- // Text Area
- tft.fillRect(4,244,232, 32, WHITE); // White Background
- char picportal[15] = {'P','i','c','t','u','r','e',' ','P','o','r','t','a','l','\0'}; // "Picture Portal" Text
- tft.drawString(43, 254, picportal, BLACK, 2);// Picture Portal Text Render
- tft.fillCircle(20, 262, 10,BLUE);
- tft.fillCircle(20, 257, 10,BLUE);
- tft.fillCircle(20, 262, 7, BLACK);
- tft.fillCircle(20, 257, 7, BLACK);
-
- tft.fillCircle(220, 262, 10,ORANGE);
- tft.fillCircle(220, 257, 10,ORANGE);
- tft.fillCircle(220, 262, 7, BLACK);
- tft.fillCircle(220, 257, 7, BLACK);
-
- tft.drawHorizontalLine(23, 262, 20, BLUE);
- tft.drawHorizontalLine(22, 260, 21, BLUE);
- tft.drawHorizontalLine(21, 258, 22, BLUE);
- tft.drawHorizontalLine(23, 264, 20, BLUE);
+ // Logo
+ drawLogoText();
// Left Button
tft.fillRect(4,280,114, 36, WHITE); // White Background
tft.fillRect(49,292,55,10, BLACK); // Black Bar
tft.fillTriangle(50,310,50,284,20,297, BLACK); // Black Triangle
-
// Right Button
tft.fillRect(122,280,114,36,WHITE); // White Background
tft.fillRect(135,292,55,10, BLACK); // Black Bar
@@ -397,24 +419,18 @@ void setup(void) {
/* **************************** Main Program ************************************ */
/* ****************************************************************************** */
void loop()
-{
- //delay(LOOPDELAY);
-
+{
// Check for Data
dataReturn = receiveData();
if(dataReturn == 1){ // Received Image Data
dispImageRow();
}else if(dataReturn == 2){ // Received Location Data
-
+ dispLocation();
}else if(dataReturn == 3){ // Received Command Data
//Serial.print("Received DATA!");
if(commandChar == ACK){
- //tft.fillRect(0,0,BOXSIZE, BOXSIZE,GREEN);
- }
- if(commandChar == NACK){
- //tft.fillRect(0,0,BOXSIZE, BOXSIZE, RED);
- }
-
+ sendCommand(ACK);
+ }
}
// Check for Button Pressed
@@ -436,6 +452,7 @@ void loop()
// Gray Out Buttons
tft.fillRect(4,280,114, 36, GRAY);
tft.fillRect(122,280,114,36,GRAY);
+ drawLoadingText();
}
}
}

0 comments on commit 56eedc2

Please sign in to comment.