Allows mbed to send data to an HTML5 web browser through a 4 pole mini jack.

Dependents:   MicIO-Example

The Problem:

*Sending data from a microcontroller to any smartphone*

While you can use the usb interface on non-mobile devices, very few smartphones allow you to use this easily. For example iOS requires the purchasing of a 100k+ liscense.

Then there's bluetooth with its gazillions of native code libraries to hook into: OS X, Windows, Linux, iOS, Motorolla, HTC, Samsung, LG,....ew.

With HTML5's web audio API, we can create a hardware bus similar to how Square's Credit Card readers works.

I've successfully tested this with my Macbook Air with Firefox v28 and Chrome v33. IE will not work under any circumstances as it currently does not support the HTML5 Web Audio API.

Protocol Overview

Sending a data playoad

Javascript is asynchronous, and setIntervals can vary by +/- a few ms. The way around this is to create a master slave bus. Our JavaScript master dictates to the microcontroller slave when it wants data through a square wave clock signal. When the square wave goes from High (1) to low (0) (falling edge), the microcontroller should be sending the data payload. Here's a screenshot of this in action: http://colinbookman.com/content/images/2014/Mar/Screen_Shot_2014_03_23_at_4_12_32_PM.png

And here's is one of the sinusoids zoomed in: /media/uploads/cbookman3/screen_shot_2014-03-24_at_11.13.43_am.png

This is a video of the bus in action:

Each data playload is a sinusoid. As of right now the library generates 16 distinct frequencies, where each frequency represents a number from 0 to 15.

The MicIO in its current form has an error rate of 5%, and transfer rate of 16bits/sec. This can easily be sped up by using a faster microcontroller, as well as modulating sinusoids in the payload. For example 0xFF would be represented by: sin(2π*1000) + sin(2π*1500) +sin(2π*2000).

Parsing a data payload

HTML5's web audio api allows us to perform a mathematical operation called a Fast Fourier Transform (FFT - http:en.wikipedia.org/wiki/Fast_Fourier_transform. FFT's basically allow us to parse out the frequencies in our audio stream. Below is a table converting hex/decimal to its payload sinusoid frequency.

Hex | Sinusoid Frequency (Hz)

0x0 | 818

0x1 | 1076

0x2 | 1335

0x3 | 1335

0x4 | 1894

0x5 | 2153

0x6 | 2411

0x7 | 2670

0x8 | 2971

0x9 | 3229

0xA | 3488

0xB | 3746

0xC | 4048

0xD | 4306

0xE | 4565

0xF | 4823

NULL | < 500 or >5500

Wiring

4 pole mini jack

http://colinbookman.com/content/images/2014/Mar/4poleminiJack.jpg

1. Left Music = Clock In/Out 2. Right Music = Master Data not yet implemented 3. Ground 4. Microphone In = Slave Data

Currently, MicIO does not support the sending of data to the microcontroller, but it'd be trivial to add in future versions.

Here is the actual wiring schematic with the mbed NXP LPC1768: https:www.sparkfun.com/products/9564. Do to my cad software not having a 4 pole mini jack, Its wired up to two 3 pole mini jacks. The left minijack should go to the mic prong, where-as the right minijack goes to left Music. http://colinbookman.com/content/images/2014/Mar/micIO_Schematic.png

Software

Slave - MBED

