Skip to content

Commit

Permalink
Interrupts changed, but touch interrupt doesn't do correct thing like…
Browse files Browse the repository at this point in the history
… 10 percent of the time
  • Loading branch information
0x416c6578 committed Jul 1, 2020
1 parent 0236bf5 commit 5da0f94
Show file tree
Hide file tree
Showing 20 changed files with 116 additions and 196 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 6 additions & 7 deletions p8-firmware/headers/Screens.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include "Arduino.h"
#include "WatchScreenBase.h"
#include "accelerometer.h"
#include "display.h"
#include "p8Time.h"
#include "pinoutP8.h"
Expand All @@ -25,14 +24,14 @@ class DemoScreen : public WatchScreenBase {
}
void screenDestroy() {}
void screenLoop() {
bma4_accel* data = getAcclData();
writeIntWithoutPrecedingZeroes(0, 0, 2, (int)data->x);
writeIntWithoutPrecedingZeroes(0, 20, 2, (int)data->y);
writeIntWithoutPrecedingZeroes(0, 40, 2, (int)data->z);
updateTouchStruct();
writeIntWithPrecedingZeroes(0, 0, 2, getTouchDataStruct()->gesture);
writeIntWithPrecedingZeroes(0, 20, 2, getTouchDataStruct()->x);
writeIntWithPrecedingZeroes(0, 40, 2, getTouchDataStruct()->y);
}
void screenTap(uint8_t x, uint8_t y) {}
bool doesImplementSwipeRight() { return false; }
bool doesImplementSwipeLeft() { return false; }
void swipeRight() {}
void swipeLeft() {}
void swipeUp() {}
void swipeDown() {}
};
Expand Down
10 changes: 0 additions & 10 deletions p8-firmware/headers/i2c.h

This file was deleted.

11 changes: 11 additions & 0 deletions p8-firmware/headers/i2cLock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include "pinoutP8.h"
#include "Arduino.h"

#define I2C_LOCKED true
#define I2C_UNLOCKED false

void initI2C();
bool getI2CState();
void lockI2C();
void unlockI2C();
2 changes: 1 addition & 1 deletion p8-firmware/headers/interrupts.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
void initInterrupts();
void touchEvent();
void handleInterrupts();
void resetInterruptFlag();
void resetInterrupts();
void buttonEvent();
void handleTouchInt();
2 changes: 1 addition & 1 deletion p8-firmware/headers/touch.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "Arduino.h"
#include "pinoutP8.h"
#include "i2c.h"
#include "i2cLock.h"
#include "ioControl.h"

#define NO_GESTURE 0x00
Expand Down
71 changes: 0 additions & 71 deletions p8-firmware/i2c.cpp

This file was deleted.

23 changes: 23 additions & 0 deletions p8-firmware/i2cLock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "headers/i2cLock.h"

#include <Wire.h>

//We have a mutex lock on the i2c object so we dont have any overlaps of accessing i2c
bool i2cLock = false;

void initI2C() {
Wire.begin();
Wire.setClock(250000);
}

bool getI2CState(){
return i2cLock;
}

void lockI2C(){
i2cLock = I2C_LOCKED;
}

void unlockI2C(){
i2cLock = I2C_UNLOCKED;
}
125 changes: 41 additions & 84 deletions p8-firmware/interrupts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,8 @@

uint16_t interruptsFlag = 0b0000000000000000;
long lastWakeTime = 0;
volatile bool lastPushButtonState;
volatile bool lastTouchState;

/*
We say to the function handleInterrupts that there is a touch event to be handled
*/
void touchEvent() {
updateTouchStruct();
interruptsFlag |= TOUCH_INT;
}

/*
We have a similar case for the button event as for a touch event
*/
void buttonEvent() {
interruptsFlag |= BUTTON_INT;
}
bool pendingButtonInt = false;
bool pendingTouchInt = false;

