Small portion of code to verify correctly working hardware and connections

Dependencies:   N5110 mbed

Committer:
el14jw
Date:
Tue Mar 01 18:54:39 2016 +0000
Revision:
0:c0833789892f
Initial code to check hardware connections in the components stated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el14jw 0:c0833789892f 1 /*
el14jw 0:c0833789892f 2 Initial prototyping to check hardware connections
el14jw 0:c0833789892f 3
el14jw 0:c0833789892f 4 Testing the following connections:
el14jw 0:c0833789892f 5
el14jw 0:c0833789892f 6 Joystick:
el14jw 0:c0833789892f 7 xPot yPot button
el14jw 0:c0833789892f 8 AnalogIn PTB11, AnalogIn PTB10, PTB9
el14jw 0:c0833789892f 9
el14jw 0:c0833789892f 10 Nokia 5110 lcd display: (Handled by library)
el14jw 0:c0833789892f 11 VCC SCE RST D/C MOSI SCLK LED
el14jw 0:c0833789892f 12 PTE26, PTA0, PTC4, PTD0, PTD2, PTD1, PTC3
el14jw 0:c0833789892f 13
el14jw 0:c0833789892f 14 buttonA buttonB g_led buzzer
el14jw 0:c0833789892f 15 PTA1 PTB23 PWM PTC11 Undecided between AnalogOut DAC0_OUT or PWM PTC10 (decision will be based on sound quality)
el14jw 0:c0833789892f 16
el14jw 0:c0833789892f 17 01/03/16
el14jw 0:c0833789892f 18 By Joel W. Webb
el14jw 0:c0833789892f 19 Revision 1.0
el14jw 0:c0833789892f 20 */
el14jw 0:c0833789892f 21
el14jw 0:c0833789892f 22 #include "mbed.h"
el14jw 0:c0833789892f 23 #include "N5110.h"
el14jw 0:c0833789892f 24
el14jw 0:c0833789892f 25 #define DIRECTION_TOLERANCE 0.05
el14jw 0:c0833789892f 26
el14jw 0:c0833789892f 27 // Nokia lcd object
el14jw 0:c0833789892f 28 // VCC SCE RST D/C MOSI SCLK LED
el14jw 0:c0833789892f 29 N5110 lcd(PTE26, PTA0, PTC4, PTD0, PTD2, PTD1, PTC3);
el14jw 0:c0833789892f 30
el14jw 0:c0833789892f 31 // Joystick
el14jw 0:c0833789892f 32 AnalogIn xPot(PTB11);
el14jw 0:c0833789892f 33 AnalogIn yPot(PTB10);
el14jw 0:c0833789892f 34 DigitalIn button(PTB9);
el14jw 0:c0833789892f 35
el14jw 0:c0833789892f 36 enum DirectionName {
el14jw 0:c0833789892f 37 UP,
el14jw 0:c0833789892f 38 DOWN,
el14jw 0:c0833789892f 39 LEFT,
el14jw 0:c0833789892f 40 RIGHT,
el14jw 0:c0833789892f 41 CENTRE,
el14jw 0:c0833789892f 42 UNKNOWN
el14jw 0:c0833789892f 43 };
el14jw 0:c0833789892f 44
el14jw 0:c0833789892f 45 // struct for Joystick
el14jw 0:c0833789892f 46 typedef struct JoyStick Joystick;
el14jw 0:c0833789892f 47 struct JoyStick {
el14jw 0:c0833789892f 48 float x; // current x value
el14jw 0:c0833789892f 49 float x0; // 'centred' x value
el14jw 0:c0833789892f 50 float y; // current y value
el14jw 0:c0833789892f 51 float y0; // 'centred' y value
el14jw 0:c0833789892f 52 int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed)
el14jw 0:c0833789892f 53 DirectionName direction; // current direction
el14jw 0:c0833789892f 54 };
el14jw 0:c0833789892f 55 // create struct variable
el14jw 0:c0833789892f 56 Joystick joystick;
el14jw 0:c0833789892f 57 Ticker pollJoystick; // Ticker interrput for polling the joystick
el14jw 0:c0833789892f 58
el14jw 0:c0833789892f 59 // Other inputs/outputs
el14jw 0:c0833789892f 60 InterruptIn buttonA(PTA1);
el14jw 0:c0833789892f 61 InterruptIn buttonB(PTB23);
el14jw 0:c0833789892f 62 PwmOut g_led(PTC11);
el14jw 0:c0833789892f 63 DigitalOut r_led(LED1); // Testing led
el14jw 0:c0833789892f 64
el14jw 0:c0833789892f 65 //Global Flags defined here
el14jw 0:c0833789892f 66 int g_joystick_flag;
el14jw 0:c0833789892f 67 int g_buttonA_flag;
el14jw 0:c0833789892f 68 int g_buttonB_flag;
el14jw 0:c0833789892f 69
el14jw 0:c0833789892f 70 // ISR defined here
el14jw 0:c0833789892f 71 void buttonA_isr(){
el14jw 0:c0833789892f 72 g_buttonA_flag = 1;
el14jw 0:c0833789892f 73 }
el14jw 0:c0833789892f 74 void buttonB_isr(){
el14jw 0:c0833789892f 75 g_buttonB_flag = 1;
el14jw 0:c0833789892f 76 }
el14jw 0:c0833789892f 77
el14jw 0:c0833789892f 78 // Functions defined here
el14jw 0:c0833789892f 79 void initInputs();
el14jw 0:c0833789892f 80 void updateJoystick();
el14jw 0:c0833789892f 81 void calibrateJoystick();
el14jw 0:c0833789892f 82
el14jw 0:c0833789892f 83 int main()
el14jw 0:c0833789892f 84 {
el14jw 0:c0833789892f 85 initInputs();
el14jw 0:c0833789892f 86 pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second
el14jw 0:c0833789892f 87
el14jw 0:c0833789892f 88 while (true) {
el14jw 0:c0833789892f 89
el14jw 0:c0833789892f 90 if (g_buttonA_flag){
el14jw 0:c0833789892f 91 g_buttonA_flag = 0;
el14jw 0:c0833789892f 92 lcd.clear();
el14jw 0:c0833789892f 93 lcd.printString("Button A",0,0);
el14jw 0:c0833789892f 94 }
el14jw 0:c0833789892f 95 if (g_buttonB_flag){
el14jw 0:c0833789892f 96 g_buttonB_flag = 0;
el14jw 0:c0833789892f 97 lcd.clear();
el14jw 0:c0833789892f 98 lcd.printString("Button B",0,1);
el14jw 0:c0833789892f 99 }
el14jw 0:c0833789892f 100 if (g_joystick_flag){
el14jw 0:c0833789892f 101 g_joystick_flag = 0;
el14jw 0:c0833789892f 102 lcd.printString(" ",0,2);
el14jw 0:c0833789892f 103 if (joystick.direction == 0){ lcd.printString("UP",0,2); }
el14jw 0:c0833789892f 104 else if (joystick.direction == 1){ lcd.printString("DOWN",0,2); }
el14jw 0:c0833789892f 105 else if (joystick.direction == 2){ lcd.printString("LEFT",0,2); }
el14jw 0:c0833789892f 106 else if (joystick.direction == 3){ lcd.printString("RIGHT",0,2); }
el14jw 0:c0833789892f 107 else if (joystick.direction == 4){ lcd.printString("CENTRE",0,2); }
el14jw 0:c0833789892f 108 else if (joystick.direction == 5){ lcd.printString("UNDEFINED",0,2); }
el14jw 0:c0833789892f 109 if (joystick.button){ lcd.printString("Button J",0,3); }
el14jw 0:c0833789892f 110 }
el14jw 0:c0833789892f 111
el14jw 0:c0833789892f 112 lcd.refresh();
el14jw 0:c0833789892f 113 sleep();
el14jw 0:c0833789892f 114
el14jw 0:c0833789892f 115 }
el14jw 0:c0833789892f 116 }
el14jw 0:c0833789892f 117
el14jw 0:c0833789892f 118
el14jw 0:c0833789892f 119
el14jw 0:c0833789892f 120 void initInputs(){
el14jw 0:c0833789892f 121
el14jw 0:c0833789892f 122 r_led = 1;
el14jw 0:c0833789892f 123 // Initialising power LED
el14jw 0:c0833789892f 124 g_led.period(0.001); // Frequency of 1kHz
el14jw 0:c0833789892f 125 g_led = 0.25; // Duty cycle of 0.25
el14jw 0:c0833789892f 126
el14jw 0:c0833789892f 127 // Setting up Interrupt service routines for input buttons
el14jw 0:c0833789892f 128 // Note no pull mode is specified so default pulldown resistors will be used
el14jw 0:c0833789892f 129 buttonA.rise(&buttonA_isr);
el14jw 0:c0833789892f 130 buttonB.rise(&buttonB_isr);
el14jw 0:c0833789892f 131 buttonA.mode(PullDown);
el14jw 0:c0833789892f 132 buttonB.mode(PullDown);
el14jw 0:c0833789892f 133
el14jw 0:c0833789892f 134
el14jw 0:c0833789892f 135 //Testing LCD display works (splash screen)
el14jw 0:c0833789892f 136 lcd.init();
el14jw 0:c0833789892f 137 wait(1.0);
el14jw 0:c0833789892f 138 lcd.printString("Calibrating",0,0);
el14jw 0:c0833789892f 139 lcd.printString("Do not move",0,1);
el14jw 0:c0833789892f 140 lcd.printString("Joystick",0,2);
el14jw 0:c0833789892f 141 lcd.refresh();
el14jw 0:c0833789892f 142
el14jw 0:c0833789892f 143 calibrateJoystick();
el14jw 0:c0833789892f 144
el14jw 0:c0833789892f 145 wait(1.0);
el14jw 0:c0833789892f 146 lcd.clear();
el14jw 0:c0833789892f 147
el14jw 0:c0833789892f 148 }
el14jw 0:c0833789892f 149
el14jw 0:c0833789892f 150 // read default positions of the joystick to calibrate later readings
el14jw 0:c0833789892f 151 void calibrateJoystick()
el14jw 0:c0833789892f 152 {
el14jw 0:c0833789892f 153 button.mode(PullDown);
el14jw 0:c0833789892f 154 // must not move during calibration
el14jw 0:c0833789892f 155 joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly)
el14jw 0:c0833789892f 156 joystick.y0 = yPot;
el14jw 0:c0833789892f 157 }
el14jw 0:c0833789892f 158 void updateJoystick()
el14jw 0:c0833789892f 159 {
el14jw 0:c0833789892f 160 // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred)
el14jw 0:c0833789892f 161 joystick.x = xPot - joystick.x0;
el14jw 0:c0833789892f 162 joystick.y = yPot - joystick.y0;
el14jw 0:c0833789892f 163 // read button state
el14jw 0:c0833789892f 164 joystick.button = button;
el14jw 0:c0833789892f 165
el14jw 0:c0833789892f 166 // calculate direction depending on x,y values
el14jw 0:c0833789892f 167 // tolerance allows a little lee-way in case joystick not exactly in the stated direction
el14jw 0:c0833789892f 168 if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14jw 0:c0833789892f 169 joystick.direction = CENTRE;
el14jw 0:c0833789892f 170 } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14jw 0:c0833789892f 171 joystick.direction = UP;
el14jw 0:c0833789892f 172 } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) {
el14jw 0:c0833789892f 173 joystick.direction = DOWN;
el14jw 0:c0833789892f 174 } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el14jw 0:c0833789892f 175 joystick.direction = RIGHT;
el14jw 0:c0833789892f 176 } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) {
el14jw 0:c0833789892f 177 joystick.direction = LEFT;
el14jw 0:c0833789892f 178 } else {
el14jw 0:c0833789892f 179 joystick.direction = UNKNOWN;
el14jw 0:c0833789892f 180 }
el14jw 0:c0833789892f 181
el14jw 0:c0833789892f 182 // set flag for printing
el14jw 0:c0833789892f 183 g_joystick_flag = 1;
el14jw 0:c0833789892f 184 }
el14jw 0:c0833789892f 185