Joel Webb
/
InitialHardwareConnectionChecking
Small portion of code to verify correctly working hardware and connections
main.cpp
- Committer:
- el14jw
- Date:
- 2016-03-01
- Revision:
- 0:c0833789892f
File content as of revision 0:c0833789892f:
/* Initial prototyping to check hardware connections Testing the following connections: Joystick: xPot yPot button AnalogIn PTB11, AnalogIn PTB10, PTB9 Nokia 5110 lcd display: (Handled by library) VCC SCE RST D/C MOSI SCLK LED PTE26, PTA0, PTC4, PTD0, PTD2, PTD1, PTC3 buttonA buttonB g_led buzzer PTA1 PTB23 PWM PTC11 Undecided between AnalogOut DAC0_OUT or PWM PTC10 (decision will be based on sound quality) 01/03/16 By Joel W. Webb Revision 1.0 */ #include "mbed.h" #include "N5110.h" #define DIRECTION_TOLERANCE 0.05 // Nokia lcd object // VCC SCE RST D/C MOSI SCLK LED N5110 lcd(PTE26, PTA0, PTC4, PTD0, PTD2, PTD1, PTC3); // Joystick AnalogIn xPot(PTB11); AnalogIn yPot(PTB10); DigitalIn button(PTB9); enum DirectionName { UP, DOWN, LEFT, RIGHT, CENTRE, UNKNOWN }; // struct for Joystick typedef struct JoyStick Joystick; struct JoyStick { float x; // current x value float x0; // 'centred' x value float y; // current y value float y0; // 'centred' y value int button; // button state (assume pull-down used, so 1 = pressed, 0 = unpressed) DirectionName direction; // current direction }; // create struct variable Joystick joystick; Ticker pollJoystick; // Ticker interrput for polling the joystick // Other inputs/outputs InterruptIn buttonA(PTA1); InterruptIn buttonB(PTB23); PwmOut g_led(PTC11); DigitalOut r_led(LED1); // Testing led //Global Flags defined here int g_joystick_flag; int g_buttonA_flag; int g_buttonB_flag; // ISR defined here void buttonA_isr(){ g_buttonA_flag = 1; } void buttonB_isr(){ g_buttonB_flag = 1; } // Functions defined here void initInputs(); void updateJoystick(); void calibrateJoystick(); int main() { initInputs(); pollJoystick.attach(&updateJoystick,1.0/10.0); // read joystick 10 times per second while (true) { if (g_buttonA_flag){ g_buttonA_flag = 0; lcd.clear(); lcd.printString("Button A",0,0); } if (g_buttonB_flag){ g_buttonB_flag = 0; lcd.clear(); lcd.printString("Button B",0,1); } if (g_joystick_flag){ g_joystick_flag = 0; lcd.printString(" ",0,2); if (joystick.direction == 0){ lcd.printString("UP",0,2); } else if (joystick.direction == 1){ lcd.printString("DOWN",0,2); } else if (joystick.direction == 2){ lcd.printString("LEFT",0,2); } else if (joystick.direction == 3){ lcd.printString("RIGHT",0,2); } else if (joystick.direction == 4){ lcd.printString("CENTRE",0,2); } else if (joystick.direction == 5){ lcd.printString("UNDEFINED",0,2); } if (joystick.button){ lcd.printString("Button J",0,3); } } lcd.refresh(); sleep(); } } void initInputs(){ r_led = 1; // Initialising power LED g_led.period(0.001); // Frequency of 1kHz g_led = 0.25; // Duty cycle of 0.25 // Setting up Interrupt service routines for input buttons // Note no pull mode is specified so default pulldown resistors will be used buttonA.rise(&buttonA_isr); buttonB.rise(&buttonB_isr); buttonA.mode(PullDown); buttonB.mode(PullDown); //Testing LCD display works (splash screen) lcd.init(); wait(1.0); lcd.printString("Calibrating",0,0); lcd.printString("Do not move",0,1); lcd.printString("Joystick",0,2); lcd.refresh(); calibrateJoystick(); wait(1.0); lcd.clear(); } // read default positions of the joystick to calibrate later readings void calibrateJoystick() { button.mode(PullDown); // must not move during calibration joystick.x0 = xPot; // initial positions in the range 0.0 to 1.0 (0.5 if centred exactly) joystick.y0 = yPot; } void updateJoystick() { // read current joystick values relative to calibrated values (in range -0.5 to 0.5, 0.0 is centred) joystick.x = xPot - joystick.x0; joystick.y = yPot - joystick.y0; // read button state joystick.button = button; // calculate direction depending on x,y values // tolerance allows a little lee-way in case joystick not exactly in the stated direction if ( fabs(joystick.y) < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = CENTRE; } else if ( joystick.y > DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = UP; } else if ( joystick.y < DIRECTION_TOLERANCE && fabs(joystick.x) < DIRECTION_TOLERANCE) { joystick.direction = DOWN; } else if ( joystick.x > DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { joystick.direction = RIGHT; } else if ( joystick.x < DIRECTION_TOLERANCE && fabs(joystick.y) < DIRECTION_TOLERANCE) { joystick.direction = LEFT; } else { joystick.direction = UNKNOWN; } // set flag for printing g_joystick_flag = 1; }