Yehowshua Immanuel / Mbed 2 deprecated Generic_Platformer

Dependencies:   mbed 4DGL-uLCD-SE MMA8452

Committer:
Yehowshua
Date:
Sat Mar 16 05:25:19 2019 +0000
Revision:
4:fb7d49e660f2
Parent:
inputs.h@0:2f4225a589d1
now with inputs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yehowshua 0:2f4225a589d1 1 #pragma once
Yehowshua 4:fb7d49e660f2 2 #include "inputs.h"
Yehowshua 4:fb7d49e660f2 3
Yehowshua 4:fb7d49e660f2 4 MMA8452 acc(p28, p27, 100000); // Accelerometer
Yehowshua 4:fb7d49e660f2 5 DigitalIn button1(p21); // Pushbuttons (pin)
Yehowshua 4:fb7d49e660f2 6 DigitalIn button2(p22);
Yehowshua 4:fb7d49e660f2 7 DigitalIn button3(p24);
Yehowshua 4:fb7d49e660f2 8
Yehowshua 4:fb7d49e660f2 9 inputs in;
Yehowshua 0:2f4225a589d1 10
Yehowshua 4:fb7d49e660f2 11 void inputs_init(void){
Yehowshua 4:fb7d49e660f2 12 button1.mode(PullUp);
Yehowshua 4:fb7d49e660f2 13 button2.mode(PullUp);
Yehowshua 4:fb7d49e660f2 14 button3.mode(PullUp);
Yehowshua 4:fb7d49e660f2 15 }
Yehowshua 4:fb7d49e660f2 16
Yehowshua 4:fb7d49e660f2 17 int get_inputs(void){
Yehowshua 4:fb7d49e660f2 18 //read inputs
Yehowshua 4:fb7d49e660f2 19 in.b1 = !button1; // Inverted, because low voltage means "pressed"
Yehowshua 4:fb7d49e660f2 20 in.b2 = !button2;
Yehowshua 4:fb7d49e660f2 21 in.b3 = !button3;
Yehowshua 4:fb7d49e660f2 22 acc.readXYZGravity(&in.a_x, &in.a_y, &in.a_z); // Read gravity
Yehowshua 4:fb7d49e660f2 23 in.a_y = -in.a_y; // Accelerometer is right handed, everything else is left handed
Yehowshua 0:2f4225a589d1 24
Yehowshua 4:fb7d49e660f2 25 //process inputs
Yehowshua 4:fb7d49e660f2 26 // First, check action buttons
Yehowshua 4:fb7d49e660f2 27 if (in.b1) return ACTION_BUTTON;
Yehowshua 4:fb7d49e660f2 28 if (in.b2) return BOMB_BUTTON;
Yehowshua 0:2f4225a589d1 29
Yehowshua 4:fb7d49e660f2 30 // Otherwise, compute motion
Yehowshua 4:fb7d49e660f2 31 double thresh = 0.2;
Yehowshua 4:fb7d49e660f2 32 if (fabs(in.a_x) + fabs(in.a_y) > thresh)
Yehowshua 4:fb7d49e660f2 33 {
Yehowshua 4:fb7d49e660f2 34 #define PI 3.1415926
Yehowshua 4:fb7d49e660f2 35 double angle = atan2(in.a_y, in.a_x); // value on [-pi, pi]
Yehowshua 4:fb7d49e660f2 36 // pc.printf("atan: %f\r\n", angle);
Yehowshua 4:fb7d49e660f2 37 if (angle > 3*PI/4 || angle < -3*PI/4) return GO_LEFT;
Yehowshua 4:fb7d49e660f2 38 else if (angle > PI/4) return GO_DOWN;
Yehowshua 4:fb7d49e660f2 39 else if (angle > -PI/4) return GO_RIGHT;
Yehowshua 4:fb7d49e660f2 40 else return GO_UP;
Yehowshua 4:fb7d49e660f2 41 }
Yehowshua 4:fb7d49e660f2 42 return NO_ACTION;
Yehowshua 4:fb7d49e660f2 43
Yehowshua 4:fb7d49e660f2 44 }