The Nintendo 64 Controller Interface is an mbed library that allows one or more Nintendo 64 controllers to be used as input devices for the mbed. With this library, one will be able to control games created for an mbed using a Nintendo 64 controller. In addition, the library can easily be used to forward N64 inputs to a computer. Using the N64 Controller executable, one can communicate with multiple controllers.
main.cpp@0:95064759a964, 2016-04-28 (annotated)
- Committer:
- fomartin
- Date:
- Thu Apr 28 00:10:38 2016 +0000
- Revision:
- 0:95064759a964
N64 Controller Interface;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fomartin | 0:95064759a964 | 1 | #include "mbed.h" |
fomartin | 0:95064759a964 | 2 | #include "N64Controller.h" |
fomartin | 0:95064759a964 | 3 | |
fomartin | 0:95064759a964 | 4 | N64Controller controller(p25, false); |
fomartin | 0:95064759a964 | 5 | |
fomartin | 0:95064759a964 | 6 | DigitalOut led1(LED1); |
fomartin | 0:95064759a964 | 7 | DigitalOut led2(LED2); |
fomartin | 0:95064759a964 | 8 | DigitalOut led3(LED3); |
fomartin | 0:95064759a964 | 9 | DigitalOut led4(LED4); |
fomartin | 0:95064759a964 | 10 | |
fomartin | 0:95064759a964 | 11 | Serial pc (USBTX, USBRX); |
fomartin | 0:95064759a964 | 12 | |
fomartin | 0:95064759a964 | 13 | //Timer timer; |
fomartin | 0:95064759a964 | 14 | |
fomartin | 0:95064759a964 | 15 | int main() { |
fomartin | 0:95064759a964 | 16 | while(1) |
fomartin | 0:95064759a964 | 17 | { |
fomartin | 0:95064759a964 | 18 | if(pc.getc() == '!') |
fomartin | 0:95064759a964 | 19 | { |
fomartin | 0:95064759a964 | 20 | //we had issues sending the raw binary over serial (probably because |
fomartin | 0:95064759a964 | 21 | //the serial connection was interpreting them as characters) |
fomartin | 0:95064759a964 | 22 | //so instead we will just send the raw ASCII for the number |
fomartin | 0:95064759a964 | 23 | //e.g., if inputData is 5267 we will send the string '0000005267' |
fomartin | 0:95064759a964 | 24 | //(max uint32 value is 10 digits, so we pad with 0 to ensure we |
fomartin | 0:95064759a964 | 25 | //are always sending 10 bytes |
fomartin | 0:95064759a964 | 26 | uint32_t data = controller.getInputData(); |
fomartin | 0:95064759a964 | 27 | char payload[10]; |
fomartin | 0:95064759a964 | 28 | sprintf(payload, "%010u", data); |
fomartin | 0:95064759a964 | 29 | |
fomartin | 0:95064759a964 | 30 | for(int i = 0; i < 10; i++) |
fomartin | 0:95064759a964 | 31 | { |
fomartin | 0:95064759a964 | 32 | pc.putc(payload[i]); |
fomartin | 0:95064759a964 | 33 | } |
fomartin | 0:95064759a964 | 34 | } |
fomartin | 0:95064759a964 | 35 | else |
fomartin | 0:95064759a964 | 36 | { |
fomartin | 0:95064759a964 | 37 | led1 = 1; |
fomartin | 0:95064759a964 | 38 | led2 = 1; |
fomartin | 0:95064759a964 | 39 | led3 = 1; |
fomartin | 0:95064759a964 | 40 | led4 = 1; |
fomartin | 0:95064759a964 | 41 | } |
fomartin | 0:95064759a964 | 42 | } |
fomartin | 0:95064759a964 | 43 | |
fomartin | 0:95064759a964 | 44 | //timer.start(); |
fomartin | 0:95064759a964 | 45 | // |
fomartin | 0:95064759a964 | 46 | // while(1) |
fomartin | 0:95064759a964 | 47 | // { |
fomartin | 0:95064759a964 | 48 | // if(controller.a()) |
fomartin | 0:95064759a964 | 49 | // { |
fomartin | 0:95064759a964 | 50 | // led1 = 1; |
fomartin | 0:95064759a964 | 51 | // } |
fomartin | 0:95064759a964 | 52 | // else |
fomartin | 0:95064759a964 | 53 | // { |
fomartin | 0:95064759a964 | 54 | // led1 = 0; |
fomartin | 0:95064759a964 | 55 | // } |
fomartin | 0:95064759a964 | 56 | // |
fomartin | 0:95064759a964 | 57 | // if(controller.b()) |
fomartin | 0:95064759a964 | 58 | // { |
fomartin | 0:95064759a964 | 59 | // led2 = 1; |
fomartin | 0:95064759a964 | 60 | // } |
fomartin | 0:95064759a964 | 61 | // else |
fomartin | 0:95064759a964 | 62 | // { |
fomartin | 0:95064759a964 | 63 | // led2 = 0; |
fomartin | 0:95064759a964 | 64 | // } |
fomartin | 0:95064759a964 | 65 | // |
fomartin | 0:95064759a964 | 66 | // if(controller.l()) |
fomartin | 0:95064759a964 | 67 | // { |
fomartin | 0:95064759a964 | 68 | // led3 = 1; |
fomartin | 0:95064759a964 | 69 | // } |
fomartin | 0:95064759a964 | 70 | // else |
fomartin | 0:95064759a964 | 71 | // { |
fomartin | 0:95064759a964 | 72 | // led3 = 0; |
fomartin | 0:95064759a964 | 73 | // } |
fomartin | 0:95064759a964 | 74 | // |
fomartin | 0:95064759a964 | 75 | // if(controller.r()) |
fomartin | 0:95064759a964 | 76 | // { |
fomartin | 0:95064759a964 | 77 | // led4 = 1; |
fomartin | 0:95064759a964 | 78 | // } |
fomartin | 0:95064759a964 | 79 | // else |
fomartin | 0:95064759a964 | 80 | // { |
fomartin | 0:95064759a964 | 81 | // led4 = 0; |
fomartin | 0:95064759a964 | 82 | // } |
fomartin | 0:95064759a964 | 83 | // |
fomartin | 0:95064759a964 | 84 | // //prints inputs to serial once every two seconds |
fomartin | 0:95064759a964 | 85 | // if(timer.read_ms() > 2000) |
fomartin | 0:95064759a964 | 86 | // { |
fomartin | 0:95064759a964 | 87 | // char output [1000]; |
fomartin | 0:95064759a964 | 88 | // memset(output, 0x0, 1000); |
fomartin | 0:95064759a964 | 89 | // int outputIndex = 0; |
fomartin | 0:95064759a964 | 90 | // |
fomartin | 0:95064759a964 | 91 | // sprintf(&output[outputIndex], "\n\rButtons Pressed:\n\n\r"); |
fomartin | 0:95064759a964 | 92 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 93 | // |
fomartin | 0:95064759a964 | 94 | // if(controller.a()) |
fomartin | 0:95064759a964 | 95 | // { |
fomartin | 0:95064759a964 | 96 | // sprintf(&output[outputIndex], "\tA\n\r"); |
fomartin | 0:95064759a964 | 97 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 98 | // } |
fomartin | 0:95064759a964 | 99 | // |
fomartin | 0:95064759a964 | 100 | // if(controller.b()) |
fomartin | 0:95064759a964 | 101 | // { |
fomartin | 0:95064759a964 | 102 | // sprintf(&output[outputIndex], "\tB\n\r"); |
fomartin | 0:95064759a964 | 103 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 104 | // } |
fomartin | 0:95064759a964 | 105 | // |
fomartin | 0:95064759a964 | 106 | // if(controller.z()) |
fomartin | 0:95064759a964 | 107 | // { |
fomartin | 0:95064759a964 | 108 | // sprintf(&output[outputIndex], "\tZ\n\r"); |
fomartin | 0:95064759a964 | 109 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 110 | // } |
fomartin | 0:95064759a964 | 111 | // |
fomartin | 0:95064759a964 | 112 | // if(controller.l()) |
fomartin | 0:95064759a964 | 113 | // { |
fomartin | 0:95064759a964 | 114 | // sprintf(&output[outputIndex], "\tL\n\r"); |
fomartin | 0:95064759a964 | 115 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 116 | // } |
fomartin | 0:95064759a964 | 117 | // |
fomartin | 0:95064759a964 | 118 | // if(controller.r()) |
fomartin | 0:95064759a964 | 119 | // { |
fomartin | 0:95064759a964 | 120 | // sprintf(&output[outputIndex], "\tR\n\r"); |
fomartin | 0:95064759a964 | 121 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 122 | // } |
fomartin | 0:95064759a964 | 123 | // |
fomartin | 0:95064759a964 | 124 | // if(controller.cUp()) |
fomartin | 0:95064759a964 | 125 | // { |
fomartin | 0:95064759a964 | 126 | // sprintf(&output[outputIndex], "\tC-Up\n\r"); |
fomartin | 0:95064759a964 | 127 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 128 | // } |
fomartin | 0:95064759a964 | 129 | // |
fomartin | 0:95064759a964 | 130 | // if(controller.cDown()) |
fomartin | 0:95064759a964 | 131 | // { |
fomartin | 0:95064759a964 | 132 | // sprintf(&output[outputIndex], "\tC-Down\n\r"); |
fomartin | 0:95064759a964 | 133 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 134 | // } |
fomartin | 0:95064759a964 | 135 | // |
fomartin | 0:95064759a964 | 136 | // if(controller.cLeft()) |
fomartin | 0:95064759a964 | 137 | // { |
fomartin | 0:95064759a964 | 138 | // sprintf(&output[outputIndex], "\tC-Left\n\r"); |
fomartin | 0:95064759a964 | 139 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 140 | // } |
fomartin | 0:95064759a964 | 141 | // |
fomartin | 0:95064759a964 | 142 | // if(controller.cRight()) |
fomartin | 0:95064759a964 | 143 | // { |
fomartin | 0:95064759a964 | 144 | // sprintf(&output[outputIndex], "\tC-Right\n\r"); |
fomartin | 0:95064759a964 | 145 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 146 | // } |
fomartin | 0:95064759a964 | 147 | // |
fomartin | 0:95064759a964 | 148 | // if(controller.dUp()) |
fomartin | 0:95064759a964 | 149 | // { |
fomartin | 0:95064759a964 | 150 | // sprintf(&output[outputIndex], "\tD-Up\n\r"); |
fomartin | 0:95064759a964 | 151 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 152 | // } |
fomartin | 0:95064759a964 | 153 | // |
fomartin | 0:95064759a964 | 154 | // if(controller.dDown()) |
fomartin | 0:95064759a964 | 155 | // { |
fomartin | 0:95064759a964 | 156 | // sprintf(&output[outputIndex], "\tD-Down\n\r"); |
fomartin | 0:95064759a964 | 157 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 158 | // } |
fomartin | 0:95064759a964 | 159 | // |
fomartin | 0:95064759a964 | 160 | // if(controller.dLeft()) |
fomartin | 0:95064759a964 | 161 | // { |
fomartin | 0:95064759a964 | 162 | // sprintf(&output[outputIndex], "\tD-Left\n\r"); |
fomartin | 0:95064759a964 | 163 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 164 | // } |
fomartin | 0:95064759a964 | 165 | // |
fomartin | 0:95064759a964 | 166 | // if(controller.dRight()) |
fomartin | 0:95064759a964 | 167 | // { |
fomartin | 0:95064759a964 | 168 | // sprintf(&output[outputIndex], "\tD-Right\n\r"); |
fomartin | 0:95064759a964 | 169 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 170 | // } |
fomartin | 0:95064759a964 | 171 | // |
fomartin | 0:95064759a964 | 172 | // sprintf(&output[outputIndex], "\nAnalog Position:\n\n\r"); |
fomartin | 0:95064759a964 | 173 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 174 | // |
fomartin | 0:95064759a964 | 175 | // sprintf(&output[outputIndex], "\tX: %d\n\r", controller.joyX()); |
fomartin | 0:95064759a964 | 176 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 177 | // |
fomartin | 0:95064759a964 | 178 | // sprintf(&output[outputIndex], "\tY: %d\n\r", controller.joyY()); |
fomartin | 0:95064759a964 | 179 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 180 | // |
fomartin | 0:95064759a964 | 181 | // sprintf(&output[outputIndex], "\n\r-----------------\n\r"); |
fomartin | 0:95064759a964 | 182 | // outputIndex += strlen(&output[outputIndex]); |
fomartin | 0:95064759a964 | 183 | // |
fomartin | 0:95064759a964 | 184 | // pc.printf("%s", output); |
fomartin | 0:95064759a964 | 185 | // |
fomartin | 0:95064759a964 | 186 | // timer.reset(); |
fomartin | 0:95064759a964 | 187 | // } |
fomartin | 0:95064759a964 | 188 | // } |
fomartin | 0:95064759a964 | 189 | } |