![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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
main.cpp@0:c546b51ecf0b, 2011-10-11 (annotated)
- Committer:
- XkLi
- Date:
- Tue Oct 11 01:24:18 2011 +0000
- Revision:
- 0:c546b51ecf0b
- Child:
- 1:6048138606a0
v1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
XkLi | 0:c546b51ecf0b | 1 | #include "mbed.h" |
XkLi | 0:c546b51ecf0b | 2 | #include "SDFileSystem.h" |
XkLi | 0:c546b51ecf0b | 3 | #include "NokiaLCD.h" |
XkLi | 0:c546b51ecf0b | 4 | #include "myBMP.h" |
XkLi | 0:c546b51ecf0b | 5 | #include "mpr121.h" |
XkLi | 0:c546b51ecf0b | 6 | #include "jpegutil.h" |
XkLi | 0:c546b51ecf0b | 7 | #include <string> |
XkLi | 0:c546b51ecf0b | 8 | #include <cctype> |
XkLi | 0:c546b51ecf0b | 9 | |
XkLi | 0:c546b51ecf0b | 10 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
XkLi | 0:c546b51ecf0b | 11 | NokiaLCD lcd(p11, p13, p14, p15, NokiaLCD::LCD6610); |
XkLi | 0:c546b51ecf0b | 12 | DigitalOut led1(LED1); |
XkLi | 0:c546b51ecf0b | 13 | DigitalOut led2(LED2); |
XkLi | 0:c546b51ecf0b | 14 | DigitalOut led3(LED3); |
XkLi | 0:c546b51ecf0b | 15 | DigitalOut led4(LED4); |
XkLi | 0:c546b51ecf0b | 16 | InterruptIn interrupt(p26); |
XkLi | 0:c546b51ecf0b | 17 | I2C i2c(p9,p10); |
XkLi | 0:c546b51ecf0b | 18 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
XkLi | 0:c546b51ecf0b | 19 | Serial pc(USBTX,USBRX); |
XkLi | 0:c546b51ecf0b | 20 | vector<string> files; |
XkLi | 0:c546b51ecf0b | 21 | string file; |
XkLi | 0:c546b51ecf0b | 22 | int selectpos = 1; |
XkLi | 0:c546b51ecf0b | 23 | int oldvalue; |
XkLi | 0:c546b51ecf0b | 24 | int value; |
XkLi | 0:c546b51ecf0b | 25 | bool showingImage = false; |
XkLi | 0:c546b51ecf0b | 26 | bool displayImage = false; |
XkLi | 0:c546b51ecf0b | 27 | |
XkLi | 0:c546b51ecf0b | 28 | |
XkLi | 0:c546b51ecf0b | 29 | string toLower(string in) |
XkLi | 0:c546b51ecf0b | 30 | { |
XkLi | 0:c546b51ecf0b | 31 | for(int i = 0; i < in.length(); i++) |
XkLi | 0:c546b51ecf0b | 32 | { |
XkLi | 0:c546b51ecf0b | 33 | in[i] = tolower(in[i]); |
XkLi | 0:c546b51ecf0b | 34 | } |
XkLi | 0:c546b51ecf0b | 35 | return in; |
XkLi | 0:c546b51ecf0b | 36 | } |
XkLi | 0:c546b51ecf0b | 37 | |
XkLi | 0:c546b51ecf0b | 38 | void changepos(int val2,int val1){ //compares the current position with the old one andchanges currsor accordingly. |
XkLi | 0:c546b51ecf0b | 39 | //val1 is the lower value ond val2 th higher possibla value |
XkLi | 0:c546b51ecf0b | 40 | |
XkLi | 0:c546b51ecf0b | 41 | if (!showingImage) { |
XkLi | 0:c546b51ecf0b | 42 | if (oldvalue==val1) { |
XkLi | 0:c546b51ecf0b | 43 | if (selectpos != 1) |
XkLi | 0:c546b51ecf0b | 44 | selectpos--; |
XkLi | 0:c546b51ecf0b | 45 | // if (selectpos < 1)selectpos = files.size(); // no more raparround |
XkLi | 0:c546b51ecf0b | 46 | } else if(oldvalue==val2){ |
XkLi | 0:c546b51ecf0b | 47 | if (selectpos != files.size()) |
XkLi | 0:c546b51ecf0b | 48 | selectpos++; |
XkLi | 0:c546b51ecf0b | 49 | //if (selectpos > files.size()) selectpos = 1; // no more raparound |
XkLi | 0:c546b51ecf0b | 50 | } |
XkLi | 0:c546b51ecf0b | 51 | } |
XkLi | 0:c546b51ecf0b | 52 | } |
XkLi | 0:c546b51ecf0b | 53 | |
XkLi | 0:c546b51ecf0b | 54 | void fallInterrupt() { |
XkLi | 0:c546b51ecf0b | 55 | value=mpr121.read(0x00); |
XkLi | 0:c546b51ecf0b | 56 | value +=mpr121.read(0x01)<<8; |
XkLi | 0:c546b51ecf0b | 57 | //pc.printf("%d\r\n",value); |
XkLi | 0:c546b51ecf0b | 58 | switch (value) { |
XkLi | 0:c546b51ecf0b | 59 | case 0: //no button is pressed (good only to execute actions on release) |
XkLi | 0:c546b51ecf0b | 60 | switch (oldvalue) { //if not do action according to button realeased. |
XkLi | 0:c546b51ecf0b | 61 | case (1<<4): |
XkLi | 0:c546b51ecf0b | 62 | if (showingImage) { |
XkLi | 0:c546b51ecf0b | 63 | lcd.background(0xFFFFFF); |
XkLi | 0:c546b51ecf0b | 64 | lcd.foreground(0xFFFFFF); |
XkLi | 0:c546b51ecf0b | 65 | lcd.cls(); |
XkLi | 0:c546b51ecf0b | 66 | showingImage = false; |
XkLi | 0:c546b51ecf0b | 67 | } |
XkLi | 0:c546b51ecf0b | 68 | break; |
XkLi | 0:c546b51ecf0b | 69 | |
XkLi | 0:c546b51ecf0b | 70 | case (1<<5): |
XkLi | 0:c546b51ecf0b | 71 | if (!showingImage) { |
XkLi | 0:c546b51ecf0b | 72 | displayImage = true; |
XkLi | 0:c546b51ecf0b | 73 | showingImage = true; |
XkLi | 0:c546b51ecf0b | 74 | } |
XkLi | 0:c546b51ecf0b | 75 | break; |
XkLi | 0:c546b51ecf0b | 76 | |
XkLi | 0:c546b51ecf0b | 77 | } |
XkLi | 0:c546b51ecf0b | 78 | case 1: |
XkLi | 0:c546b51ecf0b | 79 | changepos(-1,3); //cannot have a lower value as 0 i no button |
XkLi | 0:c546b51ecf0b | 80 | break; |
XkLi | 0:c546b51ecf0b | 81 | case 3: |
XkLi | 0:c546b51ecf0b | 82 | changepos(1,2); |
XkLi | 0:c546b51ecf0b | 83 | break; |
XkLi | 0:c546b51ecf0b | 84 | case 2: |
XkLi | 0:c546b51ecf0b | 85 | changepos((2+1),(2+4)); |
XkLi | 0:c546b51ecf0b | 86 | break; |
XkLi | 0:c546b51ecf0b | 87 | case (2+4): |
XkLi | 0:c546b51ecf0b | 88 | changepos(2,4); |
XkLi | 0:c546b51ecf0b | 89 | break; |
XkLi | 0:c546b51ecf0b | 90 | case 4: |
XkLi | 0:c546b51ecf0b | 91 | changepos((4+2),(4+8)); |
XkLi | 0:c546b51ecf0b | 92 | break; |
XkLi | 0:c546b51ecf0b | 93 | case (4+8): |
XkLi | 0:c546b51ecf0b | 94 | changepos(4,8); |
XkLi | 0:c546b51ecf0b | 95 | break; |
XkLi | 0:c546b51ecf0b | 96 | case 8: |
XkLi | 0:c546b51ecf0b | 97 | changepos(4+8,-1); // cannot have a highe boundry |
XkLi | 0:c546b51ecf0b | 98 | break; |
XkLi | 0:c546b51ecf0b | 99 | |
XkLi | 0:c546b51ecf0b | 100 | default: |
XkLi | 0:c546b51ecf0b | 101 | |
XkLi | 0:c546b51ecf0b | 102 | break; |
XkLi | 0:c546b51ecf0b | 103 | }; |
XkLi | 0:c546b51ecf0b | 104 | |
XkLi | 0:c546b51ecf0b | 105 | oldvalue=value; |
XkLi | 0:c546b51ecf0b | 106 | } |
XkLi | 0:c546b51ecf0b | 107 | |
XkLi | 0:c546b51ecf0b | 108 | void ls(char *dir) |
XkLi | 0:c546b51ecf0b | 109 | { |
XkLi | 0:c546b51ecf0b | 110 | DIR *dp; |
XkLi | 0:c546b51ecf0b | 111 | struct dirent *dirp; |
XkLi | 0:c546b51ecf0b | 112 | dp = opendir(dir); |
XkLi | 0:c546b51ecf0b | 113 | while((dirp = readdir(dp)) != NULL) |
XkLi | 0:c546b51ecf0b | 114 | { |
XkLi | 0:c546b51ecf0b | 115 | files.push_back(string(dirp->d_name)); |
XkLi | 0:c546b51ecf0b | 116 | } |
XkLi | 0:c546b51ecf0b | 117 | } |
XkLi | 0:c546b51ecf0b | 118 | |
XkLi | 0:c546b51ecf0b | 119 | int main() { |
XkLi | 0:c546b51ecf0b | 120 | interrupt.fall(&fallInterrupt); |
XkLi | 0:c546b51ecf0b | 121 | interrupt.mode(PullUp); |
XkLi | 0:c546b51ecf0b | 122 | |
XkLi | 0:c546b51ecf0b | 123 | lcd.background(0xFFFFFF); |
XkLi | 0:c546b51ecf0b | 124 | lcd.foreground(0xFFFFFF); |
XkLi | 0:c546b51ecf0b | 125 | lcd.cls(); |
XkLi | 0:c546b51ecf0b | 126 | ls("/sd"); |
XkLi | 0:c546b51ecf0b | 127 | |
XkLi | 0:c546b51ecf0b | 128 | led1 = 1; |
XkLi | 0:c546b51ecf0b | 129 | |
XkLi | 0:c546b51ecf0b | 130 | while(1) { |
XkLi | 0:c546b51ecf0b | 131 | int ypos = 1; |
XkLi | 0:c546b51ecf0b | 132 | if(displayImage) |
XkLi | 0:c546b51ecf0b | 133 | { |
XkLi | 0:c546b51ecf0b | 134 | __disable_irq(); |
XkLi | 0:c546b51ecf0b | 135 | file = files[selectpos - 1]; |
XkLi | 0:c546b51ecf0b | 136 | string ext = file.substr(file.rfind('.')+1); |
XkLi | 0:c546b51ecf0b | 137 | ext = toLower(ext); |
XkLi | 0:c546b51ecf0b | 138 | if(!ext.compare("bmp")) |
XkLi | 0:c546b51ecf0b | 139 | { |
XkLi | 0:c546b51ecf0b | 140 | RGBApixel *Colors = new RGBApixel [2]; |
XkLi | 0:c546b51ecf0b | 141 | file = "/sd/" + file; |
XkLi | 0:c546b51ecf0b | 142 | lcd.background(0x000000); |
XkLi | 0:c546b51ecf0b | 143 | lcd.foreground(0x000000); |
XkLi | 0:c546b51ecf0b | 144 | lcd.cls(); |
XkLi | 0:c546b51ecf0b | 145 | ReadBMPFromFile(file.c_str(), Colors, &lcd); |
XkLi | 0:c546b51ecf0b | 146 | showingImage = true; |
XkLi | 0:c546b51ecf0b | 147 | } |
XkLi | 0:c546b51ecf0b | 148 | else if(!ext.compare("jpeg") || !ext.compare("jpg")) |
XkLi | 0:c546b51ecf0b | 149 | { |
XkLi | 0:c546b51ecf0b | 150 | file = "/sd/" + file; |
XkLi | 0:c546b51ecf0b | 151 | lcd.background(0x000000); |
XkLi | 0:c546b51ecf0b | 152 | lcd.foreground(0x000000); |
XkLi | 0:c546b51ecf0b | 153 | lcd.cls(); |
XkLi | 0:c546b51ecf0b | 154 | ReadJPEGFromFile(file.c_str(), &lcd); |
XkLi | 0:c546b51ecf0b | 155 | showingImage = true; |
XkLi | 0:c546b51ecf0b | 156 | } |
XkLi | 0:c546b51ecf0b | 157 | else |
XkLi | 0:c546b51ecf0b | 158 | { |
XkLi | 0:c546b51ecf0b | 159 | showingImage = false; |
XkLi | 0:c546b51ecf0b | 160 | } |
XkLi | 0:c546b51ecf0b | 161 | displayImage = false; |
XkLi | 0:c546b51ecf0b | 162 | __enable_irq(); |
XkLi | 0:c546b51ecf0b | 163 | } |
XkLi | 0:c546b51ecf0b | 164 | if(!showingImage) |
XkLi | 0:c546b51ecf0b | 165 | { |
XkLi | 0:c546b51ecf0b | 166 | lcd.background(0xFFFFFF); |
XkLi | 0:c546b51ecf0b | 167 | lcd.locate(0,ypos); |
XkLi | 0:c546b51ecf0b | 168 | for(vector<string>::iterator it=files.begin(); it < files.end(); it++) |
XkLi | 0:c546b51ecf0b | 169 | { |
XkLi | 0:c546b51ecf0b | 170 | if(ypos == selectpos) |
XkLi | 0:c546b51ecf0b | 171 | { |
XkLi | 0:c546b51ecf0b | 172 | lcd.foreground(0xFF0000); |
XkLi | 0:c546b51ecf0b | 173 | } |
XkLi | 0:c546b51ecf0b | 174 | else |
XkLi | 0:c546b51ecf0b | 175 | { |
XkLi | 0:c546b51ecf0b | 176 | lcd.foreground(0x000000); |
XkLi | 0:c546b51ecf0b | 177 | } |
XkLi | 0:c546b51ecf0b | 178 | lcd.printf("%s",(*it).c_str()); |
XkLi | 0:c546b51ecf0b | 179 | ypos++; |
XkLi | 0:c546b51ecf0b | 180 | lcd.locate(0,ypos); |
XkLi | 0:c546b51ecf0b | 181 | } |
XkLi | 0:c546b51ecf0b | 182 | } |
XkLi | 0:c546b51ecf0b | 183 | led1 = !led1; |
XkLi | 0:c546b51ecf0b | 184 | wait(0.05); |
XkLi | 0:c546b51ecf0b | 185 | } |
XkLi | 0:c546b51ecf0b | 186 | } |