Binary Calculator code and directories
Dependencies: 4DGL-uLCD-SE DebounceIn PinDetect mbed
Fork of uLCD144G2_demo by
main.cpp
00001 // uLCD-144-G2 demo program for uLCD-4GL LCD driver library 00002 // 00003 #include "mbed.h" 00004 #include "uLCD_4DGL.h" 00005 #include <math.h> 00006 #include <cmath> 00007 #include <bitset> 00008 #include "PinDetect.h" 00009 #include <cmath> 00010 #include <string> 00011 00012 PinDetect pb1(p18); 00013 PinDetect pb2(p17); 00014 PinDetect pb3(p16); 00015 PinDetect pb4(p15); 00016 PinDetect pb5(p23); 00017 PinDetect pb6(p24); 00018 DigitalOut myled(LED1); 00019 DigitalOut myled1(LED2); 00020 DigitalOut myled2(LED3); 00021 DigitalOut myled3(LED4); 00022 uLCD_4DGL uLCD(p28,p27,p30); 00023 DigitalIn dip1(p20); 00024 DigitalIn dip2(p19); 00025 DigitalIn dip3(p21); 00026 DigitalIn dip4(p22); 00027 DigitalIn dip5(p10); 00028 DigitalIn dip6(p11); 00029 DigitalIn dip7(p12); 00030 DigitalIn dip8(p13); 00031 Serial pc(USBTX, USBRX); // tx, rx 00032 00033 int d1; 00034 int d2; 00035 int d3; 00036 int d4; 00037 int out; 00038 void pb1_hit_callback (void) { 00039 bitset<4> foo; 00040 bitset<4> bar; 00041 bitset<4> result; 00042 for(int i = 0; i < 4;i++){ 00043 if(i == 0){ 00044 foo.set(i,dip1); 00045 }else if(i == 1){ 00046 foo.set(i,dip2); 00047 }else if(i == 2){ 00048 foo.set(i,dip3); 00049 }else if(i == 3){ 00050 foo.set(i,dip4); 00051 } 00052 } 00053 for(int i = 0; i < 4;i++){ 00054 if(i == 0){ 00055 bar.set(i,dip5); 00056 }else if(i == 1){ 00057 bar.set(i,dip6); 00058 }else if(i == 2){ 00059 bar.set(i,dip7); 00060 }else if(i == 3){ 00061 bar.set(i,dip8); 00062 } 00063 } 00064 uLCD.cls(); 00065 uLCD.locate(0,0); 00066 string fook = foo.to_string<char,string::traits_type,string::allocator_type>(); 00067 string book = bar.to_string<char,string::traits_type,string::allocator_type>(); 00068 result = foo&=bar; 00069 string rook = result.to_string<char,string::traits_type,string::allocator_type>(); 00070 uLCD.printf("Calculation\n"); 00071 uLCD.printf("%s AND %s\n\n",fook.c_str(),book.c_str()); 00072 uLCD.printf("Result\n"); 00073 uLCD.printf("%s",rook.c_str()); 00074 //uLCD.printf("%d & %d - %d",foo.to_ulong(),bar.to_ulong(),result); 00075 } 00076 00077 void pb2_hit_callback (void) { 00078 bitset<4> foo; 00079 bitset<4> bar; 00080 bitset<4> result; 00081 for(int i = 0; i < 4;i++){ 00082 if(i == 0){ 00083 foo.set(i,dip1); 00084 }else if(i == 1){ 00085 foo.set(i,dip2); 00086 }else if(i == 2){ 00087 foo.set(i,dip3); 00088 }else if(i == 3){ 00089 foo.set(i,dip4); 00090 } 00091 } 00092 for(int i = 0; i < 4;i++){ 00093 if(i == 0){ 00094 bar.set(i,dip5); 00095 }else if(i == 1){ 00096 bar.set(i,dip6); 00097 }else if(i == 2){ 00098 bar.set(i,dip7); 00099 }else if(i == 3){ 00100 bar.set(i,dip8); 00101 } 00102 } 00103 uLCD.cls(); 00104 uLCD.locate(0,0); 00105 string fook = foo.to_string<char,string::traits_type,string::allocator_type>(); 00106 string book = bar.to_string<char,string::traits_type,string::allocator_type>(); 00107 result = foo|=bar; 00108 string rook = result.to_string<char,string::traits_type,string::allocator_type>(); 00109 uLCD.printf("Calculation\n"); 00110 uLCD.printf("%s OR %s\n\n",fook.c_str(),book.c_str()); 00111 uLCD.printf("Result\n"); 00112 uLCD.printf("%s",rook.c_str()); 00113 } 00114 00115 void pb3_hit_callback (void) { 00116 bitset<4> foo; 00117 bitset<4> bar; 00118 bitset<4> result; 00119 for(int i = 0; i < 4;i++){ 00120 if(i == 0){ 00121 foo.set(i,dip1); 00122 }else if(i == 1){ 00123 foo.set(i,dip2); 00124 }else if(i == 2){ 00125 foo.set(i,dip3); 00126 }else if(i == 3){ 00127 foo.set(i,dip4); 00128 } 00129 } 00130 for(int i = 0; i < 4;i++){ 00131 if(i == 0){ 00132 bar.set(i,dip5); 00133 }else if(i == 1){ 00134 bar.set(i,dip6); 00135 }else if(i == 2){ 00136 bar.set(i,dip7); 00137 }else if(i == 3){ 00138 bar.set(i,dip8); 00139 } 00140 } 00141 uLCD.cls(); 00142 uLCD.locate(0,0); 00143 string fook = foo.to_string<char,string::traits_type,string::allocator_type>(); 00144 string book = bar.to_string<char,string::traits_type,string::allocator_type>(); 00145 result = foo|=bar; 00146 result = result.flip(); 00147 string rook = result.to_string<char,string::traits_type,string::allocator_type>(); 00148 uLCD.printf("Calculation\n"); 00149 uLCD.printf("%s NOR %s\n\n",fook.c_str(),book.c_str()); 00150 uLCD.printf("Result\n"); 00151 uLCD.printf("%s",rook.c_str()); 00152 //uLCD.printf("%d",result.to_ulong()); 00153 } 00154 00155 void pb4_hit_callback (void) { 00156 bitset<4> foo; 00157 bitset<4> bar; 00158 bitset<4> result; 00159 for(int i = 0; i < 4;i++){ 00160 if(i == 0){ 00161 foo.set(i,dip1); 00162 }else if(i == 1){ 00163 foo.set(i,dip2); 00164 }else if(i == 2){ 00165 foo.set(i,dip3); 00166 }else if(i == 3){ 00167 foo.set(i,dip4); 00168 } 00169 } 00170 for(int i = 0; i < 4;i++){ 00171 if(i == 0){ 00172 bar.set(i,dip5); 00173 }else if(i == 1){ 00174 bar.set(i,dip6); 00175 }else if(i == 2){ 00176 bar.set(i,dip7); 00177 }else if(i == 3){ 00178 bar.set(i,dip8); 00179 } 00180 } 00181 uLCD.cls(); 00182 uLCD.locate(0,0); 00183 string fook = foo.to_string<char,string::traits_type,string::allocator_type>(); 00184 string book = bar.to_string<char,string::traits_type,string::allocator_type>(); 00185 result = foo&=bar; 00186 result = result.flip(); 00187 string rook = result.to_string<char,string::traits_type,string::allocator_type>(); 00188 uLCD.printf("Calculation\n"); 00189 uLCD.printf("%s NAND %s\n\n",fook.c_str(),book.c_str()); 00190 uLCD.printf("Result\n"); 00191 uLCD.printf("%s",rook.c_str()); 00192 } 00193 00194 void pb5_hit_callback (void) { 00195 bitset<4> foo; 00196 bitset<4> bar; 00197 bitset<4> result; 00198 for(int i = 0; i < 4;i++){ 00199 if(i == 0){ 00200 foo.set(i,dip1); 00201 }else if(i == 1){ 00202 foo.set(i,dip2); 00203 }else if(i == 2){ 00204 foo.set(i,dip3); 00205 }else if(i == 3){ 00206 foo.set(i,dip4); 00207 } 00208 } 00209 for(int i = 0; i < 4;i++){ 00210 if(i == 0){ 00211 bar.set(i,dip5); 00212 }else if(i == 1){ 00213 bar.set(i,dip6); 00214 }else if(i == 2){ 00215 bar.set(i,dip7); 00216 }else if(i == 3){ 00217 bar.set(i,dip8); 00218 } 00219 } 00220 uLCD.cls(); 00221 uLCD.locate(0,0); 00222 int n1 = (int) foo.to_ulong(); 00223 int n2 = (int) bar.to_ulong(); 00224 string fook = foo.to_string<char,string::traits_type,string::allocator_type>(); 00225 string book = bar.to_string<char,string::traits_type,string::allocator_type>(); 00226 uLCD.printf("Calculation\n"); 00227 uLCD.printf("%s + %s\n\n",fook.c_str(),book.c_str()); 00228 uLCD.printf("Result\n"); 00229 bitset<8> res(n1+n2); 00230 string rook = res.to_string<char,string::traits_type,string::allocator_type>(); 00231 uLCD.printf("%s",rook.c_str()); 00232 00233 } 00234 00235 void pb6_hit_callback (void) { 00236 bitset<4> foo; 00237 bitset<4> bar; 00238 bitset<4> result; 00239 for(int i = 0; i < 4;i++){ 00240 if(i == 0){ 00241 foo.set(i,dip1); 00242 }else if(i == 1){ 00243 foo.set(i,dip2); 00244 }else if(i == 2){ 00245 foo.set(i,dip3); 00246 }else if(i == 3){ 00247 foo.set(i,dip4); 00248 } 00249 } 00250 for(int i = 0; i < 4;i++){ 00251 if(i == 0){ 00252 bar.set(i,dip5); 00253 }else if(i == 1){ 00254 bar.set(i,dip6); 00255 }else if(i == 2){ 00256 bar.set(i,dip7); 00257 }else if(i == 3){ 00258 bar.set(i,dip8); 00259 } 00260 } 00261 uLCD.cls(); 00262 uLCD.locate(0,0); 00263 int n1 = (int) foo.to_ulong(); 00264 int n2 = (int) bar.to_ulong(); 00265 string fook = foo.to_string<char,string::traits_type,string::allocator_type>(); 00266 string book = bar.to_string<char,string::traits_type,string::allocator_type>(); 00267 string rook = result.to_string<char,string::traits_type,string::allocator_type>(); 00268 uLCD.printf("Calculation\n"); 00269 uLCD.printf("%s - %s\n\n",fook.c_str(),book.c_str()); 00270 uLCD.printf("Result\n"); 00271 //bitset<8> res(abs (n1-n2)); 00272 //string rok = res.to_string<char,string::traits_type,string::allocator_type>(); 00273 uLCD.printf("%d",abs (n1-n2)); 00274 00275 } 00276 00277 int main() 00278 { 00279 dip1.mode(PullUp); 00280 dip2.mode(PullUp); 00281 dip3.mode(PullUp); 00282 dip4.mode(PullUp); 00283 dip5.mode(PullUp); 00284 dip6.mode(PullUp); 00285 dip7.mode(PullUp); 00286 dip8.mode(PullUp); 00287 pb1.mode(PullUp); 00288 pb2.mode(PullUp); 00289 pb3.mode(PullUp); 00290 pb4.mode(PullUp); 00291 pb5.mode(PullUp); 00292 pb6.mode(PullUp); 00293 pb1.attach_deasserted(&pb1_hit_callback); 00294 // Start sampling pb input using interrupts 00295 pb1.setSampleFrequency(); 00296 pb2.attach_deasserted(&pb2_hit_callback); 00297 pb2.setSampleFrequency(); 00298 pb3.attach_deasserted(&pb3_hit_callback); 00299 pb3.setSampleFrequency(); 00300 pb4.attach_deasserted(&pb4_hit_callback); 00301 pb4.setSampleFrequency(); 00302 pb5.attach_deasserted(&pb5_hit_callback); 00303 pb5.setSampleFrequency(); 00304 pb6.attach_deasserted(&pb6_hit_callback); 00305 pb6.setSampleFrequency(); 00306 wait(1); 00307 while(1) { 00308 00309 00310 00311 00312 } 00313 00314 }
Generated on Thu Jul 14 2022 05:41:32 by 1.7.2