Skip to content

Commit

Permalink
cleaned stuff and added info about interrupt handling into .ino
Browse files Browse the repository at this point in the history
  • Loading branch information
0x416c6578 committed Jul 24, 2020
1 parent abd6c8c commit 0fd4c8a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
4 changes: 2 additions & 2 deletions p8-firmware/bluetooth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ BLECharacteristic RXchar = BLECharacteristic("0001", BLEWriteWithoutResponse, 20
for power draw debugging. No work has been done on the bluetooth stack yet
*/
void initBluetooth() {
blePeripheral.setLocalName("P8Watch1");
blePeripheral.setLocalName("P8Watch");
blePeripheral.setAdvertisingInterval(500);
blePeripheral.setDeviceName("P8Watch1");
blePeripheral.setDeviceName("P8Watch");
blePeripheral.setAdvertisedServiceUuid(bleService.uuid());
blePeripheral.addAttribute(bleService);
blePeripheral.addAttribute(TXchar);
Expand Down
21 changes: 16 additions & 5 deletions p8-firmware/p8-firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,27 @@ void loop() {

addToCumulativeBatReading(); //Battery readings are taken over a period and the mean is taken periodically to get a more accurate reading

/*
Interrupts will wake the device and cause the interrupt handler to run.
The device then starts running thread mode code (normal code) from where it last slept
That means that if the interrupt didn't cause getPowerMode to be awake, then the device will
go back to sleep
If the type of interrupt is one where the device wakes up (eg button press), then
the THREAD MODE interrupt handler (not the NVIC interrupt handler) will set the device wakeup state (as well as enabling hardware),
meaning that the screenControllerLoop is run and the device is awake
*/
if (getPowerMode() == POWER_ON) {
screenControllerLoop(); //This will run the main loop of the current screen
} else {
sleepWait(); //This puts the MCU into its sleep mode, only waking on an interrupt
}

/* If any interrupts were detected, this function will dispatch any events, and
reset the interrupt flag. This is not the NVIC interrupt handler, but a function called
in the main loop. This means that the interrupt handlers can just be simple flag-sets, and we
can guarantee the atomic execution of interrupt handling (ie making sure that the interrupt handler
doesn't read i2c whilst another part of the program was). */
/*
If any interrupts were detected, this function will dispatch any events, and
reset the interrupt flag. This is not the NVIC interrupt handler, but a function called
in the main loop. This means that the interrupt handlers can just be simple flag-sets, and we
can guarantee the atomic execution of actual interrupt handling (ie making sure that the interrupt handler
doesn't read i2c whilst another part of the program was).
*/
handleInterrupts();
}
14 changes: 9 additions & 5 deletions p8-firmware/screenController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ WatchScreenBase* currentScreen = homeScreens[currentHomeScreenIndex];

/*
This is called whenever a new screen is loaded
It will setup the screen and draw the indicator and update the screen refresh time
*/
void initScreen() {
currentScreen->screenSetup(); //Call screenSetup() on the current screen
Expand Down Expand Up @@ -61,6 +62,7 @@ void handleRightSwipe() {
*/
void prevScreen() {
if (currentHomeScreenIndex != 0) {
currentScreen->screenDestroy(); //Call 'destructor' for current screen
currentHomeScreenIndex--;
currentScreen = homeScreens[currentHomeScreenIndex];
initScreen();
Expand All @@ -72,6 +74,7 @@ void prevScreen() {
*/
void nextScreen() {
if (currentHomeScreenIndex != NUM_SCREENS - 1) {
currentScreen->screenDestroy(); //Call 'destructor' for current screen
currentHomeScreenIndex++;
currentScreen = homeScreens[currentHomeScreenIndex];
initScreen();
Expand Down Expand Up @@ -109,9 +112,9 @@ void handleButtonPress() {
The parameters are the x and y coords of the tap
*/
void handleTap(uint8_t x, uint8_t y) {
if (y < 212) {
if (y < 212) { //If the tap is on the main application
currentScreen->screenTap(x, y);
} else {
} else { //Else we are pressing in the app drawer buttons, so go to the prev or next screen
if (x < 100) {
prevScreen();
} else if (x > 160) {
Expand All @@ -121,9 +124,10 @@ void handleTap(uint8_t x, uint8_t y) {
}

/*
Sleep when we receive a long tap */
Sleep when we receive a long tap
*/
void handleLongTap(uint8_t x, uint8_t y) {
if (currentScreen->doesImplementLongTap() == true) {
if (currentScreen->doesImplementLongTap() == true) { //Make sure the current screen doesn't implement the long tap
currentScreen->screenLongTap(x, y);
} else {
enterSleep();
Expand All @@ -137,7 +141,7 @@ void handleLongTap(uint8_t x, uint8_t y) {
*/
void screenControllerLoop() {
if (millis() - lastScreenUpdate > screenUpdateMS) {
//Update the screen every 20 ms
//The refresh time is variable depending on the current screen
currentScreen->screenLoop();
lastScreenUpdate = millis();
}
Expand Down

0 comments on commit 0fd4c8a

Please sign in to comment.