Skip to content

Commit

Permalink
style: fix typo
Browse files Browse the repository at this point in the history
  • Loading branch information
DriftKingTW committed Mar 8, 2023
1 parent 8b0c335 commit b5c35a8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 41 deletions.
88 changes: 54 additions & 34 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0,
BleKeyboard bleKeyboard(BLE_NAME, AUTHOR);
TinyPICO tp = TinyPICO();

PCF8574 pcf8574RotaryExpansion(ENCODER_EXPANSION_ADDR);
bool isRotaryExpansionConnected = false;
PCF8574 pcf8574RotaryExtension(ENCODER_EXTENSION_ADDR);
bool isRotaryExtensionConnected = false;

TaskHandle_t TaskGeneralStatusCheck;
TaskHandle_t TaskLED;
TaskHandle_t TaskNetwork;
TaskHandle_t TaskScreen;
TaskHandle_t TaskEncoderExpansion;
TaskHandle_t TaskEncoderExtension;
TaskHandle_t TaskI2CScanner;

// Stucture for key stroke
Expand Down Expand Up @@ -85,6 +85,8 @@ bool clearDisplay = false;
bool isSwitchingBootMode = false;
bool isCaffeinated = false;
bool isOutputLocked = false;
bool isScreenInverted = false;
bool isScreenDisabled = false;

