A simple touch sensor that sends a message to computer when it notices touch. I'm using a simple voltage divider with 10k resistor and body impedance.

Dependencies:   mbed

Committer:
Vekotin
Date:
Thu Jan 30 06:13:55 2014 +0000
Revision:
1:7ed7d128d225
egw

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vekotin 1:7ed7d128d225 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Vekotin 1:7ed7d128d225 2 *
Vekotin 1:7ed7d128d225 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Vekotin 1:7ed7d128d225 4 * and associated documentation files (the "Software"), to deal in the Software without
Vekotin 1:7ed7d128d225 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Vekotin 1:7ed7d128d225 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Vekotin 1:7ed7d128d225 7 * Software is furnished to do so, subject to the following conditions:
Vekotin 1:7ed7d128d225 8 *
Vekotin 1:7ed7d128d225 9 * The above copyright notice and this permission notice shall be included in all copies or
Vekotin 1:7ed7d128d225 10 * substantial portions of the Software.
Vekotin 1:7ed7d128d225 11 *
Vekotin 1:7ed7d128d225 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Vekotin 1:7ed7d128d225 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Vekotin 1:7ed7d128d225 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Vekotin 1:7ed7d128d225 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Vekotin 1:7ed7d128d225 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Vekotin 1:7ed7d128d225 17 */
Vekotin 1:7ed7d128d225 18
Vekotin 1:7ed7d128d225 19 #include "stdint.h"
Vekotin 1:7ed7d128d225 20 #include "USBMIDI.h"
Vekotin 1:7ed7d128d225 21
Vekotin 1:7ed7d128d225 22
Vekotin 1:7ed7d128d225 23 USBMIDI::USBMIDI(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
Vekotin 1:7ed7d128d225 24 midi_evt = NULL;
Vekotin 1:7ed7d128d225 25 USBDevice::connect();
Vekotin 1:7ed7d128d225 26 }
Vekotin 1:7ed7d128d225 27
Vekotin 1:7ed7d128d225 28 void USBMIDI::write(MIDIMessage m) {
Vekotin 1:7ed7d128d225 29 USBDevice::write(EPBULK_IN, m.data, 4, MAX_PACKET_SIZE_EPBULK);
Vekotin 1:7ed7d128d225 30 }
Vekotin 1:7ed7d128d225 31
Vekotin 1:7ed7d128d225 32
Vekotin 1:7ed7d128d225 33 void USBMIDI::attach(void (*fptr)(MIDIMessage)) {
Vekotin 1:7ed7d128d225 34 midi_evt = fptr;
Vekotin 1:7ed7d128d225 35 }
Vekotin 1:7ed7d128d225 36
Vekotin 1:7ed7d128d225 37
Vekotin 1:7ed7d128d225 38 bool USBMIDI::EP2_OUT_callback() {
Vekotin 1:7ed7d128d225 39 uint8_t buf[64];
Vekotin 1:7ed7d128d225 40 uint32_t len;
Vekotin 1:7ed7d128d225 41 readEP(EPBULK_OUT, buf, &len, 64);
Vekotin 1:7ed7d128d225 42
Vekotin 1:7ed7d128d225 43 if (midi_evt != NULL) {
Vekotin 1:7ed7d128d225 44 for (int i=0; i<len; i+=4) {
Vekotin 1:7ed7d128d225 45 midi_evt(MIDIMessage(buf+i));
Vekotin 1:7ed7d128d225 46 }
Vekotin 1:7ed7d128d225 47 }
Vekotin 1:7ed7d128d225 48
Vekotin 1:7ed7d128d225 49 // We reactivate the endpoint to receive next characters
Vekotin 1:7ed7d128d225 50 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Vekotin 1:7ed7d128d225 51 return true;
Vekotin 1:7ed7d128d225 52 }
Vekotin 1:7ed7d128d225 53
Vekotin 1:7ed7d128d225 54
Vekotin 1:7ed7d128d225 55
Vekotin 1:7ed7d128d225 56 // Called in ISR context
Vekotin 1:7ed7d128d225 57 // Set configuration. Return false if the
Vekotin 1:7ed7d128d225 58 // configuration is not supported.
Vekotin 1:7ed7d128d225 59 bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration) {
Vekotin 1:7ed7d128d225 60 if (configuration != DEFAULT_CONFIGURATION) {
Vekotin 1:7ed7d128d225 61 return false;
Vekotin 1:7ed7d128d225 62 }
Vekotin 1:7ed7d128d225 63
Vekotin 1:7ed7d128d225 64 // Configure endpoints > 0
Vekotin 1:7ed7d128d225 65 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
Vekotin 1:7ed7d128d225 66 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Vekotin 1:7ed7d128d225 67
Vekotin 1:7ed7d128d225 68 // We activate the endpoint to be able to receive data
Vekotin 1:7ed7d128d225 69 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
Vekotin 1:7ed7d128d225 70 return true;
Vekotin 1:7ed7d128d225 71 }
Vekotin 1:7ed7d128d225 72
Vekotin 1:7ed7d128d225 73
Vekotin 1:7ed7d128d225 74 uint8_t * USBMIDI::stringIinterfaceDesc() {
Vekotin 1:7ed7d128d225 75 static uint8_t stringIinterfaceDescriptor[] = {
Vekotin 1:7ed7d128d225 76 0x0c, //bLength
Vekotin 1:7ed7d128d225 77 STRING_DESCRIPTOR, //bDescriptorType 0x03
Vekotin 1:7ed7d128d225 78 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio
Vekotin 1:7ed7d128d225 79 };
Vekotin 1:7ed7d128d225 80 return stringIinterfaceDescriptor;
Vekotin 1:7ed7d128d225 81 }
Vekotin 1:7ed7d128d225 82
Vekotin 1:7ed7d128d225 83 uint8_t * USBMIDI::stringIproductDesc() {
Vekotin 1:7ed7d128d225 84 static uint8_t stringIproductDescriptor[] = {
Vekotin 1:7ed7d128d225 85 0x12, //bLength
Vekotin 1:7ed7d128d225 86 STRING_DESCRIPTOR, //bDescriptorType 0x03
Vekotin 1:7ed7d128d225 87 'M',0,'I',0,'D',0,'I',0,' ',0,'M',0,'A',0,'N',0, //bString iProduct - MIDIMAN
Vekotin 1:7ed7d128d225 88 };
Vekotin 1:7ed7d128d225 89 return stringIproductDescriptor;
Vekotin 1:7ed7d128d225 90 }
Vekotin 1:7ed7d128d225 91
Vekotin 1:7ed7d128d225 92
Vekotin 1:7ed7d128d225 93 uint8_t * USBMIDI::configurationDesc() {
Vekotin 1:7ed7d128d225 94 static uint8_t configDescriptor[] = {
Vekotin 1:7ed7d128d225 95 // configuration descriptor
Vekotin 1:7ed7d128d225 96 0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50,
Vekotin 1:7ed7d128d225 97
Vekotin 1:7ed7d128d225 98 // The Audio Interface Collection
Vekotin 1:7ed7d128d225 99 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor
Vekotin 1:7ed7d128d225 100 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor
Vekotin 1:7ed7d128d225 101 0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors
Vekotin 1:7ed7d128d225 102 0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00, // Class-Specific MS Interface Header Descriptor
Vekotin 1:7ed7d128d225 103
Vekotin 1:7ed7d128d225 104 // MIDI IN JACKS
Vekotin 1:7ed7d128d225 105 0x06, 0x24, 0x02, 0x01, 0x01, 0x00,
Vekotin 1:7ed7d128d225 106 0x06, 0x24, 0x02, 0x02, 0x02, 0x00,
Vekotin 1:7ed7d128d225 107
Vekotin 1:7ed7d128d225 108 // MIDI OUT JACKS
Vekotin 1:7ed7d128d225 109 0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00,
Vekotin 1:7ed7d128d225 110 0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00,
Vekotin 1:7ed7d128d225 111
Vekotin 1:7ed7d128d225 112 // OUT endpoint descriptor
Vekotin 1:7ed7d128d225 113 0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
Vekotin 1:7ed7d128d225 114 0x05, 0x25, 0x01, 0x01, 0x01,
Vekotin 1:7ed7d128d225 115
Vekotin 1:7ed7d128d225 116 // IN endpoint descriptor
Vekotin 1:7ed7d128d225 117 0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
Vekotin 1:7ed7d128d225 118 0x05, 0x25, 0x01, 0x01, 0x03,
Vekotin 1:7ed7d128d225 119 };
Vekotin 1:7ed7d128d225 120 return configDescriptor;
Vekotin 1:7ed7d128d225 121 }