diff --git a/src/hand-controller/buttons.cpp b/src/hand-controller/buttons.cpp index 6782e71..0f7ef26 100644 --- a/src/hand-controller/buttons.cpp +++ b/src/hand-controller/buttons.cpp @@ -1,9 +1,10 @@ +#include "buttons.h" #include "handController.h" #include // Button variables: -int throttle = 0; -bool armed = false; +static int throttle = 0; +static bool armed = false; const int THROTTLE_THRESHOLD = 40; const int THROTTLE_MIN = 5; @@ -31,7 +32,7 @@ static void onButtonAB(MicroBitEvent) { // uBit.display.print(armed ? "1" : "0"); } -void setBtnsHandlers() { +void SetBtnsHandlers() { uBit.messageBus.listen(DEVICE_ID_BUTTON_A, DEVICE_BUTTON_EVT_CLICK, onButtonA); uBit.messageBus.listen(DEVICE_ID_BUTTON_B, DEVICE_BUTTON_EVT_CLICK, @@ -40,7 +41,11 @@ void setBtnsHandlers() { onButtonAB); } -void checkPanic() { +int GetThrottle() { return throttle; } + +bool IsArmed() { return armed; } + +void CheckPanic() { if (uBit.accelerometer.getGesture() == ACCELEROMETER_EVT_SHAKE) { armed = false; throttle = 0; diff --git a/src/hand-controller/buttons.h b/src/hand-controller/buttons.h new file mode 100644 index 0000000..cb5db72 --- /dev/null +++ b/src/hand-controller/buttons.h @@ -0,0 +1,7 @@ +#pragma once + +void SetBtnsHandlers(); +int GetThrottle(); +bool IsArmed(); + +void CheckPanic(); \ No newline at end of file diff --git a/src/hand-controller/handController.h b/src/hand-controller/handController.h index 5c51eb2..b96aa6b 100644 --- a/src/hand-controller/handController.h +++ b/src/hand-controller/handController.h @@ -1,3 +1,8 @@ +#include "buttons.h" +#include "orientation.h" +#include "pins.h" +#include "transmitter.h" +#include "view.h" #include #ifndef HAND_CONTROLLER_H @@ -7,26 +12,8 @@ #ifdef MICROBIT_UBIT_AS_STATIC_OBJECT extern MicroBit uBit; // Target the same uBit as in main.cpp - -// Hand controller variables, PARTY -extern int pitch; -extern bool armed; -extern int roll; -extern int throttle; -extern int yaw; #else extern MicroBit &uBit; #endif -// Setup peripherals: -void setBtnsHandlers(); -void setP1High(); - -// Monitoring: -void setOrientation(); -void updateDisplay(); -void checkPanic(); -void transmittData(); -void readPins(); - #endif diff --git a/src/hand-controller/orientation.cpp b/src/hand-controller/orientation.cpp index 86ba48a..01c916a 100644 --- a/src/hand-controller/orientation.cpp +++ b/src/hand-controller/orientation.cpp @@ -1,10 +1,10 @@ +#include "orientation.h" #include "handController.h" #include -#include // orientation variables: -int pitch = 0; -int roll = 0; +static int pitch = 0; +static int roll = 0; const int ANGULAR_THRESHOLD = 45; const int STEADY_STATE_THRESHOLD = 15; @@ -27,7 +27,7 @@ static int clampAngles(int value, int threshold) { return clampedValue; } -void setOrientation() { +void SetOrientation() { int measuredPitch = uBit.accelerometer.getPitch(); pitch = clampAngles(measuredPitch, ANGULAR_THRESHOLD); @@ -36,4 +36,8 @@ void setOrientation() { // uBit.display.scroll(pitch); // uBit.display.scroll(roll); -} \ No newline at end of file +} + +int GetPitch() { return pitch; } + +int GetRoll() { return roll; } \ No newline at end of file diff --git a/src/hand-controller/orientation.h b/src/hand-controller/orientation.h new file mode 100644 index 0000000..615cbd8 --- /dev/null +++ b/src/hand-controller/orientation.h @@ -0,0 +1,6 @@ +#pragma once +#include + +void SetOrientation(); +int GetPitch(); +int GetRoll(); \ No newline at end of file diff --git a/src/hand-controller/pins.cpp b/src/hand-controller/pins.cpp index 687ba77..90b6529 100644 --- a/src/hand-controller/pins.cpp +++ b/src/hand-controller/pins.cpp @@ -1,7 +1,8 @@ +#include "pins.h" #include "handController.h" #include -int yaw = 0; +static int yaw = 0; // ADC has a resolution of 1 Byte // There are different thresholds for the two pins, as P0 is more sensible @@ -33,7 +34,7 @@ static int setYawFromP2Read(int meas) { } } -void setP1High() { +void SetP1High() { int succ = uBit.io.P1.setAnalogValue(MAX_PWM_VALUE); if (succ != DEVICE_OK) { uBit.display.scroll("P1"); @@ -41,7 +42,7 @@ void setP1High() { } } -void readPins() { +void ReadPins() { int p0 = uBit.io.P0.getAnalogValue(); int p2 = uBit.io.P2.getAnalogValue(); @@ -54,4 +55,6 @@ void readPins() { } else if (p2 > p0) { yaw = setYawFromP2Read(p2); } -} \ No newline at end of file +} + +int GetYaw() { return yaw; } \ No newline at end of file diff --git a/src/hand-controller/pins.h b/src/hand-controller/pins.h new file mode 100644 index 0000000..8ef3583 --- /dev/null +++ b/src/hand-controller/pins.h @@ -0,0 +1,5 @@ +#pragma once + +void SetP1High(); +void ReadPins(); +int GetYaw(); \ No newline at end of file diff --git a/src/hand-controller/transmitter.cpp b/src/hand-controller/transmitter.cpp index 533bee8..e44a327 100644 --- a/src/hand-controller/transmitter.cpp +++ b/src/hand-controller/transmitter.cpp @@ -1,4 +1,8 @@ +#include "transmitter.h" +#include "buttons.h" #include "handController.h" +#include "orientation.h" +#include "pins.h" #include /* @@ -52,10 +56,10 @@ static void sendValue(char name, int value) { uBit.radio.datagram.send(buf, i); } -void transmittData() { - sendValue('P', pitch); - sendValue('A', armed); - sendValue('R', roll); - sendValue('T', throttle); - sendValue('Y', yaw); +void TransmittData() { + sendValue('P', GetPitch()); + sendValue('A', IsArmed()); + sendValue('R', GetRoll()); + sendValue('T', GetThrottle()); + sendValue('Y', GetYaw()); } diff --git a/src/hand-controller/transmitter.h b/src/hand-controller/transmitter.h new file mode 100644 index 0000000..f61c32f --- /dev/null +++ b/src/hand-controller/transmitter.h @@ -0,0 +1,3 @@ +#pragma once + +void TransmittData(); \ No newline at end of file diff --git a/src/hand-controller/display.cpp b/src/hand-controller/view.cpp similarity index 80% rename from src/hand-controller/display.cpp rename to src/hand-controller/view.cpp index 29dd76e..be83df1 100644 --- a/src/hand-controller/display.cpp +++ b/src/hand-controller/view.cpp @@ -1,6 +1,9 @@ +#include "view.h" +#include "buttons.h" #include "handController.h" +#include "orientation.h" +#include "pins.h" #include -#include // MicroBit display const uint16_t LED_DISPLAY_SIZE = 5; @@ -19,7 +22,7 @@ static void updatePulse() { } static void displayArmed(Image &ledDisplay) { - if (armed) { + if (IsArmed()) { updatePulse(); ledDisplay.setPixelValue(0, 0, armedPulse); } else { @@ -28,6 +31,7 @@ static void displayArmed(Image &ledDisplay) { } static void displayThrottle(Image &ledDisplay) { + int throttle = GetThrottle(); if (throttle == 0) { return; } else { @@ -41,17 +45,17 @@ static uint16_t angleToDisplayCoord(int angle) { } static void displayPitchRoll(Image &ledDisplay) { - uint16_t x = angleToDisplayCoord(roll); - uint16_t y = angleToDisplayCoord(pitch); + uint16_t x = angleToDisplayCoord(GetRoll()); + uint16_t y = angleToDisplayCoord(GetPitch()); ledDisplay.setPixelValue(x, y, LED_ON); } static void displayYaw(Image &ledDisplay) { - uint16_t x = static_cast(yaw / 30 + 2); + uint16_t x = static_cast(GetYaw() / 30 + 2); ledDisplay.setPixelValue(x, 0, LED_ON); } -void updateDisplay() { +void UpdateDisplay() { uBit.display.clear(); Image ledDisplay = Image(LED_DISPLAY_SIZE, LED_DISPLAY_SIZE); diff --git a/src/hand-controller/view.h b/src/hand-controller/view.h new file mode 100644 index 0000000..0f1f273 --- /dev/null +++ b/src/hand-controller/view.h @@ -0,0 +1,4 @@ +#pragma once +#include + +void UpdateDisplay(); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index cccfa46..d1335a9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -31,15 +31,15 @@ static void FlightController() { } static void HandController() { - setBtnsHandlers(); - setP1High(); + SetBtnsHandlers(); + SetP1High(); while (true) { - setOrientation(); - updateDisplay(); - checkPanic(); - transmittData(); - readPins(); + SetOrientation(); + UpdateDisplay(); + CheckPanic(); + TransmittData(); + ReadPins(); uBit.sleep(100); } } @@ -47,6 +47,6 @@ static void HandController() { int main() { uBit.init(); initRadio(); - FlightController(); + HandController(); return 0; } \ No newline at end of file