Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@0:8917234c9dfc, 2015-01-08 (annotated)
- Committer:
- chickesnoup
- Date:
- Thu Jan 08 23:24:21 2015 +0000
- Revision:
- 0:8917234c9dfc
Percussion instrument interface POMPOM mbed program
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chickesnoup | 0:8917234c9dfc | 1 | #include "mbed.h" |
chickesnoup | 0:8917234c9dfc | 2 | #include "Timer.h" |
chickesnoup | 0:8917234c9dfc | 3 | #include "rtos.h" |
chickesnoup | 0:8917234c9dfc | 4 | |
chickesnoup | 0:8917234c9dfc | 5 | unsigned char txFIFO[256]; |
chickesnoup | 0:8917234c9dfc | 6 | unsigned char tx_top, tx_end; |
chickesnoup | 0:8917234c9dfc | 7 | |
chickesnoup | 0:8917234c9dfc | 8 | // send message define |
chickesnoup | 0:8917234c9dfc | 9 | // 1xyyy ~ 1xyyy : x-button numbeer yyy-button power |
chickesnoup | 0:8917234c9dfc | 10 | // 2xyyy : x PI state number y timing |
chickesnoup | 0:8917234c9dfc | 11 | // 3yxxx : y-1:X 2:Y 3:Z xxx-zyro sensor state |
chickesnoup | 0:8917234c9dfc | 12 | |
chickesnoup | 0:8917234c9dfc | 13 | Timer timer[9]; |
chickesnoup | 0:8917234c9dfc | 14 | |
chickesnoup | 0:8917234c9dfc | 15 | RawSerial xbee(PA_2, PA_3); |
chickesnoup | 0:8917234c9dfc | 16 | |
chickesnoup | 0:8917234c9dfc | 17 | DigitalOut LED01(PB_13); |
chickesnoup | 0:8917234c9dfc | 18 | DigitalOut LED02(PB_14); |
chickesnoup | 0:8917234c9dfc | 19 | |
chickesnoup | 0:8917234c9dfc | 20 | DigitalIn SW[9][2] = {{PB_1,PC_1}, {PB_2,PC_2}, {PB_3,PC_3}, |
chickesnoup | 0:8917234c9dfc | 21 | {PB_4,PC_4}, {PB_5,PC_5}, {PB_6,PC_6}, |
chickesnoup | 0:8917234c9dfc | 22 | {PB_7,PC_7}, {PB_8,PC_8}, {PB_9,PC_9} |
chickesnoup | 0:8917234c9dfc | 23 | }; |
chickesnoup | 0:8917234c9dfc | 24 | |
chickesnoup | 0:8917234c9dfc | 25 | DigitalIn PI01(PC_10); |
chickesnoup | 0:8917234c9dfc | 26 | DigitalIn PI02(PC_12); |
chickesnoup | 0:8917234c9dfc | 27 | |
chickesnoup | 0:8917234c9dfc | 28 | AnalogIn analog_value0(PA_0); |
chickesnoup | 0:8917234c9dfc | 29 | AnalogIn analog_value1(PA_1); |
chickesnoup | 0:8917234c9dfc | 30 | AnalogIn analog_value2(PA_4); |
chickesnoup | 0:8917234c9dfc | 31 | |
chickesnoup | 0:8917234c9dfc | 32 | int check_time[2][9] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}}; |
chickesnoup | 0:8917234c9dfc | 33 | int int_pi[2] = {0,0}; |
chickesnoup | 0:8917234c9dfc | 34 | |
chickesnoup | 0:8917234c9dfc | 35 | int checkPI();// check PI state |
chickesnoup | 0:8917234c9dfc | 36 | int checkBT(int checkbtn); // check for button time |
chickesnoup | 0:8917234c9dfc | 37 | int send_pi(int (*pi_ara)); |
chickesnoup | 0:8917234c9dfc | 38 | void send_btn(int checkbtn); |
chickesnoup | 0:8917234c9dfc | 39 | |
chickesnoup | 0:8917234c9dfc | 40 | int checkPItime = 0; |
chickesnoup | 0:8917234c9dfc | 41 | int sendPItime = 0; |
chickesnoup | 0:8917234c9dfc | 42 | |
chickesnoup | 0:8917234c9dfc | 43 | /////////////////////////xbee////////////////////////// |
chickesnoup | 0:8917234c9dfc | 44 | void tx_fifo_check() |
chickesnoup | 0:8917234c9dfc | 45 | { |
chickesnoup | 0:8917234c9dfc | 46 | if(xbee.writeable() == 1) { |
chickesnoup | 0:8917234c9dfc | 47 | if(tx_top != tx_end) { |
chickesnoup | 0:8917234c9dfc | 48 | xbee.putc(txFIFO[tx_end]); |
chickesnoup | 0:8917234c9dfc | 49 | ++tx_end &= 255; |
chickesnoup | 0:8917234c9dfc | 50 | } |
chickesnoup | 0:8917234c9dfc | 51 | } |
chickesnoup | 0:8917234c9dfc | 52 | return; |
chickesnoup | 0:8917234c9dfc | 53 | } |
chickesnoup | 0:8917234c9dfc | 54 | |
chickesnoup | 0:8917234c9dfc | 55 | void tx_fifoset(unsigned char data) |
chickesnoup | 0:8917234c9dfc | 56 | { |
chickesnoup | 0:8917234c9dfc | 57 | txFIFO[tx_top] = data; |
chickesnoup | 0:8917234c9dfc | 58 | ++tx_top &= 255; |
chickesnoup | 0:8917234c9dfc | 59 | return; |
chickesnoup | 0:8917234c9dfc | 60 | } |
chickesnoup | 0:8917234c9dfc | 61 | |
chickesnoup | 0:8917234c9dfc | 62 | unsigned char hex_conv(unsigned char data) |
chickesnoup | 0:8917234c9dfc | 63 | { |
chickesnoup | 0:8917234c9dfc | 64 | data &= 15; |
chickesnoup | 0:8917234c9dfc | 65 | if(data < 10) return(data+48); |
chickesnoup | 0:8917234c9dfc | 66 | else return(data+55); |
chickesnoup | 0:8917234c9dfc | 67 | } |
chickesnoup | 0:8917234c9dfc | 68 | |
chickesnoup | 0:8917234c9dfc | 69 | void tx_message(int data) |
chickesnoup | 0:8917234c9dfc | 70 | { |
chickesnoup | 0:8917234c9dfc | 71 | int i; |
chickesnoup | 0:8917234c9dfc | 72 | for (i=0; i<6; i++) { |
chickesnoup | 0:8917234c9dfc | 73 | tx_fifoset(hex_conv((data>>(4*(5-i))) & 15)); |
chickesnoup | 0:8917234c9dfc | 74 | } |
chickesnoup | 0:8917234c9dfc | 75 | tx_fifoset(13); |
chickesnoup | 0:8917234c9dfc | 76 | } |
chickesnoup | 0:8917234c9dfc | 77 | /////////////////////////end xbee////////////////////////// |
chickesnoup | 0:8917234c9dfc | 78 | |
chickesnoup | 0:8917234c9dfc | 79 | void xbee_thread(void const *args) // xbee thread |
chickesnoup | 0:8917234c9dfc | 80 | { |
chickesnoup | 0:8917234c9dfc | 81 | int j; |
chickesnoup | 0:8917234c9dfc | 82 | j = 0; |
chickesnoup | 0:8917234c9dfc | 83 | tx_top = tx_end = 0; |
chickesnoup | 0:8917234c9dfc | 84 | xbee.baud(19200); |
chickesnoup | 0:8917234c9dfc | 85 | |
chickesnoup | 0:8917234c9dfc | 86 | while(1) { |
chickesnoup | 0:8917234c9dfc | 87 | tx_fifo_check(); |
chickesnoup | 0:8917234c9dfc | 88 | if(++j > 100000) { |
chickesnoup | 0:8917234c9dfc | 89 | j = 0; |
chickesnoup | 0:8917234c9dfc | 90 | |
chickesnoup | 0:8917234c9dfc | 91 | uint16_t zyrox = analog_value0.read_u16(); |
chickesnoup | 0:8917234c9dfc | 92 | tx_message(31000+(zyrox>>8)); |
chickesnoup | 0:8917234c9dfc | 93 | uint16_t zyroy = analog_value1.read_u16(); |
chickesnoup | 0:8917234c9dfc | 94 | tx_message(32000+(zyroy>>8)); |
chickesnoup | 0:8917234c9dfc | 95 | uint16_t zyroz = analog_value2.read_u16(); |
chickesnoup | 0:8917234c9dfc | 96 | tx_message(33000+(zyroz>>8)); |
chickesnoup | 0:8917234c9dfc | 97 | } |
chickesnoup | 0:8917234c9dfc | 98 | |
chickesnoup | 0:8917234c9dfc | 99 | send_pi(int_pi); |
chickesnoup | 0:8917234c9dfc | 100 | |
chickesnoup | 0:8917234c9dfc | 101 | } |
chickesnoup | 0:8917234c9dfc | 102 | |
chickesnoup | 0:8917234c9dfc | 103 | } |
chickesnoup | 0:8917234c9dfc | 104 | int ii = 0; |
chickesnoup | 0:8917234c9dfc | 105 | |
chickesnoup | 0:8917234c9dfc | 106 | int main() |
chickesnoup | 0:8917234c9dfc | 107 | { |
chickesnoup | 0:8917234c9dfc | 108 | Thread thread(xbee_thread); // define xbee thread |
chickesnoup | 0:8917234c9dfc | 109 | |
chickesnoup | 0:8917234c9dfc | 110 | while(1) { |
chickesnoup | 0:8917234c9dfc | 111 | |
chickesnoup | 0:8917234c9dfc | 112 | if(++ii > 100000) { |
chickesnoup | 0:8917234c9dfc | 113 | ii = 0; |
chickesnoup | 0:8917234c9dfc | 114 | |
chickesnoup | 0:8917234c9dfc | 115 | LED01 = !LED01; |
chickesnoup | 0:8917234c9dfc | 116 | } |
chickesnoup | 0:8917234c9dfc | 117 | |
chickesnoup | 0:8917234c9dfc | 118 | check_time[0][0] = checkBT(0); |
chickesnoup | 0:8917234c9dfc | 119 | send_btn(0); |
chickesnoup | 0:8917234c9dfc | 120 | |
chickesnoup | 0:8917234c9dfc | 121 | check_time[0][1] = checkBT(1); |
chickesnoup | 0:8917234c9dfc | 122 | send_btn(1); |
chickesnoup | 0:8917234c9dfc | 123 | |
chickesnoup | 0:8917234c9dfc | 124 | check_time[0][2] = checkBT(2); |
chickesnoup | 0:8917234c9dfc | 125 | send_btn(2); |
chickesnoup | 0:8917234c9dfc | 126 | |
chickesnoup | 0:8917234c9dfc | 127 | check_time[0][3] = checkBT(3); |
chickesnoup | 0:8917234c9dfc | 128 | send_btn(3); |
chickesnoup | 0:8917234c9dfc | 129 | |
chickesnoup | 0:8917234c9dfc | 130 | check_time[0][4] = checkBT(4); |
chickesnoup | 0:8917234c9dfc | 131 | send_btn(4); |
chickesnoup | 0:8917234c9dfc | 132 | |
chickesnoup | 0:8917234c9dfc | 133 | check_time[0][5] = checkBT(5); |
chickesnoup | 0:8917234c9dfc | 134 | send_btn(5); |
chickesnoup | 0:8917234c9dfc | 135 | |
chickesnoup | 0:8917234c9dfc | 136 | check_time[0][6] = checkBT(6); |
chickesnoup | 0:8917234c9dfc | 137 | send_btn(6); |
chickesnoup | 0:8917234c9dfc | 138 | |
chickesnoup | 0:8917234c9dfc | 139 | check_time[0][7] = checkBT(7); |
chickesnoup | 0:8917234c9dfc | 140 | send_btn(7); |
chickesnoup | 0:8917234c9dfc | 141 | |
chickesnoup | 0:8917234c9dfc | 142 | check_time[0][8] = checkBT(8); |
chickesnoup | 0:8917234c9dfc | 143 | send_btn(8); |
chickesnoup | 0:8917234c9dfc | 144 | |
chickesnoup | 0:8917234c9dfc | 145 | int_pi[0] = checkPI(); |
chickesnoup | 0:8917234c9dfc | 146 | |
chickesnoup | 0:8917234c9dfc | 147 | } |
chickesnoup | 0:8917234c9dfc | 148 | } |
chickesnoup | 0:8917234c9dfc | 149 | |
chickesnoup | 0:8917234c9dfc | 150 | ////////////////////////// define function ////////////////////// |
chickesnoup | 0:8917234c9dfc | 151 | |
chickesnoup | 0:8917234c9dfc | 152 | //check for PIstate |
chickesnoup | 0:8917234c9dfc | 153 | int checkPI() |
chickesnoup | 0:8917234c9dfc | 154 | { |
chickesnoup | 0:8917234c9dfc | 155 | int checkpt = 0; |
chickesnoup | 0:8917234c9dfc | 156 | if(PI01 == 1 && PI02 == 1) { |
chickesnoup | 0:8917234c9dfc | 157 | checkpt = 1; |
chickesnoup | 0:8917234c9dfc | 158 | } else if(PI01 == 0 && PI02 == 1) { |
chickesnoup | 0:8917234c9dfc | 159 | checkpt = 2; |
chickesnoup | 0:8917234c9dfc | 160 | } else if(PI01 == 0 && PI02 == 0) { |
chickesnoup | 0:8917234c9dfc | 161 | checkpt = 3; |
chickesnoup | 0:8917234c9dfc | 162 | } else if(PI01 == 1 && PI02 == 0) { |
chickesnoup | 0:8917234c9dfc | 163 | checkpt = 4; |
chickesnoup | 0:8917234c9dfc | 164 | } else { |
chickesnoup | 0:8917234c9dfc | 165 | checkpt = 5; // for error check |
chickesnoup | 0:8917234c9dfc | 166 | |
chickesnoup | 0:8917234c9dfc | 167 | } |
chickesnoup | 0:8917234c9dfc | 168 | |
chickesnoup | 0:8917234c9dfc | 169 | return checkpt; |
chickesnoup | 0:8917234c9dfc | 170 | } |
chickesnoup | 0:8917234c9dfc | 171 | |
chickesnoup | 0:8917234c9dfc | 172 | // check for button time |
chickesnoup | 0:8917234c9dfc | 173 | int checkBT(int checkbtn) |
chickesnoup | 0:8917234c9dfc | 174 | { |
chickesnoup | 0:8917234c9dfc | 175 | int i = 0; |
chickesnoup | 0:8917234c9dfc | 176 | |
chickesnoup | 0:8917234c9dfc | 177 | if(SW[checkbtn][0] == 1 && SW[checkbtn][1] == 0 ) { |
chickesnoup | 0:8917234c9dfc | 178 | timer[checkbtn].start(); |
chickesnoup | 0:8917234c9dfc | 179 | return i; |
chickesnoup | 0:8917234c9dfc | 180 | } else if(SW[checkbtn][0] == 1 && SW[checkbtn][1] == 1 ) { |
chickesnoup | 0:8917234c9dfc | 181 | i = 10002 + (checkbtn * 1000); |
chickesnoup | 0:8917234c9dfc | 182 | return i; |
chickesnoup | 0:8917234c9dfc | 183 | } else if(SW[checkbtn][0] == 0 && SW[checkbtn][1] == 0 ) { |
chickesnoup | 0:8917234c9dfc | 184 | i = 10000 + (checkbtn * 1000); |
chickesnoup | 0:8917234c9dfc | 185 | |
chickesnoup | 0:8917234c9dfc | 186 | check_time[1][checkbtn] = 0; |
chickesnoup | 0:8917234c9dfc | 187 | return i; |
chickesnoup | 0:8917234c9dfc | 188 | } else if (SW[checkbtn][0] == 1 && SW[checkbtn][1] == 0 ) { |
chickesnoup | 0:8917234c9dfc | 189 | return 0; |
chickesnoup | 0:8917234c9dfc | 190 | } |
chickesnoup | 0:8917234c9dfc | 191 | |
chickesnoup | 0:8917234c9dfc | 192 | else { |
chickesnoup | 0:8917234c9dfc | 193 | // i = 10003 + (checkbtn * 1); |
chickesnoup | 0:8917234c9dfc | 194 | return 0; |
chickesnoup | 0:8917234c9dfc | 195 | } |
chickesnoup | 0:8917234c9dfc | 196 | } |
chickesnoup | 0:8917234c9dfc | 197 | //send pi state |
chickesnoup | 0:8917234c9dfc | 198 | int send_pi(int (*pi_aray)) |
chickesnoup | 0:8917234c9dfc | 199 | { |
chickesnoup | 0:8917234c9dfc | 200 | int i = 0; |
chickesnoup | 0:8917234c9dfc | 201 | if(pi_aray[0]!= pi_aray[1]) { |
chickesnoup | 0:8917234c9dfc | 202 | |
chickesnoup | 0:8917234c9dfc | 203 | i = 20000 + pi_aray[0]; |
chickesnoup | 0:8917234c9dfc | 204 | tx_message(i); |
chickesnoup | 0:8917234c9dfc | 205 | pi_aray[1] = pi_aray[0]; |
chickesnoup | 0:8917234c9dfc | 206 | return 0; |
chickesnoup | 0:8917234c9dfc | 207 | } |
chickesnoup | 0:8917234c9dfc | 208 | return 0; |
chickesnoup | 0:8917234c9dfc | 209 | } |
chickesnoup | 0:8917234c9dfc | 210 | |
chickesnoup | 0:8917234c9dfc | 211 | void send_btn(int checkbtn) |
chickesnoup | 0:8917234c9dfc | 212 | { |
chickesnoup | 0:8917234c9dfc | 213 | int i = 10002 + (checkbtn * 1000); |
chickesnoup | 0:8917234c9dfc | 214 | int ii = 0; |
chickesnoup | 0:8917234c9dfc | 215 | if(check_time[0][checkbtn] == i && check_time[1][checkbtn] == 0) { |
chickesnoup | 0:8917234c9dfc | 216 | timer[checkbtn].stop(); |
chickesnoup | 0:8917234c9dfc | 217 | ii = timer[checkbtn].read_ms(); |
chickesnoup | 0:8917234c9dfc | 218 | if(ii > 800){ |
chickesnoup | 0:8917234c9dfc | 219 | ii = ii - 800; |
chickesnoup | 0:8917234c9dfc | 220 | } |
chickesnoup | 0:8917234c9dfc | 221 | if(ii > 997) { |
chickesnoup | 0:8917234c9dfc | 222 | ii = 997; |
chickesnoup | 0:8917234c9dfc | 223 | |
chickesnoup | 0:8917234c9dfc | 224 | } |
chickesnoup | 0:8917234c9dfc | 225 | ii = i+ii; |
chickesnoup | 0:8917234c9dfc | 226 | |
chickesnoup | 0:8917234c9dfc | 227 | tx_message(ii); |
chickesnoup | 0:8917234c9dfc | 228 | timer[checkbtn].reset(); |
chickesnoup | 0:8917234c9dfc | 229 | check_time[1][checkbtn] = 1; |
chickesnoup | 0:8917234c9dfc | 230 | |
chickesnoup | 0:8917234c9dfc | 231 | } |
chickesnoup | 0:8917234c9dfc | 232 | } |