Binary Calculator code and directories

Dependencies:   4DGL-uLCD-SE DebounceIn PinDetect mbed

Fork of uLCD144G2_demo by jim hamblen

Code for binary calculator which calculates AND OR NOR NAND and binary addition and subtraction

Demo video showing the calculator and the functions: /media/uploads/nickw0606/img_0261.mov

Files at this revision

API Documentation at this revision

Comitter:
nickw0606
Date:
Fri Mar 13 07:00:42 2015 +0000
Parent:
8:31e63caf37e2
Commit message:
none;

Changed in this revision

DebounceIn.lib Show annotated file Show diff for this revision Revisions of this file
PinDetect.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 31e63caf37e2 -r a7aa6c9f601a DebounceIn.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebounceIn.lib	Fri Mar 13 07:00:42 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/DebounceIn/#31ae5cfb44a4
diff -r 31e63caf37e2 -r a7aa6c9f601a PinDetect.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PinDetect.lib	Fri Mar 13 07:00:42 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/AjK/code/PinDetect/#cb3afc45028b
diff -r 31e63caf37e2 -r a7aa6c9f601a main.cpp
--- a/main.cpp	Sat Nov 30 02:06:03 2013 +0000
+++ b/main.cpp	Fri Mar 13 07:00:42 2015 +0000
@@ -2,170 +2,313 @@
 //
 #include "mbed.h"
 #include "uLCD_4DGL.h"
+#include <math.h>
+#include <cmath>
+#include <bitset>
+#include "PinDetect.h"
+#include <cmath>
+#include <string>
 
-uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
+PinDetect pb1(p18);
+PinDetect pb2(p17);
+PinDetect pb3(p16);
+PinDetect pb4(p15);
+PinDetect pb5(p23);
+PinDetect pb6(p24);
+DigitalOut myled(LED1);
+DigitalOut myled1(LED2);
+DigitalOut myled2(LED3);
+DigitalOut myled3(LED4);
+uLCD_4DGL uLCD(p28,p27,p30);
+DigitalIn dip1(p20);
+DigitalIn dip2(p19);
+DigitalIn dip3(p21);
+DigitalIn dip4(p22);
+DigitalIn dip5(p10);
+DigitalIn dip6(p11);
+DigitalIn dip7(p12);
+DigitalIn dip8(p13);
+Serial pc(USBTX, USBRX); // tx, rx
+
+int d1;
+int d2;
+int d3;
+int d4;
+int out;
+void pb1_hit_callback (void) {
+    bitset<4> foo;
+    bitset<4> bar;
+    bitset<4> result;
+    for(int i = 0; i < 4;i++){
+          if(i == 0){
+            foo.set(i,dip1); 
+          }else if(i == 1){
+            foo.set(i,dip2); 
+          }else if(i == 2){
+            foo.set(i,dip3); 
+          }else if(i == 3){
+            foo.set(i,dip4); 
+          }
+       }
+       for(int i = 0; i < 4;i++){
+          if(i == 0){
+            bar.set(i,dip5); 
+          }else if(i == 1){
+            bar.set(i,dip6); 
+          }else if(i == 2){
+            bar.set(i,dip7); 
+          }else if(i == 3){
+            bar.set(i,dip8); 
+          }
+       }
+    uLCD.cls();
+    uLCD.locate(0,0);
+    string fook = foo.to_string<char,string::traits_type,string::allocator_type>();
+    string book = bar.to_string<char,string::traits_type,string::allocator_type>();
+    result = foo&=bar;
+    string rook = result.to_string<char,string::traits_type,string::allocator_type>();
+    uLCD.printf("Calculation\n");
+    uLCD.printf("%s AND %s\n\n",fook.c_str(),book.c_str());
+    uLCD.printf("Result\n");
+    uLCD.printf("%s",rook.c_str());
+    //uLCD.printf("%d & %d - %d",foo.to_ulong(),bar.to_ulong(),result);
+}
+
+void pb2_hit_callback (void) {
+    bitset<4> foo;
+    bitset<4> bar;
+    bitset<4> result;
+    for(int i = 0; i < 4;i++){
+          if(i == 0){
+            foo.set(i,dip1); 
+          }else if(i == 1){
+            foo.set(i,dip2); 
+          }else if(i == 2){
+            foo.set(i,dip3); 
+          }else if(i == 3){
+            foo.set(i,dip4); 
+          }
+       }
+       for(int i = 0; i < 4;i++){
+          if(i == 0){
+            bar.set(i,dip5); 
+          }else if(i == 1){
+            bar.set(i,dip6); 
+          }else if(i == 2){
+            bar.set(i,dip7); 
+          }else if(i == 3){
+            bar.set(i,dip8); 
+          }
+       }
+    uLCD.cls();
+    uLCD.locate(0,0);
+    string fook = foo.to_string<char,string::traits_type,string::allocator_type>();
+    string book = bar.to_string<char,string::traits_type,string::allocator_type>();
+    result = foo|=bar;
+    string rook = result.to_string<char,string::traits_type,string::allocator_type>();
+    uLCD.printf("Calculation\n");
+    uLCD.printf("%s OR %s\n\n",fook.c_str(),book.c_str());
+    uLCD.printf("Result\n");
+    uLCD.printf("%s",rook.c_str());
+}
+
+void pb3_hit_callback (void) {
+    bitset<4> foo;
+    bitset<4> bar;
+    bitset<4> result;
+    for(int i = 0; i < 4;i++){
+          if(i == 0){
+            foo.set(i,dip1); 
+          }else if(i == 1){
+            foo.set(i,dip2); 
+          }else if(i == 2){
+            foo.set(i,dip3); 
+          }else if(i == 3){
+            foo.set(i,dip4); 
+          }
+       }
+       for(int i = 0; i < 4;i++){
+          if(i == 0){
+            bar.set(i,dip5); 
+          }else if(i == 1){
+            bar.set(i,dip6); 
+          }else if(i == 2){
+            bar.set(i,dip7); 
+          }else if(i == 3){
+            bar.set(i,dip8); 
+          }
+       }
+    uLCD.cls();
+    uLCD.locate(0,0);
+    string fook = foo.to_string<char,string::traits_type,string::allocator_type>();
+    string book = bar.to_string<char,string::traits_type,string::allocator_type>();
+    result = foo|=bar;
+    result = result.flip();
+    string rook = result.to_string<char,string::traits_type,string::allocator_type>();
+    uLCD.printf("Calculation\n");
+    uLCD.printf("%s NOR %s\n\n",fook.c_str(),book.c_str());
+    uLCD.printf("Result\n");
+    uLCD.printf("%s",rook.c_str());
+    //uLCD.printf("%d",result.to_ulong());
+}
+
+void pb4_hit_callback (void) {
+    bitset<4> foo;
+    bitset<4> bar;
+    bitset<4> result;
+    for(int i = 0; i < 4;i++){
+          if(i == 0){
+            foo.set(i,dip1); 
+          }else if(i == 1){
+            foo.set(i,dip2); 
+          }else if(i == 2){
+            foo.set(i,dip3); 
+          }else if(i == 3){
+            foo.set(i,dip4); 
+          }
+       }
+       for(int i = 0; i < 4;i++){
+          if(i == 0){
+            bar.set(i,dip5); 
+          }else if(i == 1){
+            bar.set(i,dip6); 
+          }else if(i == 2){
+            bar.set(i,dip7); 
+          }else if(i == 3){
+            bar.set(i,dip8); 
+          }
+       }
+    uLCD.cls();
+    uLCD.locate(0,0);
+    string fook = foo.to_string<char,string::traits_type,string::allocator_type>();
+    string book = bar.to_string<char,string::traits_type,string::allocator_type>();
+    result = foo&=bar;
+    result = result.flip(); 
+    string rook = result.to_string<char,string::traits_type,string::allocator_type>();
+    uLCD.printf("Calculation\n");
+    uLCD.printf("%s NAND %s\n\n",fook.c_str(),book.c_str());
+    uLCD.printf("Result\n");
+    uLCD.printf("%s",rook.c_str());
+}
+
+void pb5_hit_callback (void) {
+    bitset<4> foo;
+    bitset<4> bar;
+    bitset<4> result;
+    for(int i = 0; i < 4;i++){
+          if(i == 0){
+            foo.set(i,dip1); 
+          }else if(i == 1){
+            foo.set(i,dip2); 
+          }else if(i == 2){
+            foo.set(i,dip3); 
+          }else if(i == 3){
+            foo.set(i,dip4); 
+          }
+       }
+       for(int i = 0; i < 4;i++){
+          if(i == 0){
+            bar.set(i,dip5); 
+          }else if(i == 1){
+            bar.set(i,dip6); 
+          }else if(i == 2){
+            bar.set(i,dip7); 
+          }else if(i == 3){
+            bar.set(i,dip8); 
+          }
+       }
+    uLCD.cls();
+    uLCD.locate(0,0);
+    int n1 = (int) foo.to_ulong();
+    int n2 = (int) bar.to_ulong();
+    string fook = foo.to_string<char,string::traits_type,string::allocator_type>();
+    string book = bar.to_string<char,string::traits_type,string::allocator_type>();
+    uLCD.printf("Calculation\n");
+    uLCD.printf("%s + %s\n\n",fook.c_str(),book.c_str());
+    uLCD.printf("Result\n");
+    bitset<8>      res(n1+n2);
+    string rook = res.to_string<char,string::traits_type,string::allocator_type>();
+    uLCD.printf("%s",rook.c_str());
+    
+}
+
+void pb6_hit_callback (void) {
+    bitset<4> foo;
+    bitset<4> bar;
+    bitset<4> result;
+    for(int i = 0; i < 4;i++){
+          if(i == 0){
+            foo.set(i,dip1); 
+          }else if(i == 1){
+            foo.set(i,dip2); 
+          }else if(i == 2){
+            foo.set(i,dip3); 
+          }else if(i == 3){
+            foo.set(i,dip4); 
+          }
+       }
+       for(int i = 0; i < 4;i++){
+          if(i == 0){
+            bar.set(i,dip5); 
+          }else if(i == 1){
+            bar.set(i,dip6); 
+          }else if(i == 2){
+            bar.set(i,dip7); 
+          }else if(i == 3){
+            bar.set(i,dip8); 
+          }
+       }
+    uLCD.cls();
+    uLCD.locate(0,0);
+    int n1 = (int) foo.to_ulong();
+    int n2 = (int) bar.to_ulong();
+    string fook = foo.to_string<char,string::traits_type,string::allocator_type>();
+    string book = bar.to_string<char,string::traits_type,string::allocator_type>();
+    string rook = result.to_string<char,string::traits_type,string::allocator_type>();
+    uLCD.printf("Calculation\n");
+    uLCD.printf("%s - %s\n\n",fook.c_str(),book.c_str());
+    uLCD.printf("Result\n");
+    //bitset<8>      res(abs (n1-n2));
+    //string rok = res.to_string<char,string::traits_type,string::allocator_type>();
+    uLCD.printf("%d",abs (n1-n2));
+    
+}
 
 int main()
 {
-    // basic printf demo = 16 by 18 characters on screen
-    uLCD.printf("\nHello uLCD World\n"); //Default Green on black text
-    uLCD.printf("\n  Starting Demo...");
-    uLCD.text_width(4); //4X size text
-    uLCD.text_height(4);
-    uLCD.color(RED);
-    for (int i=10; i>=0; --i) {
-        uLCD.locate(1,2);
-        uLCD.printf("%2D",i);
-        wait(.5);
-    }
-    uLCD.cls();
-    uLCD.printf("Change baudrate......");
-    uLCD.baudrate(3000000); //jack up baud rate to max for fast display
-    //if demo hangs here - try lower baud rates
-    //
-    // printf text only full screen mode demo
-    uLCD.background_color(BLUE);
-    uLCD.cls();
-    uLCD.locate(0,0);
-    uLCD.color(WHITE);
-    uLCD.textbackground_color(BLUE);
-    uLCD.set_font(FONT_7X8);
-    uLCD.text_mode(OPAQUE);
-    int i=0;
-    while(i<64) {
-        if(i%16==0) uLCD.cls();
-        uLCD.printf("TxtLine %2D Page %D\n",i%16,i/16 );
-        i++; //16 lines with 18 charaters per line
-    }
-    wait(0.5);
-    //demo graphics commands
-    uLCD.background_color(BLACK);
-    uLCD.cls();
-    uLCD.background_color(DGREY);
-    uLCD.filled_circle(60, 50, 30, 0xFF00FF);
-    uLCD.triangle(120, 100, 40, 40, 10, 100, 0x0000FF);
-    uLCD.line(0, 0, 80, 60, 0xFF0000);
-    uLCD.filled_rectangle(50, 50, 100, 90, 0x00FF00);
-    uLCD.pixel(60, 60, BLACK);
-    uLCD.read_pixel(120, 70);
-    uLCD.circle(120, 60, 10, BLACK);
-    uLCD.set_font(FONT_7X8);
-    uLCD.text_mode(TRANSPARENT);
-    uLCD.text_bold(ON);
-    uLCD.text_char('B', 9, 8, BLACK);
-    uLCD.text_char('I',10, 8, BLACK);
-    uLCD.text_char('G',11, 8, BLACK);
-    uLCD.text_italic(ON);
-    uLCD.text_string("This is a test of string", 1, 4, FONT_7X8, WHITE);
-    wait(2);
+    dip1.mode(PullUp);
+    dip2.mode(PullUp);
+    dip3.mode(PullUp);
+    dip4.mode(PullUp);
+    dip5.mode(PullUp);
+    dip6.mode(PullUp);
+    dip7.mode(PullUp);
+    dip8.mode(PullUp);
+    pb1.mode(PullUp);
+    pb2.mode(PullUp);
+    pb3.mode(PullUp);
+    pb4.mode(PullUp);
+    pb5.mode(PullUp);
+    pb6.mode(PullUp);
+    pb1.attach_deasserted(&pb1_hit_callback);
+    // Start sampling pb input using interrupts
+    pb1.setSampleFrequency();
+    pb2.attach_deasserted(&pb2_hit_callback);
+    pb2.setSampleFrequency();
+    pb3.attach_deasserted(&pb3_hit_callback);
+    pb3.setSampleFrequency();
+    pb4.attach_deasserted(&pb4_hit_callback);
+    pb4.setSampleFrequency();
+    pb5.attach_deasserted(&pb5_hit_callback);
+    pb5.setSampleFrequency();
+    pb6.attach_deasserted(&pb6_hit_callback);
+    pb6.setSampleFrequency();
+    wait(1);
+    while(1) {
+       
+       
+       
+       
+   }
 
-//Bouncing Ball Demo
-    float fx=50.0,fy=21.0,vx=1.0,vy=0.4;
-    int x=50,y=21,radius=4;
-    uLCD.background_color(BLACK);
-    uLCD.cls();
-    //draw walls
-    uLCD.line(0, 0, 127, 0, WHITE);
-    uLCD.line(127, 0, 127, 127, WHITE);
-    uLCD.line(127, 127, 0, 127, WHITE);
-    uLCD.line(0, 127, 0, 0, WHITE);
-    for (int i=0; i<1500; i++) {
-        //draw ball
-        uLCD.filled_circle(x, y, radius, RED);
-        //bounce off edge walls and slow down a bit?
-        if ((x<=radius+1) || (x>=126-radius)) vx = -.90*vx;
-        if ((y<=radius+1) || (y>=126-radius)) vy = -.90*vy;
-        //erase old ball location
-        uLCD.filled_circle(x, y, radius, BLACK);
-        //move ball
-        fx=fx+vx;
-        fy=fy+vy;
-        x=(int)fx;
-        y=(int)fy;
-    }
-    wait(0.5);
-//draw an image pixel by pixel
-    int pixelcolors[50][50];
-    uLCD.background_color(BLACK);
-    uLCD.cls();
-//compute Mandelbrot set image for display
-//image size in pixels
-    const unsigned ImageHeight=128;
-    const unsigned ImageWidth=128;
-    //"c" region to display
-    double MinRe = -0.75104;
-    double MaxRe = -0.7408;
-    double MinIm = 0.10511;
-    double MaxIm = MinIm+(MaxRe-MinRe)*ImageHeight/ImageWidth;
-    double Re_factor = (MaxRe-MinRe)/(ImageWidth-1);
-    double Im_factor = (MaxIm-MinIm)/(ImageHeight-1);
-    unsigned MaxIterations = 2048;
-    for(unsigned y=0; y<ImageHeight; ++y) {
-        double c_im = MaxIm - y*Im_factor;
-        for(unsigned x=0; x<ImageWidth; ++x) {
-            double c_re = MinRe + x*Re_factor;
-            double Z_re = c_re, Z_im = c_im;
-            int niterations=0;
-            for(unsigned n=0; n<MaxIterations; ++n) {
-                double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
-                if(Z_re2 + Z_im2 > 4) {
-                    niterations = n;
-                    break;
-                }
-                Z_im = 2*Z_re*Z_im + c_im;
-                Z_re = Z_re2 - Z_im2 + c_re;
-            }
-            if (niterations!=(MaxIterations-1))
-                uLCD.pixel(x,y,((niterations & 0xF00)<<12)+((niterations & 0xF0)<<8)+((niterations & 0x0F)<<4) );
-        }
-    }
-    wait(5);
-// PLASMA wave BLIT animation
-//draw an image using BLIT (Block Image Transfer) fastest way to transfer pixel data
-    uLCD.cls();
-    int num_cols=50;
-    int num_rows=50;
-    int frame=0;
-    double a,b,c=0.0;
-    while(frame<75) {
-        for (int k=0; k<num_cols; k++) {
-            b= (1+sin(3.14159*k*0.75/(num_cols-1.0)+c))*0.5;
-            for (int i=0; i<num_rows; i++) {
-                a= (1+sin(3.14159*i*0.75/(num_rows-1.0)+c))*0.5;
-                // a and b will be a sine wave output between 0 and 1
-                // sine wave was scaled for nice effect across array
-                // uses a and b to compute pixel colors based on rol and col location in array
-                // also keeps colors at the same brightness level
-                if ((a+b) <.667)
-                    pixelcolors[i][k] =  (255-(int(254.0*((a+b)/0.667)))<<16) | (int(254.0*((a+b)/0.667))<<8) | 0;
-                else if ((a+b)<1.333)
-                    pixelcolors[i][k] = (0 <<16) | (255-(int (254.0*((a+b-0.667)/0.667)))<<8) | int(254.0*((a+b-0.667)/0.667));
-                else
-                    pixelcolors[i][k] = (int(255*((a+b-1.333)/0.667))<<16) | (0<<8)  | (255-(int (254.0*((a+b-1.333)/0.667))));
-            }
-        }
-        uLCD.BLIT(39, 39, 50, 50, &pixelcolors[0][0]);
-        c = c + 0.0314159*3.0;
-        if (c > 6.2831) c = 0.0;
-        frame++;
-    }
-    //Load Image Demo
-    uLCD.cls();
-    //SD card needed with image and video files for last two demos
-    uLCD.cls();
-    uLCD.media_init();
-    uLCD.printf("\n\nAn SD card is needed for image and video data");
-    uLCD.set_sector_address(0x001D, 0x4C01);
-    uLCD.display_image(0,0);
-    wait(10);
-    //Play video demo
-    while(1) {
-        uLCD.cls();
-        uLCD.media_init();
-        uLCD.set_sector_address(0x001D, 0x4C42);
-        uLCD.display_video(0,0);
-    }
 }
-
-
-