// OLED Screen Content
String contentTop = "";
Expand Down Expand Up @@ -119,27 +121,28 @@ void setup() {

printSpacer();

pcf8574RotaryExpansion.encoder(encoderPinA, encoderPinB);
pcf8574RotaryExpansion.pinMode(encoderSW, INPUT_PULLUP);
pcf8574RotaryExpansion.pinMode(expansionBtn1, INPUT_PULLUP);
pcf8574RotaryExpansion.pinMode(expansionBtn2, INPUT_PULLUP);
pcf8574RotaryExpansion.pinMode(expansionBtn3, INPUT_PULLUP);
pcf8574RotaryExpansion.setLatency(0);
pcf8574RotaryExtension.encoder(encoderPinA, encoderPinB);
pcf8574RotaryExtension.pinMode(encoderSW, INPUT_PULLUP);
pcf8574RotaryExtension.pinMode(extensionBtn1, INPUT_PULLUP);
pcf8574RotaryExtension.pinMode(extensionBtn2, INPUT_PULLUP);
pcf8574RotaryExtension.pinMode(extensionBtn3, INPUT_PULLUP);
pcf8574RotaryExtension.setLatency(0);

if (pcf8574RotaryExpansion.begin()) {
Serial.println("Rotary Expansion Board initialized");
isRotaryExpansionConnected = true;
if (pcf8574RotaryExtension.begin()) {
Serial.println("Rotary Extension Board initialized");
isRotaryExtensionConnected = true;
} else {
Serial.println("Rotary Expansion Board not found");
isRotaryExpansionConnected = false;
Serial.println("Rotary Extension Board not found");
isRotaryExtensionConnected = false;
}

xTaskCreate(encoderTask, /* Task function. */
"Encoder Task", /* name of task. */
5000, /* Stack size of task */
NULL, /* parameter of the task */
2, /* priority of the task */
&TaskEncoderExpansion /* Task handle to keep track of created task */
xTaskCreate(
encoderTask, /* Task function. */
"Encoder Task", /* name of task. */
5000, /* Stack size of task */
NULL, /* parameter of the task */
2, /* priority of the task */
&TaskEncoderExtension /* Task handle to keep track of created task */
);

printSpacer();
Expand Down Expand Up @@ -445,13 +448,13 @@ void encoderTask(void *pvParameters) {
bool trigger = false;
unsigned long rotaryEncoderLastButtonPress = 0;
String direction = "";
byte btnArray[] = {encoderSW, expansionBtn1, expansionBtn2, expansionBtn3};
byte btnArray[] = {encoderSW, extensionBtn1, extensionBtn2, extensionBtn3};

while (true) {
if (isRotaryExpansionConnected) {
if (isRotaryExtensionConnected) {
// Scan for rotary encoder
pinAState = pcf8574RotaryExpansion.digitalRead(encoderPinA);
pinBState = pcf8574RotaryExpansion.digitalRead(encoderPinB);
pinAState = pcf8574RotaryExtension.digitalRead(encoderPinA);
pinBState = pcf8574RotaryExtension.digitalRead(encoderPinB);
if (pinAState != lastPinAState || pinBState != lastPinBState) {
if (pinAState == true && pinBState == true) {
if (lastPinAState == false && lastPinBState == true) {
Expand Down Expand Up @@ -484,20 +487,20 @@ void encoderTask(void *pvParameters) {

// Scan for button press
for (int i = 0; i < 4; i++) {
btnState = pcf8574RotaryExpansion.digitalRead(btnArray[i]);
btnState = pcf8574RotaryExtension.digitalRead(btnArray[i]);
if (btnState == LOW &&
millis() - rotaryEncoderLastButtonPress > 200) {
switch (btnArray[i]) {
case encoderSW:
bleKeyboard.write(KEY_MEDIA_PLAY_PAUSE);
break;
case expansionBtn1:
case extensionBtn1:
bleKeyboard.write(KEY_MEDIA_NEXT_TRACK);
break;
case expansionBtn2:
case extensionBtn2:
bleKeyboard.write(KEY_MEDIA_PREVIOUS_TRACK);
break;
case expansionBtn3:
case extensionBtn3:
bleKeyboard.write(KEY_MEDIA_PLAY_PAUSE);
break;
}
Expand All @@ -513,16 +516,16 @@ void encoderTask(void *pvParameters) {
void i2cScannerTask(void *pvParameters) {
while (true) {
byte error;
byte devices[1] = {ENCODER_EXPANSION_ADDR};
byte devices[1] = {ENCODER_EXTENSION_ADDR};
for (int i : devices) {
Wire.beginTransmission(devices[i]);
error = Wire.endTransmission();

if (devices[i] == ENCODER_EXPANSION_ADDR) {
if (devices[i] == ENCODER_EXTENSION_ADDR) {
if (error == 0)
isRotaryExpansionConnected = true;
isRotaryExtensionConnected = true;
else
isRotaryExpansionConnected = false;
isRotaryExtensionConnected = false;
}
}

Expand Down Expand Up @@ -559,6 +562,12 @@ void loop() {
} else if (r == 3 && c == 4) {
isOutputLocked = !isOutputLocked;
delay(300);
} else if (r == 1 && c == 6) {
isScreenDisabled = !isScreenDisabled;
delay(300);
} else if (r == 3 && c == 6) {
isScreenInverted = !isScreenInverted;
delay(300);
}
} else if (keyMap[r][c].keyInfo.startsWith("MACRO_")) {
// Macro press
Expand Down Expand Up @@ -914,12 +923,17 @@ void renderScreen() {
u8g2.drawGlyph(0, 16, 0x00);
}

while (clearDisplay) {
while (clearDisplay || isScreenDisabled) {
u8g2.clearBuffer();
u8g2.sendBuffer();
delay(100);
}

if (isScreenInverted) {
u8g2.drawBox(0, 0, 192, 64);
u8g2.setDrawColor(2);
}

u8g2.sendBuffer();
}

Expand Down Expand Up @@ -1013,7 +1027,13 @@ void checkBattery() {
void showLowBatteryWarning() {
if (!isLowBattery) {
if (bleKeyboard.isConnected()) {
tp.DotStar_SetPower(false);
if (isScreenDisabled) {
tp.DotStar_SetPower(true);
tp.DotStar_SetBrightness(1);
tp.DotStar_SetPixelColor(0, 0, 255);
} else {
tp.DotStar_SetPower(false);
}
}
return;
}
Expand Down
14 changes: 7 additions & 7 deletions src/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ using namespace std;
#define ROWS 5
#define COLS 7

// ====== Expansion Board Pin Definition ======
// ====== Extension Board Pin Definition ======

// Rotary Encoder Expansion Board
// Rotary Encoder Extension Board
#define encoderPinA P5
#define encoderPinB P4
#define encoderSW P6
#define expansionBtn1 P0
#define expansionBtn2 P1
#define expansionBtn3 P2
#define ENCODER_EXPANSION_ADDR 0x38
#define extensionBtn1 P0
#define extensionBtn2 P1
#define extensionBtn3 P2
#define ENCODER_EXTENSION_ADDR 0x38

// ====== End Expansion Board Pin Definition ======
// ====== End Extension Board Pin Definition ======

struct Key {
uint8_t keyStroke;
Expand Down

0 comments on commit b5c35a8

Please sign in to comment.