Percussion instrument interface POMPOM mbed program

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Timer.h"
00003 #include "rtos.h"
00004 
00005 unsigned char txFIFO[256];
00006 unsigned char tx_top, tx_end;
00007 
00008 // send message define
00009 // 1xyyy ~ 1xyyy : x-button numbeer yyy-button power
00010 // 2xyyy : x PI state number y timing
00011 // 3yxxx : y-1:X 2:Y 3:Z xxx-zyro sensor state
00012 
00013 Timer timer[9];
00014 
00015 RawSerial xbee(PA_2, PA_3);
00016 
00017 DigitalOut LED01(PB_13);
00018 DigitalOut LED02(PB_14);
00019 
00020 DigitalIn SW[9][2] = {{PB_1,PC_1}, {PB_2,PC_2}, {PB_3,PC_3},
00021     {PB_4,PC_4}, {PB_5,PC_5}, {PB_6,PC_6},
00022     {PB_7,PC_7}, {PB_8,PC_8}, {PB_9,PC_9}
00023 };
00024 
00025 DigitalIn PI01(PC_10);
00026 DigitalIn PI02(PC_12);
00027 
00028 AnalogIn analog_value0(PA_0);
00029 AnalogIn analog_value1(PA_1);
00030 AnalogIn analog_value2(PA_4);
00031 
00032 int check_time[2][9] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};
00033 int int_pi[2] = {0,0};
00034 
00035 int checkPI();// check PI state
00036 int checkBT(int checkbtn); // check for button time
00037 int send_pi(int (*pi_ara));
00038 void send_btn(int checkbtn);
00039 
00040 int checkPItime = 0;
00041 int sendPItime = 0;
00042 
00043 /////////////////////////xbee//////////////////////////
00044 void tx_fifo_check()
00045 {
00046     if(xbee.writeable() == 1) {
00047         if(tx_top != tx_end) {
00048             xbee.putc(txFIFO[tx_end]);
00049             ++tx_end &= 255;
00050         }
00051     }
00052     return;
00053 }
00054 
00055 void tx_fifoset(unsigned char data)
00056 {
00057     txFIFO[tx_top] = data;
00058     ++tx_top &= 255;
00059     return;
00060 }
00061 
00062 unsigned char hex_conv(unsigned char data)
00063 {
00064     data &= 15;
00065     if(data < 10) return(data+48);
00066     else return(data+55);
00067 }
00068 
00069 void tx_message(int data)
00070 {
00071     int i;
00072     for (i=0; i<6; i++) {
00073         tx_fifoset(hex_conv((data>>(4*(5-i))) & 15));
00074     }
00075     tx_fifoset(13);
00076 }
00077 /////////////////////////end xbee//////////////////////////
00078 
00079 void xbee_thread(void const *args) // xbee thread
00080 {
00081     int j;
00082     j = 0;
00083     tx_top = tx_end = 0;
00084     xbee.baud(19200);
00085 
00086     while(1) {
00087         tx_fifo_check();
00088         if(++j > 100000) {
00089             j = 0;
00090 
00091             uint16_t zyrox = analog_value0.read_u16();
00092             tx_message(31000+(zyrox>>8));
00093             uint16_t zyroy = analog_value1.read_u16();
00094             tx_message(32000+(zyroy>>8));
00095             uint16_t zyroz = analog_value2.read_u16();
00096             tx_message(33000+(zyroz>>8));
00097         }
00098 
00099         send_pi(int_pi);
00100 
00101     }
00102 
00103 }
00104 int ii = 0;
00105 
00106 int main()
00107 {
00108     Thread thread(xbee_thread);   // define xbee thread
00109 
00110     while(1) {
00111 
00112         if(++ii > 100000) {
00113             ii = 0;
00114 
00115             LED01 = !LED01;
00116         }
00117 
00118         check_time[0][0] = checkBT(0);
00119         send_btn(0);
00120 
00121         check_time[0][1] = checkBT(1);
00122         send_btn(1);
00123 
00124         check_time[0][2] = checkBT(2);
00125         send_btn(2);
00126 
00127         check_time[0][3] = checkBT(3);
00128         send_btn(3);
00129 
00130         check_time[0][4] = checkBT(4);
00131         send_btn(4);
00132 
00133         check_time[0][5] = checkBT(5);
00134         send_btn(5);
00135 
00136         check_time[0][6] = checkBT(6);
00137         send_btn(6);
00138 
00139         check_time[0][7] = checkBT(7);
00140         send_btn(7);
00141 
00142         check_time[0][8] = checkBT(8);
00143         send_btn(8);
00144 
00145         int_pi[0] = checkPI();
00146 
00147     }
00148 }
00149 
00150 ////////////////////////// define function //////////////////////
00151 
00152 //check for PIstate
00153 int checkPI()
00154 {
00155     int checkpt = 0;
00156     if(PI01 == 1 && PI02 == 1) {
00157         checkpt = 1;
00158     } else if(PI01 == 0 && PI02 == 1) {
00159         checkpt = 2;
00160     } else if(PI01 == 0 && PI02 == 0) {
00161         checkpt = 3;
00162     } else if(PI01 == 1 && PI02 == 0) {
00163         checkpt = 4;
00164     } else {
00165         checkpt = 5; // for error check
00166 
00167     }
00168 
00169     return checkpt;
00170 }
00171 
00172 // check for button time
00173 int checkBT(int checkbtn)
00174 {
00175     int i = 0;
00176 
00177     if(SW[checkbtn][0] == 1 && SW[checkbtn][1] == 0 ) {
00178         timer[checkbtn].start();
00179         return i;
00180     } else if(SW[checkbtn][0] == 1 && SW[checkbtn][1] == 1 ) {
00181         i = 10002 + (checkbtn * 1000);
00182         return i;
00183     } else if(SW[checkbtn][0] == 0 && SW[checkbtn][1] == 0 ) {
00184         i = 10000 + (checkbtn * 1000);
00185 
00186         check_time[1][checkbtn] = 0;
00187         return i;
00188     } else if (SW[checkbtn][0] == 1 && SW[checkbtn][1] == 0 ) {
00189         return 0;
00190     }
00191 
00192     else {
00193         // i = 10003 + (checkbtn * 1);
00194         return 0;
00195     }
00196 }
00197 //send pi state
00198 int send_pi(int (*pi_aray))
00199 {
00200     int i = 0;
00201     if(pi_aray[0]!= pi_aray[1]) {
00202 
00203         i = 20000 + pi_aray[0];
00204         tx_message(i);
00205         pi_aray[1] = pi_aray[0];
00206         return 0;
00207     }
00208     return 0;
00209 }
00210 
00211 void send_btn(int checkbtn)
00212 {
00213     int i = 10002 + (checkbtn * 1000);
00214     int ii = 0;
00215     if(check_time[0][checkbtn] == i && check_time[1][checkbtn] == 0) {
00216         timer[checkbtn].stop();
00217         ii = timer[checkbtn].read_ms();
00218         if(ii > 800){
00219         ii = ii - 800;
00220         }
00221         if(ii > 997) {
00222             ii = 997;
00223 
00224         }
00225         ii = i+ii;
00226         
00227         tx_message(ii);
00228         timer[checkbtn].reset();
00229         check_time[1][checkbtn] = 1;
00230 
00231     }
00232 }