Flageoboard with M0

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <mbed.h>
00002 
00003 extern "C" void mbed_reset();
00004 
00005 // Debug
00006 BusOut leds(LED1, LED2, LED3, LED4);
00007 
00008 // Analog Input
00009 AnalogIn analogIn0(p20);
00010 AnalogIn analogIn1(p19);
00011 AnalogIn analogIn2(p17);
00012 
00013 // Input
00014 DigitalIn kb_dat(p9);
00015 DigitalIn kb_clk(p10);
00016 Ticker ps2_ticker;
00017 int wdt = 0;
00018 int bit = 0;
00019 int val = 0;
00020 int ignore = 0;
00021 unsigned ks[256];
00022 
00023 // Output
00024 SPI spi(p11, p12, p13);
00025 DigitalOut lrck(p14);
00026 Ticker ticker;
00027 unsigned *freqs = (unsigned *)calloc(127, sizeof(unsigned)); // Frequencies Of Each Keys
00028 int amp = 0x00;
00029 int da = 1;
00030 int sweep = 0;
00031 int fs;
00032 int f;
00033 int count = 0;
00034 
00035 void sampling() {
00036     int outa = amp >> 3;
00037     lrck = 1;
00038     spi.write(outa);
00039     lrck = 0;
00040     spi.write(outa);
00041     amp = -amp;
00042     count++;
00043     if (sweep && count % 4 == 0) {
00044         f -= sweep;
00045         if (f >= 16) ticker.attach_us(&sampling, 327273 / f);
00046     }
00047 }
00048 
00049 void onTicker() {
00050     if (bit) {
00051         if (++wdt >= 10) {
00052             if (val == 0xF0) {
00053                 ignore = 1;
00054             } else {
00055                 if (!ignore) {
00056                     if (freqs[ks[val]]) {
00057                         f = freqs[ks[val]];
00058                         amp = analogIn0.read_u16();
00059                         da = (0xFFFF - analogIn1.read_u16()) >> 11;
00060                         sweep = analogIn2.read_u16() >> 12;
00061                         ticker.attach_us(&sampling, 327273 / f);
00062                         leds = ks[val];
00063                         leds = sweep;
00064                     } else {
00065                         //mbed_reset();
00066                     }
00067                 }
00068                 ignore = 0;
00069             }
00070             wdt = 0;
00071             bit = 0;
00072             val = 0;
00073         }
00074     }
00075     if (amp > 0) {
00076         amp -= da;
00077         if (amp < 0) amp = 0;
00078     }
00079 }
00080 
00081 void onKbClkFall() {
00082     wdt = 0;
00083     if (bit >= 1 && bit < 9) val += (int)kb_dat << (bit - 1);
00084     bit++;
00085 }
00086 
00087 int main() {
00088     // Output
00089     char *k = "-0987654321[poiuytrewq';lkjhgfdsa /.,mnbvcxz";    // Keyboard Layout
00090     unsigned f0 = 64;                                            // Frequency Of Fundamental Tone
00091     for (int m = 1; m <= 4; m++) {
00092         for (int n = 1; n <= 11; n++) freqs[*k++] = f0 * n;
00093         f0 = f0 * 3 / 4;
00094     }
00095     spi.format(16, 0);
00096     spi.frequency(1600 * 1000);
00097 
00098     // Input    
00099     ks[0x4E] = '-'; ks[0x45] = '0'; ks[0x46] = '9'; ks[0x3E] = '8'; ks[0x3D] = '7'; ks[0x36] = '6'; ks[0x2E] = '5'; ks[0x25] = '4'; ks[0x26] = '3'; ks[0x1E] = '2'; ks[0x16] = '1';
00100     ks[0x54] = '['; ks[0x4D] = 'p'; ks[0x44] = 'o'; ks[0x43] = 'i'; ks[0x3C] = 'u'; ks[0x35] = 'y'; ks[0x2C] = 't'; ks[0x2D] = 'r'; ks[0x24] = 'e'; ks[0x1D] = 'w'; ks[0x15] = 'q';
00101     ks[0x52] = '\''; ks[0x4C] = ';'; ks[0x4B] = 'l'; ks[0x42] = 'k'; ks[0x3B] = 'j'; ks[0x33] = 'h'; ks[0x34] = 'g'; ks[0x2B] = 'f'; ks[0x23] = 'd'; ks[0x1B] = 's'; ks[0x1C] = 'a';
00102     ks[0x29] = ' '; ks[0x4A] = '/'; ks[0x49] = '.'; ks[0x41] = ','; ks[0x3A] = 'm'; ks[0x31] = 'n'; ks[0x32] = 'b'; ks[0x2A] = 'v'; ks[0x21] = 'c'; ks[0x22] = 'x'; ks[0x1A] = 'z';
00103     ps2_ticker.attach_us(&onTicker, 100);
00104     while (1) {
00105         while (kb_clk) ;
00106         onKbClkFall();
00107         while (!kb_clk) ;
00108     }
00109 }