The Code repo can be found on the mbed webpage (http:mbed.org/users/cbookman3/code/MicIO/ I've also published an example micIO application http:mbed.org/users/cbookman3/code/MicIO-Example/.

Basically it takes in a string array (or number). Then micIO sends the data in half byte payloads each time the master requests more data. If there is no more data, it simply does not generate any sinusoids, aka frequency of 0.

Master

This code can be found on https:github.com/cobookman/HTML5.MicIO.

The HTML5 MicIO library when instantiated, will try to bind to the microphone. Upon sucessful binding, it'll begin to request for data. There's two javascript files you must include:

  • js/clock.js - Generates a square wave clock for MicIO w/some helpers
  • js/index.js - The MicIO library

make sure that the volume on your computer/mobile device is all the way up.

To create a new MicIO master instance you simply run:

Example javascript master usage

var micIO = new MicIO(function onDataRecieved(halfByteArr) {
  //do stuff with the data E.g:
  var byteArr = [];
  for(var i = 0; i < halfByteArr.length; ++i) {
    var byteIndex = Math.floor(i/2);
      if(i%2 === 0) {		      //first half of the byte
	byteArr[byteIndex] = (halfByteArr[i] << 4) & 0xF0;
      } else {  			//second half of the byte
         byteArr[byteIndex] += halfByteArr[i] & 0x0F;
      }
    }
 });
Committer:
cbookman3
Date:
Sun Mar 23 20:45:58 2014 +0000
Revision:
3:1630409f9bd6
Parent:
2:c90f916f0b08
forgot to remove lcd library (used in debugging)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cbookman3 0:294495efee3f 1 /* Mbed MicIO library
cbookman3 0:294495efee3f 2 * Copyright (c) 2014, Colin Bookman, cobookman [at] gmail [dot] com
cbookman3 0:294495efee3f 3 *
cbookman3 0:294495efee3f 4 * This program is free software: you can redistribute it and/or modify
cbookman3 0:294495efee3f 5 * it under the terms of the GNU General Public License as published by
cbookman3 0:294495efee3f 6 * the Free Software Foundation, either version 3 of the License, or
cbookman3 0:294495efee3f 7 * (at your option) any later version.
cbookman3 0:294495efee3f 8 *
cbookman3 0:294495efee3f 9 * This program is distributed in the hope that it will be useful,
cbookman3 0:294495efee3f 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cbookman3 0:294495efee3f 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cbookman3 0:294495efee3f 12 * GNU General Public License for more details.
cbookman3 0:294495efee3f 13 *
cbookman3 0:294495efee3f 14 * You should have received a copy of the GNU General Public License
cbookman3 0:294495efee3f 15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
cbookman3 0:294495efee3f 16 */
cbookman3 0:294495efee3f 17
cbookman3 0:294495efee3f 18 #ifndef MBED_MICIO_H
cbookman3 0:294495efee3f 19 #define MBED_MICIO_H
cbookman3 0:294495efee3f 20
cbookman3 0:294495efee3f 21 #include "mbed.h"
cbookman3 0:294495efee3f 22
cbookman3 0:294495efee3f 23 /**
cbookman3 0:294495efee3f 24 * @code
cbookman3 0:294495efee3f 25 * #include "MicIO.h"
cbookman3 0:294495efee3f 26 *
cbookman3 0:294495efee3f 27 * MicIO micIO(p18,p19); //AnalogOut (Output - Mic), AnalogIn (Input Headphone Left/Right)
cbookman3 0:294495efee3f 28 *
cbookman3 0:294495efee3f 29 * int main() {
cbookman3 0:294495efee3f 30 * micIO.send("HI WORLD", 8);
cbookman3 0:294495efee3f 31 * micIO.send([0xFF, 0xFA, 0xFB, 0x00], 4);
cbookman3 0:294495efee3f 32 * }
cbookman3 0:294495efee3f 33 * @endcode
cbookman3 0:294495efee3f 34 */
cbookman3 0:294495efee3f 35 class MicIO {
cbookman3 0:294495efee3f 36 public:
cbookman3 0:294495efee3f 37
cbookman3 0:294495efee3f 38 /** Create a MicIO instance */
cbookman3 0:294495efee3f 39 MicIO(PinName output, PinName input);
cbookman3 2:c90f916f0b08 40 float clockPeriod;
cbookman3 2:c90f916f0b08 41
cbookman3 0:294495efee3f 42 /* Send byte array through micIO */
cbookman3 0:294495efee3f 43 void send(const char * arr, int length);
cbookman3 0:294495efee3f 44
cbookman3 0:294495efee3f 45 /* Send just 4 bits */
cbookman3 0:294495efee3f 46 void send4Bits(unsigned char byte);
cbookman3 0:294495efee3f 47
cbookman3 0:294495efee3f 48 /*
cbookman3 0:294495efee3f 49 Read the current value of the input clock.
cbookman3 0:294495efee3f 50 1 = high
cbookman3 0:294495efee3f 51 0 = low
cbookman3 0:294495efee3f 52 -1 = unchanged
cbookman3 0:294495efee3f 53 */
cbookman3 0:294495efee3f 54 int clock();
cbookman3 0:294495efee3f 55
cbookman3 0:294495efee3f 56 /* Extracts the lower 4 bits of a byte */
cbookman3 0:294495efee3f 57 unsigned char lower4Bits(unsigned char byte);
cbookman3 0:294495efee3f 58 /* Extracts the upper 4 bits of a byte */
cbookman3 0:294495efee3f 59 unsigned char upper4Bits(unsigned char byte);
cbookman3 0:294495efee3f 60
cbookman3 0:294495efee3f 61 protected:
cbookman3 0:294495efee3f 62 AnalogOut _micOut;
cbookman3 0:294495efee3f 63 AnalogIn _clockIn;
cbookman3 0:294495efee3f 64 float _sinTable[361]; //361 in case of overflow...not that it SHOULD happen
cbookman3 0:294495efee3f 65 /*
cbookman3 0:294495efee3f 66 sendSin outputs a sin wave for 1 period.
cbookman3 0:294495efee3f 67 -------------------------
cbookman3 0:294495efee3f 68 |Speed | |
cbookman3 0:294495efee3f 69 |Factor | Frequency |
cbookman3 0:294495efee3f 70 |------------------------
cbookman3 0:294495efee3f 71 | 4.75 | 5.73Khz |
cbookman3 0:294495efee3f 72 | 4.50 | 5.45Khz |
cbookman3 0:294495efee3f 73 | 4.25 | 5.13Khz |
cbookman3 0:294495efee3f 74 | 4.00 | 4.84Khz |
cbookman3 0:294495efee3f 75 | 3.75 | 4.54Khz |
cbookman3 0:294495efee3f 76 | 3.50 | 4.23Khz |
cbookman3 0:294495efee3f 77 | 3.25 | 3.92Khz |
cbookman3 0:294495efee3f 78 | 3.00 | 3.65-3.62Khz|
cbookman3 0:294495efee3f 79 | 2.75 | 3.33Khz |
cbookman3 0:294495efee3f 80 | 2.50 | 3Khz |
cbookman3 0:294495efee3f 81 | 2.25 | 2.73Khz |
cbookman3 0:294495efee3f 82 | 2.00 | 2.41Khz |
cbookman3 0:294495efee3f 83 | 1.75 | 2.12Khz |
cbookman3 0:294495efee3f 84 | 1.50 | 1.818Khz |
cbookman3 0:294495efee3f 85 | 1.25 | 1.5Khz |
cbookman3 0:294495efee3f 86 | 1.00 | 1.2Khz |
cbookman3 0:294495efee3f 87 | 0.75 | 0.909KHz |
cbookman3 0:294495efee3f 88 | 0.5 | 0.606Khz |
cbookman3 0:294495efee3f 89 -------------------------
cbookman3 0:294495efee3f 90 */
cbookman3 0:294495efee3f 91 void _sendSin(float sinSeed); //sinSeed of 1 = 5khz
cbookman3 0:294495efee3f 92
cbookman3 2:c90f916f0b08 93 /* Get the number of cycles the sin wave should run for */
cbookman3 2:c90f916f0b08 94 int _numCycles(float sinSeed);
cbookman3 2:c90f916f0b08 95
cbookman3 0:294495efee3f 96 /* Generate a sin Table */
cbookman3 0:294495efee3f 97 void _genSinTable();
cbookman3 0:294495efee3f 98
cbookman3 0:294495efee3f 99 /* Go from 4 bits to a sin seed */
cbookman3 0:294495efee3f 100 float _getSinSeed(unsigned char bits4);
cbookman3 0:294495efee3f 101
cbookman3 0:294495efee3f 102 };
cbookman3 0:294495efee3f 103
cbookman3 0:294495efee3f 104 #endif