Schoolproject, Emulates a QT1070 from a touchberry

Dependencies:   mbed

Committer:
Perijah
Date:
Fri Nov 11 11:57:40 2016 +0000
Revision:
1:f4caacc4df1b
Parent:
0:b7b55b8a4d2b
Correct keysstates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Perijah 0:b7b55b8a4d2b 1 #include "I2C_QT1070.h"
Perijah 0:b7b55b8a4d2b 2
Perijah 0:b7b55b8a4d2b 3 namespace QT1070touchemulator
Perijah 0:b7b55b8a4d2b 4 {
Perijah 0:b7b55b8a4d2b 5
Perijah 0:b7b55b8a4d2b 6 I2cQT1070::I2cQT1070(PinName sda, PinName scl, int address, int frequentie):I2CSlave(sda, scl)
Perijah 0:b7b55b8a4d2b 7 {
Perijah 0:b7b55b8a4d2b 8 this->address(address);
Perijah 0:b7b55b8a4d2b 9 int registerAdress = 0;
Perijah 0:b7b55b8a4d2b 10 frequency(frequentie);
Perijah 0:b7b55b8a4d2b 11 }
Perijah 0:b7b55b8a4d2b 12
Perijah 0:b7b55b8a4d2b 13 void I2cQT1070::check_for_instruction(void)
Perijah 0:b7b55b8a4d2b 14 {
Perijah 1:f4caacc4df1b 15 //Checks if the I2Cslave has been adresses with something. If so, we need to
Perijah 1:f4caacc4df1b 16 //determine what is was for. The switch will do this for us.
Perijah 0:b7b55b8a4d2b 17 int adressed = receive();
Perijah 0:b7b55b8a4d2b 18 switch (adressed) {
Perijah 1:f4caacc4df1b 19 //In this case, we already know what to send to the master.
Perijah 0:b7b55b8a4d2b 20 case I2CSlave::ReadAddressed:
Perijah 0:b7b55b8a4d2b 21 printf("Read addressed \r\n");
Perijah 0:b7b55b8a4d2b 22 send_data_to_master();
Perijah 0:b7b55b8a4d2b 23 break;
Perijah 1:f4caacc4df1b 24 //In this case, the master is telling the slave what he will want to read
Perijah 0:b7b55b8a4d2b 25 case I2CSlave::WriteAddressed:
Perijah 0:b7b55b8a4d2b 26 printf("Write addressed \r\n");
Perijah 0:b7b55b8a4d2b 27 save_register_adress();
Perijah 0:b7b55b8a4d2b 28 break;
Perijah 0:b7b55b8a4d2b 29 }
Perijah 0:b7b55b8a4d2b 30
Perijah 0:b7b55b8a4d2b 31 }
Perijah 0:b7b55b8a4d2b 32
Perijah 1:f4caacc4df1b 33 //In case the master wants to read some date, we must first know what it wants to
Perijah 1:f4caacc4df1b 34 //read. save_register_adress() will determine what the master wants to read and
Perijah 1:f4caacc4df1b 35 //store it in registerAdress.
Perijah 1:f4caacc4df1b 36 void I2cQT1070::save_register_adress()
Perijah 1:f4caacc4df1b 37 {
Perijah 0:b7b55b8a4d2b 38 registerAdress = read();
Perijah 1:f4caacc4df1b 39 printf("This is what the read function gives \r\n");
Perijah 0:b7b55b8a4d2b 40 printf("" + registerAdress);
Perijah 0:b7b55b8a4d2b 41 stop();
Perijah 0:b7b55b8a4d2b 42 }
Perijah 0:b7b55b8a4d2b 43
Perijah 1:f4caacc4df1b 44 //Now we know what the master wants to read, we can send it. Depending on the
Perijah 1:f4caacc4df1b 45 //value of registerAdress (a.k.a. what the master wants to read) we need to send
Perijah 1:f4caacc4df1b 46 //data to the master.
Perijah 0:b7b55b8a4d2b 47 void I2cQT1070::send_data_to_master()
Perijah 0:b7b55b8a4d2b 48 {
Perijah 0:b7b55b8a4d2b 49 switch(registerAdress) {
Perijah 0:b7b55b8a4d2b 50 case CHIP_ID: {
Perijah 0:b7b55b8a4d2b 51 printf("received CHIP_ID command \r\n");
Perijah 0:b7b55b8a4d2b 52 char value[1];
Perijah 0:b7b55b8a4d2b 53 value[0] = Qt1070Chip.getChipID();
Perijah 0:b7b55b8a4d2b 54 write(value, 1);
Perijah 0:b7b55b8a4d2b 55 break;
Perijah 0:b7b55b8a4d2b 56 }
Perijah 0:b7b55b8a4d2b 57 case FIRMWARE: {
Perijah 0:b7b55b8a4d2b 58 printf("received FIRMWARE command \r\n");
Perijah 0:b7b55b8a4d2b 59 char value2[1];
Perijah 0:b7b55b8a4d2b 60 value2[0] = Qt1070Chip.getFirmware();
Perijah 0:b7b55b8a4d2b 61 write(value2, 1);
Perijah 0:b7b55b8a4d2b 62 break;
Perijah 0:b7b55b8a4d2b 63 }
Perijah 0:b7b55b8a4d2b 64 case KEY_STATUS: {
Perijah 0:b7b55b8a4d2b 65 printf("received KEY_STATUS command \r\n");
Perijah 0:b7b55b8a4d2b 66 char value3[1];
Perijah 0:b7b55b8a4d2b 67 value3[0] = Qt1070Chip.getKeystate();
Perijah 0:b7b55b8a4d2b 68 write(value3, 1);
Perijah 0:b7b55b8a4d2b 69 break;
Perijah 0:b7b55b8a4d2b 70 }
Perijah 0:b7b55b8a4d2b 71 case RESET: {
Perijah 0:b7b55b8a4d2b 72 printf("recieved WRITE_DATA command \r\n");
Perijah 0:b7b55b8a4d2b 73 break;
Perijah 0:b7b55b8a4d2b 74 }
Perijah 0:b7b55b8a4d2b 75 default: {
Perijah 0:b7b55b8a4d2b 76 printf("unknown command\n\r");
Perijah 0:b7b55b8a4d2b 77 }
Perijah 0:b7b55b8a4d2b 78 }
Perijah 0:b7b55b8a4d2b 79 }
Perijah 0:b7b55b8a4d2b 80 };