From a5d17638b75ba6fdf43fbb6389bd33406d810c20 Mon Sep 17 00:00:00 2001 From: Englebert <=> Date: Wed, 12 Apr 2023 06:39:54 +0800 Subject: [PATCH] Removed GPIO to the Raspberry Pi. Mirgrated fully using I2C --- src/main.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/main.h | 2 ++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 8c7b9a5..2a4baf5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -510,7 +510,9 @@ void parse_i2c_command(void) { Serial.println("Get flags command"); i2c_send[0] = - I2C_CAPABILITY_FLAG_RC_INPUT; + I2C_CAPABILITY_FLAG_RC_INPUT | + I2C_CAPABILITY_FLAG_ROTARY | + I2C_CAPABILITY_FLAG_BUTTONS; i2c_send[1] = 0; i2c_send_length = 2; @@ -584,6 +586,86 @@ void parse_i2c_command(void) { } i2c_send_length = I2C_PROTOCOL_STRING_LENGTH; + // 0x11 - I2C_COMMAND_ID_GET_ROTARY_EVENTS + } else if(i2c_received[1] == I2C_COMMAND_ID_GET_ROTARY_EVENTS) { + /** + Get rotary encoder events (for main and secondary rotary encoders, if present); + Master asks slave if any rotary encoder events (first or secondary rotary encoder) took place. + Master sends: 1 byte: command id; + Slave responds: 1 byte: each bit represents: + bit 0: rotary encoder was pressed; + bit 1: rotary encoder was long pressed; + bit 2: rotary encoder was rotated CCW; + bit 3: rotary encoder was rotated CW; + bit 4: rotary encoder was rotated fast CCW; + bit 5: rotary encoder was rotated fast CW; + **/ + uint8_t rotary_encoder = 0; + i2c_send[0] = rotary_encoder; + i2c_send_length = 1; + + // 0x10 - I2C_COMMAND_ID_GET_BUTTONS_EVENTS + } else if(i2c_received[1] == I2C_COMMAND_ID_GET_BUTTONS_EVENTS) { + /** + Get buttons: master asks slave if any buttons where pressed; + Master sends: 1 byte: command id; + Slave responds: 4 bytes: + first 2 bytes: each bit represents 1 if a button was pressed, for a possible of 16 buttons on the device; + last 2 bytes: each bit represents 1 if a button was long pressed, for a possible of 16 buttons on the device; + bit 0 - Menu/Ok button + bit 1 - Cancel button + bit 2 - Plus button + bit 3 - Minus button + bit 4 - QA1 button + bit 5 - QA2 button + bit 6 - QA3 button + bit 7 - Action Plus button + bit 8 - Action Minus button + bit 9... - future use + **/ + + for(uint8_t i = 0; i < 8; i++) + rubybutton[i] = 0; + + // MENU + if((uint32_t)(millis() - rubybutton_last[0]) > rubybutton_delay) { + rubybutton[0] = incomingReadings.rubybutton & 0x01; + // digitalWrite(BUTTON_MENU, rubybutton[0]); + rubybutton_last[0] = millis(); + } + + // CANCEL + if((uint32_t)(millis() - rubybutton_last[1]) > rubybutton_delay) { + rubybutton[1] = (incomingReadings.rubybutton >> 1) & 0x01; + // digitalWrite(BUTTON_BACK, rubybutton[1]); + rubybutton_last[1] = millis(); + } + + // PLUS + if((uint32_t)(millis() - rubybutton_last[2]) > rubybutton_delay) { + rubybutton[2] = (incomingReadings.rubybutton >> 2) & 0x01; + // digitalWrite(BUTTON_UP, rubybutton[2]); + rubybutton_last[2] = millis(); + } + + // MINUS + if((uint32_t)(millis() - rubybutton_last[3]) > rubybutton_delay) { + rubybutton[3] = (incomingReadings.rubybutton >> 3) & 0x01; + // digitalWrite(BUTTON_DOWN, rubybutton[3]); + rubybutton_last[3] = millis(); + } + + i2c_send[0] = + rubybutton[0] | // Menu/Ok button + rubybutton[1] << 1 | // Cancel button + rubybutton[2] << 2 | // Plus button + rubybutton[3] << 3 ; // Minus button + i2c_send[1] = 0; + i2c_send[2] = 0; + i2c_send[3] = 0; + i2c_send_length = 4; + + // Others undefined } else { Serial.print("Others: "); @@ -701,6 +783,7 @@ void loop() { // sbus_tx.Write(); + /** if((incomingReadings.rubybutton & 0x01) != rubybutton[0]) { if((uint32_t)(millis() - rubybutton_last[0]) > 100) { rubybutton[0] = incomingReadings.rubybutton & 0x01; @@ -732,6 +815,7 @@ void loop() { rubybutton_last[2] = millis(); } } + **/ last_sent = millis(); } diff --git a/src/main.h b/src/main.h index d412dd9..b13424c 100644 --- a/src/main.h +++ b/src/main.h @@ -90,6 +90,8 @@ int16_t s_iI2CLastResponseSentPosition = -1; uint16_t s_iI2CLastResponseLength = 0; bool rubybutton[16]; uint32_t rubybutton_last[16]; +uint16_t rubybutton_delay = 100; + #define DEFAULT_CHANNEL 1 #define EEPROM_SIZE 7 // define the number of bytes you want to access