Schoolproject, Emulates a QT1070 from a touchberry

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Perijah
Date:
Fri Nov 11 11:57:40 2016 +0000
Parent:
0:b7b55b8a4d2b
Commit message:
Correct keysstates

Changed in this revision

I2C_QT1070.cpp Show annotated file Show diff for this revision Revisions of this file
I2C_QT1070.h Show annotated file Show diff for this revision Revisions of this file
QT1070.cpp Show annotated file Show diff for this revision Revisions of this file
QT1070.h 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
--- a/I2C_QT1070.cpp	Thu Nov 10 13:28:23 2016 +0000
+++ b/I2C_QT1070.cpp	Fri Nov 11 11:57:40 2016 +0000
@@ -12,48 +12,48 @@
 
 void I2cQT1070::check_for_instruction(void)
 {
+    //Checks if the I2Cslave has been adresses with something. If so, we need to
+    //determine what is was for. The switch will do this for us. 
     int adressed = receive();
-    int command = 0;
     switch (adressed) {
+        //In this case, we already know what to send to the master.
         case I2CSlave::ReadAddressed:
             printf("Read addressed \r\n");
-
             send_data_to_master();
             break;
-
+        //In this case, the master is telling the slave what he will want to read
         case I2CSlave::WriteAddressed:
             printf("Write addressed \r\n");
-         // process_instruction();
             save_register_adress();
-           // command = read();
-
             break;
     }
 
 }
 
-void I2cQT1070::save_register_adress(){
+//In case the master wants to read some date, we must first know what it wants to
+//read. save_register_adress() will determine what the master wants to read and 
+//store it in registerAdress.  
+void I2cQT1070::save_register_adress()
+{
     registerAdress = read();
-    printf("This is what the read function goves \r\n");
+    printf("This is what the read function gives \r\n");
     printf("" + registerAdress);
     stop();
 }
 
+//Now we know what the master wants to read, we can send it. Depending on the 
+//value of registerAdress (a.k.a. what the master wants to read) we need to send
+//data to the master.
 void I2cQT1070::send_data_to_master()
 {
-
     switch(registerAdress) {
-    
-
         case CHIP_ID: {
             printf("received CHIP_ID command \r\n");
             char value[1];
             value[0] = Qt1070Chip.getChipID(); 
             write(value, 1);
-            
             break;
         }
-
         case FIRMWARE: {
             printf("received FIRMWARE command \r\n");
             char value2[1];
@@ -72,14 +72,9 @@
             printf("recieved WRITE_DATA command \r\n");
             break;
         }
-
         default: {
             printf("unknown command\n\r");
         }
     }
-
 }
-
-
-
 };
\ No newline at end of file
--- a/I2C_QT1070.h	Thu Nov 10 13:28:23 2016 +0000
+++ b/I2C_QT1070.h	Fri Nov 11 11:57:40 2016 +0000
@@ -5,32 +5,25 @@
 namespace QT1070touchemulator
 {
 
-enum Qt1070Register {
-  CHIP_ID = 0,
-  FIRMWARE = 1,
-  KEY_STATUS = 3,
-  RESET = 57
-};
-
-class I2cQT1070 : public I2CSlave
-{
-
+    enum Qt1070Register {
+        CHIP_ID = 0,
+        FIRMWARE = 1,
+        KEY_STATUS = 3,
+        RESET = 57
+    };
 
-public:
-    I2cQT1070(PinName sda, PinName scl, int address=0xDE, int frequentie = 100);
-    void check_for_instruction(void);
-    void save_register_adress(void);
-private:
-    QT1070 Qt1070Chip;
-    int registerAdress;
-    int read_data(void);
-    void send_data_to_master(void);
-    void process_instruction(void);
-
-
-
-};
-
-
-
+    class I2cQT1070 : public I2CSlave
+    {
+    public:
+        I2cQT1070(PinName sda, PinName scl, int address=0xDE, int frequentie = 100);
+        void check_for_instruction(void);
+        void save_register_adress(void);
+    
+    private:
+        QT1070 Qt1070Chip;
+        int registerAdress;
+        int read_data(void);
+        void send_data_to_master(void);
+        void process_instruction(void);
+    };
 };
\ No newline at end of file
--- a/QT1070.cpp	Thu Nov 10 13:28:23 2016 +0000
+++ b/QT1070.cpp	Fri Nov 11 11:57:40 2016 +0000
@@ -5,65 +5,67 @@
 DigitalIn btnDown(p12); // 31
 DigitalIn btnLeft(p13); // 22
 DigitalIn btnCenter(p14); //14
-
 extern "C" void mbed_reset();
 