/*
Winterrupts.h uses GPIOTE Events for interrupt handling, which has the drawback of drawing more power
Expand All @@ -38,12 +23,8 @@ void initInterrupts() {
NVIC_EnableIRQ(GPIOTE_IRQn);
NRF_GPIOTE->EVENTS_PORT = 1;
NRF_GPIOTE->INTENSET = GPIOTE_INTENSET_PORT_Msk;

lastPushButtonState = digitalRead(PUSH_BUTTON_IN);
NRF_GPIO->PIN_CNF[PUSH_BUTTON_IN] |= ((uint32_t)(lastPushButtonState ? GPIO_PIN_CNF_SENSE_Low : GPIO_PIN_CNF_SENSE_High) << GPIO_PIN_CNF_SENSE_Pos);

lastTouchState = digitalRead(TP_INT);
NRF_GPIO->PIN_CNF[TP_INT] |= ((uint32_t)(lastTouchState ? GPIO_PIN_CNF_SENSE_Low : GPIO_PIN_CNF_SENSE_High) << GPIO_PIN_CNF_SENSE_Pos);
NRF_GPIO->PIN_CNF[PUSH_BUTTON_IN] |= ((uint32_t)(GPIO_PIN_CNF_SENSE_High) << GPIO_PIN_CNF_SENSE_Pos);
NRF_GPIO->PIN_CNF[TP_INT] |= ((uint32_t)(GPIO_PIN_CNF_SENSE_Low) << GPIO_PIN_CNF_SENSE_Pos);
}

#ifdef __cplusplus
Expand All @@ -52,25 +33,13 @@ extern "C" {
void GPIOTE_IRQHandler() {
if ((NRF_GPIOTE->EVENTS_PORT != 0)) {
NRF_GPIOTE->EVENTS_PORT = 0;

bool buttonRead = digitalRead(PUSH_BUTTON_IN);
if (buttonRead != lastPushButtonState) {
lastPushButtonState = buttonRead;
NRF_GPIO->PIN_CNF[PUSH_BUTTON_IN] &= ~GPIO_PIN_CNF_SENSE_Msk;
NRF_GPIO->PIN_CNF[PUSH_BUTTON_IN] |= ((lastPushButtonState ? GPIO_PIN_CNF_SENSE_Low : GPIO_PIN_CNF_SENSE_High) << GPIO_PIN_CNF_SENSE_Pos);
if (lastPushButtonState == true)
buttonEvent();
}
bool touchRead = digitalRead(TP_INT);
if (touchRead != lastTouchState) {
lastTouchState = touchRead;
NRF_GPIO->PIN_CNF[TP_INT] &= ~GPIO_PIN_CNF_SENSE_Msk;
NRF_GPIO->PIN_CNF[TP_INT] |= ((lastTouchState ? GPIO_PIN_CNF_SENSE_Low : GPIO_PIN_CNF_SENSE_High) << GPIO_PIN_CNF_SENSE_Pos);
if (lastTouchState == false)
touchEvent();
if (!digitalRead(TP_INT)) {
updateTouchStruct();
pendingTouchInt = true;
} else if (digitalRead(PUSH_BUTTON_IN)) {
pendingButtonInt = true;
}
}
(void)NRF_GPIOTE->EVENTS_PORT;
}
#ifdef __cplusplus
}
Expand All @@ -83,63 +52,51 @@ Originally, having many function calls in the interrupt handling code (doing all
the interrupt) could randomly cause a crash, this way doesn't
*/
void handleInterrupts() {
if (interruptsFlag != 0) {
if (getPowerMode() == POWER_OFF) {
exitSleep();
resetInterruptFlag();
lastWakeTime = millis();
return;
}
if (getPowerMode() == POWER_OFF) {
exitSleep();
resetInterrupts();
lastWakeTime = millis();
return;
}

switch (interruptsFlag) {
case TOUCH_INT:
handleTouchInt();
resetInterruptFlag();
if (pendingTouchInt) {
TouchDataStruct* touchData = getTouchDataStruct();
switch (touchData->gesture) {
case SINGLE_TAP:
handleTap(touchData->x, touchData->y);
break;
case LONG_PRESS:
break;
case SWIPE_DOWN:
handleDownSwipe();
break;
case SWIPE_UP:
handleUpSwipe();
break;
case SWIPE_LEFT:
handleLeftSwipe();
break;
case BUTTON_INT:
handleButtonPress(); //Return to the time screen
resetInterruptFlag();
case SWIPE_RIGHT:
handleRightSwipe();
break;
}
} else {
} else if (pendingButtonInt) {
handleButtonPress();
}

else {
if (millis() - lastWakeTime > SLEEP_AFTER_N_SECONDS * 1000) {
enterSleep();
}
}
}

/*
Originally this was called by the actuall interrupt handling function, but it caused problems
when accessing the i2c bus when the thread code was also accessing it, randomly causing a crash
So it was moved to a function that is called whilst in thread mode to stop the concurrent accessing
of the i2c bus causing a crash
*/
void handleTouchInt() {
switch (getTouchDataStruct()->gesture) {
case SINGLE_TAP:
handleTap(getTouchDataStruct()->x, getTouchDataStruct()->y);
break;
case LONG_PRESS:
break;
case SWIPE_DOWN:
handleDownSwipe();
break;
case SWIPE_UP:
handleUpSwipe();
break;
case SWIPE_LEFT:
handleLeftSwipe();
break;
case SWIPE_RIGHT:
handleRightSwipe();
break;
}
resetInterrupts();
}

/*
Remove any flagged interrupts
*/
void resetInterruptFlag() {
interruptsFlag = 0;
void resetInterrupts() {
pendingTouchInt = false;
pendingButtonInt = false;
}
6 changes: 3 additions & 3 deletions p8-firmware/p8-firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include "headers/powerControl.h"
#include "headers/touch.h"
#include "headers/watchdog.h"
#include "headers/i2c.h"
#include "headers/accelerometer.h"
#include "headers/i2cLock.h"
//#include "headers/accelerometer.h"
#include "nrf52.h"

void setup() {
Expand All @@ -22,7 +22,7 @@ void setup() {
initFastSPI(); //Initialize EasyDMA SPI
initDisplay(); //Initialize display
initI2C();
initAccel();
//initAccel();
initTouch(); //Initialize touch panel

initInterrupts(); //Setup interrupts
Expand Down
11 changes: 3 additions & 8 deletions p8-firmware/screenController.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "headers/screenController.h"

#define NUM_SCREENS 4
#define NUM_SCREENS 3
/*
Similar to ATCWatch, an instance of every screen will be instantiated at bootup
There will be a pointer to the current screen which will have the methods called on it
Expand All @@ -13,7 +13,7 @@ TimeDateSetScreen timeDateSetScreen;
DemoScreen demoScreen;

int currentHomeScreenIndex = 0;
WatchScreenBase* homeScreens[NUM_SCREENS] = {&timeScreen, &stopWatchScreen, &timeDateSetScreen, &demoScreen};
WatchScreenBase* homeScreens[NUM_SCREENS] = {&timeScreen, &stopWatchScreen, &timeDateSetScreen};
WatchScreenBase* currentScreen = homeScreens[currentHomeScreenIndex];

/*
Expand Down Expand Up @@ -133,7 +133,6 @@ void drawAppIndicator() {
writeChar(startOfString + (0 * indicatorFontSize * FONT_WIDTH) + (0 * indicatorFontSize), 216, indicatorFontSize, (currentHomeScreenIndex == 0) ? GLYPH_CLOCK_SEL : GLYPH_CLOCK_UNSEL, COLOUR_WHITE, COLOUR_BLACK);
writeChar(startOfString + (1 * indicatorFontSize * FONT_WIDTH) + (1 * indicatorFontSize), 216, indicatorFontSize, (currentHomeScreenIndex == 1) ? GLYPH_STOPWATCH_SEL : GLYPH_STOPWATCH_UNSEL, COLOUR_WHITE, COLOUR_BLACK);
writeChar(startOfString + (2 * indicatorFontSize * FONT_WIDTH) + (2 * indicatorFontSize), 216, indicatorFontSize, (currentHomeScreenIndex == 2) ? GLYPH_SETTINGS_SEL : GLYPH_SETTINGS_UNSEL, COLOUR_WHITE, COLOUR_BLACK);
writeChar(startOfString + (3 * indicatorFontSize * FONT_WIDTH) + (3 * indicatorFontSize), 216, indicatorFontSize, (currentHomeScreenIndex == 3) ? GLYPH_TOUCH_SEL : GLYPH_TOUCH_UNSEL, COLOUR_WHITE, COLOUR_BLACK);
//Draw the "can scroll left/right" indicators in the corners of the screen
switch (currentHomeScreenIndex) {
case 0:
Expand All @@ -145,12 +144,8 @@ void drawAppIndicator() {
writeChar(225, 216, indicatorFontSize, GLYPH_ARROW_RIGHT, COLOUR_WHITE, COLOUR_BLACK);
break;
case 2:
writeChar(0, 216, indicatorFontSize, GLYPH_ARROW_LEFT, COLOUR_WHITE, COLOUR_BLACK);
writeChar(225, 216, indicatorFontSize, GLYPH_ARROW_RIGHT, COLOUR_WHITE, COLOUR_BLACK);
break;
case 3:
writeChar(0, 216, indicatorFontSize, GLYPH_ARROW_LEFT, COLOUR_WHITE, COLOUR_BLACK);
writeChar(225, 216, indicatorFontSize, GLYPH_ARROW_RIGHT, 0b1000010000010000, COLOUR_BLACK);
break;
break;
}
}
Loading

0 comments on commit 5da0f94

Please sign in to comment.