This is a code which generates the various zoomed versions of an image stored in an SD card and displays it on a Nokia LCD based on the keys pressed on a capacitive touch pad.

Dependencies:   FatFileSystem mbed

Fork of Lab3 by Martin Sturm

Revision:
1:6048138606a0
Parent:
0:c546b51ecf0b
--- a/main.cpp	Tue Oct 11 01:24:18 2011 +0000
+++ b/main.cpp	Thu Oct 11 06:10:31 2012 +0000
@@ -3,184 +3,116 @@
 #include "NokiaLCD.h"
 #include "myBMP.h"
 #include "mpr121.h"
-#include "jpegutil.h"
 #include <string>
+#include <vector>
 #include <cctype>
 
 SDFileSystem sd(p5, p6, p7, p8, "sd");
 NokiaLCD lcd(p11, p13, p14, p15, NokiaLCD::LCD6610);
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
-InterruptIn interrupt(p26);
 I2C i2c(p9,p10);
-Mpr121 mpr121(&i2c, Mpr121::ADD_VSS);
+Mpr121 touch(&i2c, Mpr121::ADD_VSS);
 Serial pc(USBTX,USBRX);
-vector<string> files;
+InterruptIn interrupt(p26);
+DigitalOut led1(LED1);
+vector<string> f;
 string file;
-int selectpos = 1;
-int oldvalue;
 int value;
-bool showingImage = false;
-bool displayImage = false;
-
+bool  zoom= false;
+bool select_pic[12];
 
-string toLower(string in)
-{
-    for(int i = 0; i < in.length(); i++)
-    {
-        in[i] = tolower(in[i]);
-    }
-    return in;
-}
-
-void changepos(int val2,int val1){ //compares the current position with the old one andchanges currsor accordingly.
-//val1 is the lower value ond val2 th higher possibla value
+void fallInterrupt()
+{  // Interrupt service routine to display the image according to the key pressed
+    value=touch.read(0x00);
+    value +=touch.read(0x01)<<8; // value of the key pressed is assigned to the variable 'value'
 
-if (!showingImage) {
-                if (oldvalue==val1) {
-                    if (selectpos != 1)
-                    selectpos--;
-                    // if (selectpos < 1)selectpos = files.size(); // no more raparround
-                } else if(oldvalue==val2){
-                    if (selectpos != files.size())
-                    selectpos++;
-                    //if (selectpos > files.size()) selectpos = 1; // no more raparound
-                }
-            }
-}
+    pc.printf("%d\r\n",value);
+    switch (value) {
+        case 0:
+            break;
 
-void fallInterrupt() {
-    value=mpr121.read(0x00);
-    value +=mpr121.read(0x01)<<8;
-    //pc.printf("%d\r\n",value);
-    switch (value) {
-        case 0: //no button is pressed (good only to execute actions on release)
-                switch (oldvalue) { //if not do action according to button realeased.
-                    case (1<<4):
-                        if (showingImage) {
-                            lcd.background(0xFFFFFF);
-                            lcd.foreground(0xFFFFFF);
-                            lcd.cls();
-                            showingImage = false;
-                        }
-                        break;
-
-                    case (1<<5):
-                        if (!showingImage) {
-                            displayImage = true;
-                            showingImage = true;
-                        }
-                        break;
-                
-            }
-            case 1:
-            changepos(-1,3); //cannot have a lower value as 0 i no button
+        case 1: {
+            select_pic[0] = true;
+            zoom = true;
+            break;
+        }
+        case 2: {
+            select_pic[3] = true;
+            zoom = true;
+            break;
+        }
+        case 4: {
+            select_pic[5] = true;
+            zoom = true;
             break;
-        case 3:
-            changepos(1,2);
-            break;
-        case 2:
-            changepos((2+1),(2+4));
-            break;
-        case (2+4):
-            changepos(2,4);
+        }
+        case 8: {
+            select_pic[2] = true;
+            zoom = true;
             break;
-        case 4:
-            changepos((4+2),(4+8));
+        }
+        case 16: {
+            select_pic[1] = true;
+            zoom = true;
             break;
-        case (4+8):
-            changepos(4,8);
+        }
+        case 32: {
+            select_pic[4] = true;
+            zoom = true;
             break;
-        case 8:
-            changepos(4+8,-1); // cannot have a highe boundry
-            break;
+        }
 
         default:
 
             break;
-    };
+    }
 
-    oldvalue=value;
 }
 
-void ls(char *dir)
-{
+void open_dir(char *dir)
+{   // Opening the SD card directory and pushing the files into the directory 
     DIR *dp;
     struct dirent *dirp;
     dp = opendir(dir);
-    while((dirp = readdir(dp)) != NULL)
-    {
-        files.push_back(string(dirp->d_name));
+    while((dirp = readdir(dp)) != NULL) {
+        f.push_back(string(dirp->d_name));
     }
 }
 
-int main() {
+int main()
+{
+
     interrupt.fall(&fallInterrupt);
     interrupt.mode(PullUp);
-    
     lcd.background(0xFFFFFF);
     lcd.foreground(0xFFFFFF);
     lcd.cls();
-    ls("/sd");
-    
+    open_dir("/sd");
     led1 = 1;
-    
+
     while(1) {
-        int ypos = 1;
-        if(displayImage)
-        {
+
+        if(zoom) {
             __disable_irq();
-            file = files[selectpos - 1];
-            string ext = file.substr(file.rfind('.')+1);
-            ext = toLower(ext);
-            if(!ext.compare("bmp"))
-            {
-                RGBApixel *Colors = new RGBApixel [2];
-                file = "/sd/" + file;
-                lcd.background(0x000000);
-                lcd.foreground(0x000000);
-                lcd.cls();
-                ReadBMPFromFile(file.c_str(), Colors, &lcd);
-                showingImage = true;
+            for(int i =0; i <f.size(); i++) {
+                if (select_pic[i] == true) {
+                    file = f[i];
+                    select_pic[i] = false;
+                }
             }
-            else if(!ext.compare("jpeg") || !ext.compare("jpg"))
-            {
-                file = "/sd/" + file;
-                lcd.background(0x000000);
-                lcd.foreground(0x000000);
-                lcd.cls();
-                ReadJPEGFromFile(file.c_str(), &lcd);
-                showingImage = true;
-            }
-            else
-            {
-                showingImage = false;
-            }
-            displayImage = false;
+
+            RGBApixel *Colors = new RGBApixel [2];
+            file = "/sd/" + file;
+            lcd.background(0x000000);
+            lcd.foreground(0x000000);
+            lcd.cls();
+            ReadBMPFromFile(file.c_str(), Colors, &lcd); // Function to display image on the nokia LCD. Refer myBMP.cpp for complete functionality.
+
+
+            zoom = false;
             __enable_irq();
         }
-        if(!showingImage)
-        {
-            lcd.background(0xFFFFFF);
-            lcd.locate(0,ypos);
-            for(vector<string>::iterator it=files.begin(); it < files.end(); it++)
-            {
-                if(ypos == selectpos)
-                {
-                    lcd.foreground(0xFF0000);
-                }
-                else
-                {
-                    lcd.foreground(0x000000);
-                }
-                lcd.printf("%s",(*it).c_str());
-                ypos++;
-                lcd.locate(0,ypos);
-            }
-        }
+
         led1 = !led1;
-        wait(0.05);
+        wait(0.2);
     }
 }