-
 namespace QT1070touchemulator
 {
-
-QT1070::QT1070()
-{
-    id_firmware = 0x00;
-    id_chip = 0x01;
-}
-
-char QT1070::getFirmware()
-{
-    return id_firmware;
-}
-
-char QT1070::getChipID()
-{
-    return id_chip;
-}
-
-int QT1070::getKeystate()
-{
-    int createdData = 0x00;
-    if (btnDown==1) {
-        createdData  = createdData | 0x01;
-    } else {
-        createdData= createdData & ~ 0x01;
+    //constructor
+    QT1070::QT1070()
+    {
+        id_firmware = 0x00;
+        id_chip = 0x01;
+    }
+    
+    char QT1070::getFirmware()
+    {
+        return id_firmware;
+    }
+    
+    char QT1070::getChipID()
+    {
+        return id_chip;
     }
-    if (!btnUp) {
-        createdData = createdData | 0x02;
-    } else {
-        createdData= createdData & ~ 0x02;
-    }
-    if (!btnRight) {
-        createdData = createdData | 0x04;
-    } else {
-        createdData= createdData & ~ 0x04;
+    
+    int QT1070::getKeystate()
+    {
+        char createdData = 0x00;
+        if (btnDown==1) {
+            createdData  = createdData | 0x01;
+        } else {
+            createdData= createdData & ~ 0x01;
+        }
+        
+        if (btnUp) {
+            createdData = createdData | 0x02;
+        } else {
+            createdData= createdData & ~ 0x02;
+        }
+        
+        if (btnRight) {
+            createdData = createdData | 0x04;
+        } else {
+            createdData= createdData & ~ 0x04;
+        }
+        
+        if (btnLeft) {
+            createdData = createdData | 0x08;
+        } else {
+            createdData= createdData & ~ 0x08;
+        }
+        
+        if (btnCenter) {
+            createdData  = createdData | 0x10;
+        } else {
+            createdData= createdData & ~ 0x10;
+        }
+    
+        return createdData;
+    
     }
-    if (!btnLeft) {
-        createdData = createdData | 0x08;
-    } else {
-        createdData= createdData & ~ 0x08;
-    }
-    if (!btnCenter) {
-        createdData  = createdData | 0x10;
-    } else {
-        createdData= createdData & ~ 0x10;
+    
+    void QT1070::resetMbed()
+    {
+        mbed_reset();
     }
 
-    return createdData;
-
-}
-
-void QT1070::resetMbed()
-{
-    mbed_reset();
-}
-
 };
--- a/QT1070.h	Thu Nov 10 13:28:23 2016 +0000
+++ b/QT1070.h	Fri Nov 11 11:57:40 2016 +0000
@@ -1,24 +1,20 @@
-//ifndef MEMORY_HEADER
-//define MEMORY_HEADER
 #pragma once
 #include "mbed.h"
-
-
 namespace QT1070touchemulator {
-class QT1070
-{
-private:
-    char id_firmware;
-    char id_chip;
-
-public:
-    QT1070();
-    char getFirmware();
-    char getChipID();
-    int getKeystate();
-    void resetMbed();
-
-
-
-};
+    class QT1070
+    {
+    private:
+        char id_firmware;
+        char id_chip;
+    
+    public:
+        QT1070();
+        char getFirmware();
+        char getChipID();
+        int getKeystate();
+        void resetMbed();
+    
+    
+    
+    };
 };
\ No newline at end of file
--- a/main.cpp	Thu Nov 10 13:28:23 2016 +0000
+++ b/main.cpp	Fri Nov 11 11:57:40 2016 +0000
@@ -1,20 +1,13 @@
 #include "mbed.h"
 #include "I2C_QT1070.h"
 #include "QT1070.h"
-DigitalOut myled(LED1);
-
+DigitalOut led1(LED1);
 Serial pc(USBTX,USBRX);
-
-QT1070touchemulator::QT1070 chiptje;
-//I2cMemoryDevice::I2cMemory i2cMemory (p28,p27,0xAA);
-
+QT1070touchemulator::QT1070 qt1070Chip;
 
 int main()
 {
-
-
     pc.baud(9600);
-    
     QT1070touchemulator::I2cQT1070 i2cQT1070Chip(p28, p27, 0x36, 100000);
     int cycleCounter = 0;
 
@@ -24,53 +17,50 @@
         cycleCounter++;
         if(cycleCounter > 100000) 
         {
-            myled = !myled;
+            led1 = !led1;
             cycleCounter =0;
         }
-
-    };    
-
-
+    };
 
-    //char fw = chiptje.getFirmware();
-//    pc.printf("firmware=%d\r\n", fw);
+//    char fw = qt1070Chip.getFirmware();
+//    pc.printf("firmware=%02x\r\n", fw);
 //
-//    char ci = chiptje.getChipID();
-//    pc.printf("chip id=%d\r\n", ci);
+//    char ci = qt1070Chip.getChipID();
+//    pc.printf("chip id=%02x\r\n", ci);
 //
 //    while(1) {
-//        myled = 1;
+//        led1 = 1;
 //        wait(0.1);
-//        myled = 0;
+//        led1 = 0;
 //
 //
-//        int ks = chiptje.getKeystate();
-//        //pc.printf("keystate=%d\r\n", ks);
+//        int ks = qt1070Chip.getKeystate();
+//       // pc.printf("keystate=%02x\r\n", ks);
 //
-//            if(ks==28){
+//            if(ks & 0x02){
 //                    pc.printf("you pressed UP\r\n");
 //             }
 //
-//            if(ks==26){
+//            if(ks & 0x04){
 //                    pc.printf("you pressed RIGHT\r\n");
 //            }
 //
-//            if(ks==31){
+//            if(ks & 0x01){
 //                    pc.printf("you pressed DOWN\r\n");
 //            }
 //
-//            if(ks==22){
+//            if(ks & 0x08){
 //                    pc.printf("you pressed LEFT\r\n");
 //            }
 //
-//            if(ks==14){
+//            if(ks & 0x10){
 //                    pc.printf("you pressed CENTER\r\n");
-//                    chiptje.resetMbed();
+//                    //qt1070Chip.resetMbed();
 //            }
 //
-//
-//
-//
 //        wait(0.2);
 //    }
+
 }
+
+