Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple models of Apple remote + long press #97

Merged
merged 1 commit into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 103 additions & 28 deletions SOURCES/WEBAPP/ESP32/aurora/aurora.ino
Original file line number Diff line number Diff line change
Expand Up @@ -599,20 +599,82 @@ void prev_preset(void) {
update_preset();
}

void increase_volume(void) {
masterVolume.val += 0.5f;
#define VOLUME_STEP 0.5f

void increase_volume_step(float factor) {
masterVolume.val += VOLUME_STEP * factor;
if( masterVolume.val > 0.f )
masterVolume.val = 0.f;
setMasterVolume();
}

void decrease_volume(void) {
masterVolume.val -= 0.5f;
void decrease_volume_step(float factor) {
masterVolume.val -= VOLUME_STEP * factor;
if( masterVolume.val <= -80.f )
masterVolume.val = -80.f;
setMasterVolume();
}

void increase_volume(void) {
increase_volume_step(1.0f);
}

void decrease_volume(void) {
decrease_volume_step(1.0f);
}

#if HAVE_IRRECEIVER

#define DELAY_REMOTE_MS 150 // minimal time between IR remote actions

#define REMOTE_BUTTON_NONE 0
#define REMOTE_BUTTON_PLUS 1
#define REMOTE_BUTTON_PREVIOUS 2
#define REMOTE_BUTTON_PLAY 3
#define REMOTE_BUTTON_NEXT 4
#define REMOTE_BUTTON_MINUS 5
#define REMOTE_BUTTON_MENU 6
#define REMOTE_BUTTON_CENTER 7
#define REMOTE_BUTTON_RELEASED 8
#define REMOTE_BUTTON_REPEAT 9


uint8_t ir_action(uint32_t value) {
uint8_t action = REMOTE_BUTTON_NONE;
if (value == 0xFFFFFFFF) {
action = REMOTE_BUTTON_REPEAT;
}
else if ((value & 0xFFFF0000) == 0x77E10000) {
// Apple remotes
// Following code is working for multiple Apple remote models
// Inspired by https://github.com/brackendawson/Appleceiver/blob/357a0a16013d9e35f1119530399aba55b7030bab/Appleceiver.ino#L43-L55
switch ((value & 0x00007F00) >> 8) {
case 0x50: action = REMOTE_BUTTON_PLUS; break;
case 0x10: action = REMOTE_BUTTON_PREVIOUS; break;
case 0x7a: action = REMOTE_BUTTON_PLAY; break;
case 0x60: action = REMOTE_BUTTON_NEXT; break;
case 0x30: action = REMOTE_BUTTON_MINUS; break;
case 0x40: action = REMOTE_BUTTON_MENU; break;
case 0x3a: action = REMOTE_BUTTON_CENTER; break;
case 0x20: action = REMOTE_BUTTON_RELEASED; /* emitted when PLAY or CENTER buttons are released */; break;
}
}
return action;
}

uint8_t repeatable_action = REMOTE_BUTTON_NONE;
unsigned long last_action_time = millis();
unsigned long last_repeat_time = millis();
unsigned long repeat_duration_ms = 0;

float speed_factor(unsigned long repeat_duration_ms) {
/* Return volume speed factor depending on long press duration */
if (repeat_duration_ms < 1000) return 1.0f;
if (repeat_duration_ms < 3000) return 2.0f;
return 4.0f;
}
#endif

//==============================================================================
/*! Arduino Main Loop
*
Expand Down Expand Up @@ -716,31 +778,44 @@ void loop()
decode_results irResults;
if( irReceiver.decode( &irResults ) )
{
if( irResults.value == APPLE_REMOTE_UP )
{
increase_volume();
needUpdateUI = true;
}
else if( irResults.value == APPLE_REMOTE_DOWN )
{
decrease_volume();
needUpdateUI = true;
}
else if( irResults.value == APPLE_REMOTE_LEFT )
{
myDisplay.drawSwitchingPreset();

prev_preset();

needUpdateUI = true;
}
else if( irResults.value == APPLE_REMOTE_RIGHT )
{
myDisplay.drawSwitchingPreset();

next_preset();
unsigned long now = millis();
if (now - last_action_time > DELAY_REMOTE_MS) {
uint8_t action = ir_action(irResults.value);

if (action == REMOTE_BUTTON_REPEAT && repeatable_action != REMOTE_BUTTON_NONE) {
/* repeat last action */
action = repeatable_action;
last_repeat_time = now;
repeat_duration_ms = now - last_action_time;
} else {
/* no repeat */
last_action_time = now;
repeat_duration_ms = 0;
}

needUpdateUI = true;
repeatable_action = REMOTE_BUTTON_NONE;
switch (action) {
case REMOTE_BUTTON_PLUS:
increase_volume_step(speed_factor(repeat_duration_ms));
needUpdateUI = true;
repeatable_action = action; // this action can be repeated
break;
case REMOTE_BUTTON_MINUS:
decrease_volume_step(speed_factor(repeat_duration_ms));
needUpdateUI = true;
repeatable_action = action; // this action can be repeated
break;
case REMOTE_BUTTON_PREVIOUS:
myDisplay.drawSwitchingPreset();
prev_preset();
needUpdateUI = true;
break;
case REMOTE_BUTTON_NEXT:
myDisplay.drawSwitchingPreset();
next_preset();
needUpdateUI = true;
break;
}
}
//else
// Serial.println(irResults.value, HEX);
Expand Down
12 changes: 1 addition & 11 deletions SOURCES/WEBAPP/ESP32/aurora/hwconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,5 @@
#define HAVE_IRRECEIVER (1)
#define IR_RECEIVER_PIN 5

#define APPLE_REMOTE_UP 0x77E1D047
#define APPLE_REMOTE_DOWN 0x77E1B047
#define APPLE_REMOTE_LEFT 0x77E11047
#define APPLE_REMOTE_RIGHT 0x77E1E047
#define APPLE_REMOTE_CENTER_DOWN 0x77E1BA47
#define APPLE_REMOTE_CENTER_UP 0x77E12047
#define APPLE_REMOTE_MENU 0x77E14047
#define APPLE_REMOTE_PLAY_DOWN 0x77E17A47
#define APPLE_REMOTE_PLAY_UP 0x77E12047


#endif
#endif