code yo
Dependencies: MMA8451Q USBDevice mbed
Fork of idd_graph_sensor by
main.cpp@1:c5189364947e, 2014-09-25 (annotated)
- Committer:
- cdierk
- Date:
- Thu Sep 25 19:10:16 2014 +0000
- Revision:
- 1:c5189364947e
- Parent:
- 0:c9bb3c9d5ce8
First commit!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bjo3rn | 0:c9bb3c9d5ce8 | 1 | #include "mbed.h" |
bjo3rn | 0:c9bb3c9d5ce8 | 2 | #include "MMA8451Q.h" |
cdierk | 1:c5189364947e | 3 | #include "USBMouse.h" |
cdierk | 1:c5189364947e | 4 | |
cdierk | 1:c5189364947e | 5 | USBMouse mouse; |
bjo3rn | 0:c9bb3c9d5ce8 | 6 | |
bjo3rn | 0:c9bb3c9d5ce8 | 7 | // define I2C Pins and address for KL25Z. Taken from default sample code. |
bjo3rn | 0:c9bb3c9d5ce8 | 8 | PinName const SDA = PTE25; |
bjo3rn | 0:c9bb3c9d5ce8 | 9 | PinName const SCL = PTE24; |
bjo3rn | 0:c9bb3c9d5ce8 | 10 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
bjo3rn | 0:c9bb3c9d5ce8 | 11 | |
bjo3rn | 0:c9bb3c9d5ce8 | 12 | //serial connection to PC via USB |
bjo3rn | 0:c9bb3c9d5ce8 | 13 | Serial pc(USBTX, USBRX); |
bjo3rn | 0:c9bb3c9d5ce8 | 14 | |
cdierk | 1:c5189364947e | 15 | DigitalIn button1(D8); |
cdierk | 1:c5189364947e | 16 | DigitalIn button2(D7); |
cdierk | 1:c5189364947e | 17 | |
bjo3rn | 0:c9bb3c9d5ce8 | 18 | int main(void) |
bjo3rn | 0:c9bb3c9d5ce8 | 19 | { |
cdierk | 1:c5189364947e | 20 | AnalogIn xPin(A3); |
cdierk | 1:c5189364947e | 21 | AnalogIn yPin(A4); |
cdierk | 1:c5189364947e | 22 | AnalogIn zPin(A5); |
cdierk | 1:c5189364947e | 23 | button1.mode(PullUp); |
cdierk | 1:c5189364947e | 24 | button2.mode(PullUp); |
cdierk | 1:c5189364947e | 25 | |
bjo3rn | 0:c9bb3c9d5ce8 | 26 | //configure on-board I2C accelerometer on KL25Z |
bjo3rn | 0:c9bb3c9d5ce8 | 27 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
bjo3rn | 0:c9bb3c9d5ce8 | 28 | |
cdierk | 1:c5189364947e | 29 | Serial pc(USBTX, USBRX); |
bjo3rn | 0:c9bb3c9d5ce8 | 30 | |
bjo3rn | 0:c9bb3c9d5ce8 | 31 | while (true) { |
cdierk | 1:c5189364947e | 32 | |
cdierk | 1:c5189364947e | 33 | float xValue = xPin.read(); |
cdierk | 1:c5189364947e | 34 | float yValue = yPin.read(); |
cdierk | 1:c5189364947e | 35 | float zValue = zPin.read(); |
cdierk | 1:c5189364947e | 36 | pc.printf("%1.2f, %1.2f, %1.2f \r\n", xValue - 0.48, yValue - 0.44, zValue); |
cdierk | 1:c5189364947e | 37 | mouse.move(-150 * (xValue - 0.48), 150 * (yValue - 0.44)); |
cdierk | 1:c5189364947e | 38 | |
cdierk | 1:c5189364947e | 39 | if (!button1) { |
cdierk | 1:c5189364947e | 40 | mouse.click(MOUSE_LEFT); |
cdierk | 1:c5189364947e | 41 | } |
cdierk | 1:c5189364947e | 42 | if (!button2) { |
cdierk | 1:c5189364947e | 43 | |
cdierk | 1:c5189364947e | 44 | float yValueMax = yPin.read(); |
cdierk | 1:c5189364947e | 45 | yValue = yPin.read(); |
cdierk | 1:c5189364947e | 46 | while(true){ |
cdierk | 1:c5189364947e | 47 | if (yValue > yValueMax){ |
cdierk | 1:c5189364947e | 48 | yValueMax = yValue; |
cdierk | 1:c5189364947e | 49 | yValue = yPin.read(); |
cdierk | 1:c5189364947e | 50 | } else if ((yValue + .30) <= yValueMax){ |
cdierk | 1:c5189364947e | 51 | mouse.click(MOUSE_LEFT); |
cdierk | 1:c5189364947e | 52 | break; |
cdierk | 1:c5189364947e | 53 | } else { |
cdierk | 1:c5189364947e | 54 | yValue = yPin.read(); |
cdierk | 1:c5189364947e | 55 | } |
cdierk | 1:c5189364947e | 56 | } |
cdierk | 1:c5189364947e | 57 | /*while(true){ |
cdierk | 1:c5189364947e | 58 | if (yValue <= .50){ |
cdierk | 1:c5189364947e | 59 | yValue = yPin.read(); |
cdierk | 1:c5189364947e | 60 | } else { |
cdierk | 1:c5189364947e | 61 | while(true){ |
cdierk | 1:c5189364947e | 62 | yValue = yPin.read(); |
cdierk | 1:c5189364947e | 63 | if (yValue < .40){ |
cdierk | 1:c5189364947e | 64 | mouse.click(MOUSE_LEFT); |
cdierk | 1:c5189364947e | 65 | break; |
cdierk | 1:c5189364947e | 66 | } |
cdierk | 1:c5189364947e | 67 | } |
cdierk | 1:c5189364947e | 68 | } |
cdierk | 1:c5189364947e | 69 | break; |
cdierk | 1:c5189364947e | 70 | }*/ |
cdierk | 1:c5189364947e | 71 | } |
bjo3rn | 0:c9bb3c9d5ce8 | 72 | } |
bjo3rn | 0:c9bb3c9d5ce8 | 73 | } |
bjo3rn | 0:c9bb3c9d5ce8 | 74 | |
bjo3rn | 0:c9bb3c9d5ce8 | 75 | /* |
bjo3rn | 0:c9bb3c9d5ce8 | 76 | // Graphing sketch for Processing |
bjo3rn | 0:c9bb3c9d5ce8 | 77 | |
bjo3rn | 0:c9bb3c9d5ce8 | 78 | |
bjo3rn | 0:c9bb3c9d5ce8 | 79 | // This program takes ASCII-encoded strings containing floating point numbers |
bjo3rn | 0:c9bb3c9d5ce8 | 80 | // from the serial port at 9600 baud and graphs them. It expects values in the |
bjo3rn | 0:c9bb3c9d5ce8 | 81 | // range -1.0 to 1.0, followed by a newline, or newline and carriage return |
bjo3rn | 0:c9bb3c9d5ce8 | 82 | |
bjo3rn | 0:c9bb3c9d5ce8 | 83 | // Created 20 Apr 2005 |
bjo3rn | 0:c9bb3c9d5ce8 | 84 | // Updated 18 Jan 2008 by Tom Igoe |
bjo3rn | 0:c9bb3c9d5ce8 | 85 | // Adapted 16 Sep 2014 by Bjoern Hartmann for mbed |
bjo3rn | 0:c9bb3c9d5ce8 | 86 | // This example code is in the public domain. |
bjo3rn | 0:c9bb3c9d5ce8 | 87 | |
bjo3rn | 0:c9bb3c9d5ce8 | 88 | import processing.serial.*; |
bjo3rn | 0:c9bb3c9d5ce8 | 89 | |
bjo3rn | 0:c9bb3c9d5ce8 | 90 | Serial myPort; // The serial port |
bjo3rn | 0:c9bb3c9d5ce8 | 91 | int xPos = 1; // horizontal position of the graph |
bjo3rn | 0:c9bb3c9d5ce8 | 92 | |
bjo3rn | 0:c9bb3c9d5ce8 | 93 | float minVal=-1.0; |
bjo3rn | 0:c9bb3c9d5ce8 | 94 | float maxVal=1.0; |
bjo3rn | 0:c9bb3c9d5ce8 | 95 | |
bjo3rn | 0:c9bb3c9d5ce8 | 96 | void setup () { |
bjo3rn | 0:c9bb3c9d5ce8 | 97 | // set the window size: |
bjo3rn | 0:c9bb3c9d5ce8 | 98 | size(400, 300); |
bjo3rn | 0:c9bb3c9d5ce8 | 99 | |
bjo3rn | 0:c9bb3c9d5ce8 | 100 | // List all the available serial ports |
bjo3rn | 0:c9bb3c9d5ce8 | 101 | println(Serial.list()); |
bjo3rn | 0:c9bb3c9d5ce8 | 102 | // Open whatever port is the one you're using. |
bjo3rn | 0:c9bb3c9d5ce8 | 103 | myPort = new Serial(this, "/dev/tty.usbmodem1412", 9600); |
bjo3rn | 0:c9bb3c9d5ce8 | 104 | // don't generate a serialEvent() unless you get a newline character: |
bjo3rn | 0:c9bb3c9d5ce8 | 105 | myPort.bufferUntil('\n'); |
bjo3rn | 0:c9bb3c9d5ce8 | 106 | // set inital background: |
bjo3rn | 0:c9bb3c9d5ce8 | 107 | background(0); |
bjo3rn | 0:c9bb3c9d5ce8 | 108 | } |
bjo3rn | 0:c9bb3c9d5ce8 | 109 | void draw () { |
bjo3rn | 0:c9bb3c9d5ce8 | 110 | // everything happens in the serialEvent() |
bjo3rn | 0:c9bb3c9d5ce8 | 111 | } |
bjo3rn | 0:c9bb3c9d5ce8 | 112 | |
bjo3rn | 0:c9bb3c9d5ce8 | 113 | void serialEvent (Serial myPort) { |
bjo3rn | 0:c9bb3c9d5ce8 | 114 | // get the ASCII string: |
bjo3rn | 0:c9bb3c9d5ce8 | 115 | String inString = myPort.readStringUntil('\n'); |
bjo3rn | 0:c9bb3c9d5ce8 | 116 | |
bjo3rn | 0:c9bb3c9d5ce8 | 117 | if (inString != null) { |
bjo3rn | 0:c9bb3c9d5ce8 | 118 | // trim off any whitespace: |
bjo3rn | 0:c9bb3c9d5ce8 | 119 | inString = trim(inString); |
bjo3rn | 0:c9bb3c9d5ce8 | 120 | // convert to an int and map to the screen height: |
bjo3rn | 0:c9bb3c9d5ce8 | 121 | float inFloat = float(inString); |
bjo3rn | 0:c9bb3c9d5ce8 | 122 | float screenY = map(inFloat, minVal, maxVal, 0, height); |
bjo3rn | 0:c9bb3c9d5ce8 | 123 | |
bjo3rn | 0:c9bb3c9d5ce8 | 124 | // draw the line from bottom of screen to desired height |
bjo3rn | 0:c9bb3c9d5ce8 | 125 | stroke(127, 34, 255); |
bjo3rn | 0:c9bb3c9d5ce8 | 126 | line(xPos, height, xPos, height - screenY); |
bjo3rn | 0:c9bb3c9d5ce8 | 127 | |
bjo3rn | 0:c9bb3c9d5ce8 | 128 | // at the edge of the screen, go back to the beginning: |
bjo3rn | 0:c9bb3c9d5ce8 | 129 | if (xPos >= width) { |
bjo3rn | 0:c9bb3c9d5ce8 | 130 | xPos = 0; |
bjo3rn | 0:c9bb3c9d5ce8 | 131 | background(0); |
bjo3rn | 0:c9bb3c9d5ce8 | 132 | } else { |
bjo3rn | 0:c9bb3c9d5ce8 | 133 | // increment the horizontal position: |
bjo3rn | 0:c9bb3c9d5ce8 | 134 | xPos++; |
bjo3rn | 0:c9bb3c9d5ce8 | 135 | } |
bjo3rn | 0:c9bb3c9d5ce8 | 136 | } |
bjo3rn | 0:c9bb3c9d5ce8 | 137 | } |
bjo3rn | 0:c9bb3c9d5ce8 | 138 | */ |