accelerometer trial
Dependencies: mbed C12832 MMA7660 USBDevice
main.cpp@21:f7e4b453f684, 2019-10-27 (annotated)
- Committer:
- wojt86
- Date:
- Sun Oct 27 17:13:56 2019 +0000
- Revision:
- 21:f7e4b453f684
- Parent:
- 20:53426ae5b7b1
original before changes (from 26.10)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 1:291a88a2c151 | 1 | #include "mbed.h" |
samux | 1:291a88a2c151 | 2 | #include "USBKeyboard.h" |
knasp | 7:ffe2220d3880 | 3 | #include "MMA7660.h" |
knasp | 10:150f8d03f18d | 4 | #include "C12832.h" |
knasp | 9:66681432403a | 5 | |
knasp | 9:66681432403a | 6 | |
wojt86 | 20:53426ae5b7b1 | 7 | MMA7660 Accelerometer (p28,p27); //define accelerometer |
wojt86 | 20:53426ae5b7b1 | 8 | USBKeyboard Usbkeyboard; //define keyboard |
wojt86 | 20:53426ae5b7b1 | 9 | C12832 LCD (p5,p7,p6,p8,p11); //define LCD |
wojt86 | 20:53426ae5b7b1 | 10 | DigitalOut led1(p12); //leds |
wojt86 | 20:53426ae5b7b1 | 11 | DigitalOut led2(p13); |
wojt86 | 20:53426ae5b7b1 | 12 | DigitalOut led3(p14); |
wojt86 | 20:53426ae5b7b1 | 13 | DigitalOut led4(p15); |
knasp | 10:150f8d03f18d | 14 | |
wojt86 | 20:53426ae5b7b1 | 15 | DigitalIn Switch1 (p8); //switch mode 1 (arrows) and 2 (letters) |
wojt86 | 20:53426ae5b7b1 | 16 | DigitalIn Switch2 (p16); //switch to numbers |
wojt86 | 21:f7e4b453f684 | 17 | DigitalIn Switch3 (p17); //enter |
wojt86 | 21:f7e4b453f684 | 18 | DigitalIn Switch4 (p18); //spacebar |
wojt86 | 20:53426ae5b7b1 | 19 | |
wojt86 | 20:53426ae5b7b1 | 20 | int main (void) //main program |
wojt86 | 20:53426ae5b7b1 | 21 | { |
wojt86 | 20:53426ae5b7b1 | 22 | //enum Orientation {Up, Down,Right, Left,Back, Front,Unknown}; |
wojt86 | 20:53426ae5b7b1 | 23 | MMA7660::Orientation myOrientation; //defining orientation |
knasp | 10:150f8d03f18d | 24 | |
wojt86 | 20:53426ae5b7b1 | 25 | while (true) { |
wojt86 | 20:53426ae5b7b1 | 26 | |
wojt86 | 20:53426ae5b7b1 | 27 | |
wojt86 | 20:53426ae5b7b1 | 28 | myOrientation = Accelerometer.getOrientation (); //getting the orientation from accelerometer |
wojt86 | 20:53426ae5b7b1 | 29 | |
wojt86 | 20:53426ae5b7b1 | 30 | |
wojt86 | 20:53426ae5b7b1 | 31 | if (Switch4){ |
wojt86 | 20:53426ae5b7b1 | 32 | |
wojt86 | 20:53426ae5b7b1 | 33 | Usbkeyboard.printf (" "); //spacebar |
wojt86 | 20:53426ae5b7b1 | 34 | |
wojt86 | 20:53426ae5b7b1 | 35 | } |
wojt86 | 20:53426ae5b7b1 | 36 | |
wojt86 | 20:53426ae5b7b1 | 37 | if (Switch3){ |
wojt86 | 20:53426ae5b7b1 | 38 | |
wojt86 | 21:f7e4b453f684 | 39 | Usbkeyboard.keyCode (RIGHT_ARROW); //enter will be HERE |
wojt86 | 20:53426ae5b7b1 | 40 | |
wojt86 | 20:53426ae5b7b1 | 41 | } |
wojt86 | 20:53426ae5b7b1 | 42 | |
wojt86 | 20:53426ae5b7b1 | 43 | if (Switch1){ //if the switch is on |
wojt86 | 20:53426ae5b7b1 | 44 | |
wojt86 | 20:53426ae5b7b1 | 45 | if (myOrientation == MMA7660::Up){ //if the orientation is up |
wojt86 | 20:53426ae5b7b1 | 46 | |
wojt86 | 20:53426ae5b7b1 | 47 | LCD.printf("Left"); //print "left" on LCD (due to orientation of the accelerometer on the limb, the directions are changed) |
wojt86 | 20:53426ae5b7b1 | 48 | Usbkeyboard.keyCode (LEFT_ARROW); //device simulates pressing "left arrow key" on the keyboard |
wojt86 | 20:53426ae5b7b1 | 49 | led2=0; //led off |
wojt86 | 20:53426ae5b7b1 | 50 | led1=0; //led off |
wojt86 | 20:53426ae5b7b1 | 51 | led3=0; //led off |
wojt86 | 20:53426ae5b7b1 | 52 | led4=1; //led on |
wojt86 | 20:53426ae5b7b1 | 53 | } |
wojt86 | 20:53426ae5b7b1 | 54 | else if (myOrientation == MMA7660::Down){ |
wojt86 | 20:53426ae5b7b1 | 55 | |
wojt86 | 20:53426ae5b7b1 | 56 | LCD.printf("Right"); |
wojt86 | 20:53426ae5b7b1 | 57 | Usbkeyboard.keyCode (RIGHT_ARROW); |
wojt86 | 20:53426ae5b7b1 | 58 | led3=0; |
wojt86 | 20:53426ae5b7b1 | 59 | led1=0; |
wojt86 | 20:53426ae5b7b1 | 60 | led2=1; |
wojt86 | 20:53426ae5b7b1 | 61 | led4=0; |
wojt86 | 20:53426ae5b7b1 | 62 | } |
wojt86 | 20:53426ae5b7b1 | 63 | else if (myOrientation == MMA7660::Left){ |
wojt86 | 20:53426ae5b7b1 | 64 | |
wojt86 | 20:53426ae5b7b1 | 65 | LCD.printf("UP"); |
wojt86 | 20:53426ae5b7b1 | 66 | Usbkeyboard.keyCode (UP_ARROW); |
wojt86 | 20:53426ae5b7b1 | 67 | led1=1; |
wojt86 | 20:53426ae5b7b1 | 68 | led3=0; |
wojt86 | 20:53426ae5b7b1 | 69 | led2=0; |
wojt86 | 20:53426ae5b7b1 | 70 | led4=0; |
wojt86 | 20:53426ae5b7b1 | 71 | } |
wojt86 | 20:53426ae5b7b1 | 72 | else if (myOrientation == MMA7660::Right){ |
wojt86 | 20:53426ae5b7b1 | 73 | |
wojt86 | 20:53426ae5b7b1 | 74 | LCD.printf("DOWN"); |
wojt86 | 20:53426ae5b7b1 | 75 | Usbkeyboard.keyCode (DOWN_ARROW); |
wojt86 | 20:53426ae5b7b1 | 76 | led4=0; |
wojt86 | 20:53426ae5b7b1 | 77 | led1=0; |
wojt86 | 20:53426ae5b7b1 | 78 | led3=1; |
wojt86 | 20:53426ae5b7b1 | 79 | led2=0; |
wojt86 | 20:53426ae5b7b1 | 80 | |
wojt86 | 20:53426ae5b7b1 | 81 | |
wojt86 | 20:53426ae5b7b1 | 82 | } |
wojt86 | 20:53426ae5b7b1 | 83 | else if (myOrientation == MMA7660::Unknown){ |
wojt86 | 20:53426ae5b7b1 | 84 | |
wojt86 | 20:53426ae5b7b1 | 85 | LCD.printf("Don't Know"); |
wojt86 | 20:53426ae5b7b1 | 86 | |
wojt86 | 20:53426ae5b7b1 | 87 | } |
wojt86 | 20:53426ae5b7b1 | 88 | } |
wojt86 | 20:53426ae5b7b1 | 89 | |
wojt86 | 20:53426ae5b7b1 | 90 | else { //if the switch is off than |
wojt86 | 20:53426ae5b7b1 | 91 | |
wojt86 | 20:53426ae5b7b1 | 92 | if (myOrientation == MMA7660::Up){ |
wojt86 | 20:53426ae5b7b1 | 93 | |
wojt86 | 20:53426ae5b7b1 | 94 | LCD.printf("LEFT"); |
wojt86 | 20:53426ae5b7b1 | 95 | Usbkeyboard.printf ("a\0"); //instead of simulating pressing of the "left arrow", simulates pressing "a key" |
wojt86 | 20:53426ae5b7b1 | 96 | led2=0; |
wojt86 | 20:53426ae5b7b1 | 97 | led1=0; |
wojt86 | 20:53426ae5b7b1 | 98 | led3=0; |
wojt86 | 20:53426ae5b7b1 | 99 | led4=1; |
wojt86 | 20:53426ae5b7b1 | 100 | } |
wojt86 | 20:53426ae5b7b1 | 101 | else if (myOrientation == MMA7660::Down){ |
wojt86 | 20:53426ae5b7b1 | 102 | |
wojt86 | 20:53426ae5b7b1 | 103 | LCD.printf("right"); |
wojt86 | 20:53426ae5b7b1 | 104 | Usbkeyboard.printf ("d\0"); |
wojt86 | 20:53426ae5b7b1 | 105 | led3=0; |
wojt86 | 20:53426ae5b7b1 | 106 | led1=0; |
wojt86 | 20:53426ae5b7b1 | 107 | led2=1; |
wojt86 | 20:53426ae5b7b1 | 108 | led4=0; |
wojt86 | 20:53426ae5b7b1 | 109 | } |
wojt86 | 20:53426ae5b7b1 | 110 | else if (myOrientation == MMA7660::Left){ |
wojt86 | 20:53426ae5b7b1 | 111 | |
wojt86 | 20:53426ae5b7b1 | 112 | LCD.printf("Up"); |
wojt86 | 20:53426ae5b7b1 | 113 | Usbkeyboard.printf ("w\0"); |
wojt86 | 20:53426ae5b7b1 | 114 | led1=1; |
wojt86 | 20:53426ae5b7b1 | 115 | led3=0; |
wojt86 | 20:53426ae5b7b1 | 116 | led2=0; |
wojt86 | 20:53426ae5b7b1 | 117 | led4=0; |
wojt86 | 20:53426ae5b7b1 | 118 | } |
wojt86 | 20:53426ae5b7b1 | 119 | else if (myOrientation == MMA7660::Right){ |
wojt86 | 20:53426ae5b7b1 | 120 | |
wojt86 | 20:53426ae5b7b1 | 121 | LCD.printf("Down"); |
wojt86 | 20:53426ae5b7b1 | 122 | Usbkeyboard.printf ("s\0"); |
wojt86 | 20:53426ae5b7b1 | 123 | led4=0; |
wojt86 | 20:53426ae5b7b1 | 124 | led1=0; |
wojt86 | 20:53426ae5b7b1 | 125 | led3=1; |
wojt86 | 20:53426ae5b7b1 | 126 | led2=0; |
wojt86 | 20:53426ae5b7b1 | 127 | |
wojt86 | 20:53426ae5b7b1 | 128 | |
wojt86 | 20:53426ae5b7b1 | 129 | |
wojt86 | 20:53426ae5b7b1 | 130 | } |
wojt86 | 20:53426ae5b7b1 | 131 | else if (myOrientation == MMA7660::Unknown){ |
wojt86 | 20:53426ae5b7b1 | 132 | |
wojt86 | 20:53426ae5b7b1 | 133 | LCD.printf("Don't Know"); |
wojt86 | 20:53426ae5b7b1 | 134 | |
wojt86 | 20:53426ae5b7b1 | 135 | } |
wojt86 | 20:53426ae5b7b1 | 136 | |
wojt86 | 20:53426ae5b7b1 | 137 | } |
wojt86 | 20:53426ae5b7b1 | 138 | |
knasp | 10:150f8d03f18d | 139 | |
wojt86 | 20:53426ae5b7b1 | 140 | } |
wojt86 | 21:f7e4b453f684 | 141 | return false; |
wojt86 | 20:53426ae5b7b1 | 142 | } |