Skip to content

Commit

Permalink
Merge pull request #167 from t3knomanzer/new-output-mode
Browse files Browse the repository at this point in the history
New output mode
  • Loading branch information
t3knomanzer authored Sep 6, 2020
2 parents 82b2424 + 8b09931 commit c634895
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 24 deletions.
1 change: 1 addition & 0 deletions Desktop/Application/MaxMix/MaxMix.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
<Compile Include="Services\Audio\IAudioDevice.cs" />
<Compile Include="Services\Audio\IAudioSession.cs" />
<Compile Include="Services\Audio\IAudioSessionService.cs" />
<Compile Include="Services\Communication\Message\MessageSetDefaultEndpoint.cs" />
<Compile Include="Services\Communication\Serial\CobsSerializationService .cs" />
<Compile Include="Services\Communication\CommunicationService.cs" />
<Compile Include="Services\Communication\ICommunicationService.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ public void SetItemVolume(int id, int volume, bool isMuted)
// TODO: Raise error
}
}

public void SetDefaultEndpoint(int id)
{
if (_devices.TryGetValue(id, out var device))
{
AudioExtensions.SetDefaultEndpoint(device.Device.DeviceID, Role.Multimedia);
}

}
#endregion

#region Private Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ internal interface IAudioSessionService
void Start();
void Stop();
void SetItemVolume(int id, int volume, bool isMuted);
void SetDefaultEndpoint(int id);

event DefaultAudioDeviceChangedDelegate DefaultDeviceChanged;
event AudioDeviceCreatedDelegate DeviceCreated;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MaxMix.Services.Communication
{
internal class MessageSetDefaultEndpoint : IMessage
{
#region Constructor
public MessageSetDefaultEndpoint() { }
public MessageSetDefaultEndpoint(int id)
{
_id = id;
}
#endregion

#region Consts
#endregion

#region Fields
private int _id;
#endregion

#region Properties
public int Id { get => _id; }
#endregion

#region Private Methods
#endregion

#region Public Methods

/*
* ---------------------------------------
* CHUNK TYPE SIZE (BYTES)
* ---------------------------------------
* ID INT32 4
* ---------------------------------------
* 4
*/

public byte[] GetBytes()
{
var result = new List<byte>();
result.AddRange(BitConverter.GetBytes(Id));
return result.ToArray();
}

public bool SetBytes(byte[] bytes)
{
var idBytes = bytes.Take(4).Reverse().ToArray();
_id = BitConverter.ToInt32(idBytes, 0);
return true;
}
#endregion
}
}
10 changes: 9 additions & 1 deletion Desktop/Application/MaxMix/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public MainViewModel()
_serializationService.RegisterType<MessageAddSession>(2);
_serializationService.RegisterType<MessageRemoveSession>(3);
_serializationService.RegisterType<MessageUpdateVolumeSession>(4);
_serializationService.RegisterType<MessageSettings>(5);
_serializationService.RegisterType<MessageSetDefaultEndpoint>(5);
_serializationService.RegisterType<MessageSettings>(6);

_settingsViewModel = new SettingsViewModel();
_settingsViewModel.PropertyChanged += OnSettingsChanged;
Expand Down Expand Up @@ -180,6 +181,8 @@ private void RaiseExitRequested()
#region EventHandlers
private void OnDefaultDeviceChanged(object sender, int id)
{
var message = new MessageSetDefaultEndpoint(id);
_communicationService.Send(message);
}

private void OnDeviceCreated(object sender, int id, string displayName, int volume, bool isMuted)
Expand Down Expand Up @@ -237,6 +240,11 @@ private void OnMessageReceived(object sender, IMessage message)
var message_ = message as MessageUpdateVolumeSession;
_audioSessionService.SetItemVolume(message_.Id, message_.Volume, message_.IsMuted);
}
else if (message.GetType() == typeof(MessageSetDefaultEndpoint))
{
var message_ = message as MessageSetDefaultEndpoint;
_audioSessionService.SetDefaultEndpoint(message_.Id);
}
}

private void OnCommunicationError(object sender, string e)
Expand Down
21 changes: 19 additions & 2 deletions Embedded/MaxMix/Commands.ino
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ void SendItemVolumeCommand(Item* item, uint8_t* rawBuffer, uint8_t* packageBuffe
Serial.write(packageBuffer, encodeSize);
}

//---------------------------------------------------------
//---------------------------------------------------------
void SendSetDefaultEndpointCommand(Item* item, uint8_t* rawBuffer, uint8_t* packageBuffer)
{
rawBuffer[0] = packageRevision++;
rawBuffer[1] = MSG_COMMAND_SET_DEFAULT_ENDPOINT;

rawBuffer[2] = (uint8_t)(item->id >> 24) & 0xFF;
rawBuffer[3] = (uint8_t)(item->id >> 16) & 0xFF;
rawBuffer[4] = (uint8_t)(item->id >> 8) & 0xFF;
rawBuffer[5] = (uint8_t)item->id & 0xFF;

uint8_t encodeSize = EncodePackage(rawBuffer, 8, packageBuffer);
Serial.write(packageBuffer, encodeSize);
}


//---------------------------------------------------------
//---------------------------------------------------------
void AddItemCommand(uint8_t* packageBuffer, Item* itemsBuffer, uint8_t* itemCount)
Expand Down Expand Up @@ -75,8 +92,8 @@ void RemoveItemCommand(uint8_t* packageBuffer, Item* itemsBuffer, uint8_t* itemC
//---------------------------------------------------------
void UpdateItemVolumeCommand(uint8_t* packageBuffer, Item* itemsBuffer, uint8_t index)
{
itemsBuffer[index].volume = packageBuffer[6];
itemsBuffer[index].isMuted = packageBuffer[7];
itemsBuffer[index].volume = packageBuffer[6];
itemsBuffer[index].isMuted = packageBuffer[7];
}

//---------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion Embedded/MaxMix/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ static const uint8_t MSG_COMMAND_ACKNOWLEDGMENT = 1;
static const uint8_t MSG_COMMAND_ADD = 2;
static const uint8_t MSG_COMMAND_REMOVE = 3;
static const uint8_t MSG_COMMAND_UPDATE_VOLUME = 4;
static const uint8_t MSG_COMMAND_SETTINGS = 5;
static const uint8_t MSG_COMMAND_SET_DEFAULT_ENDPOINT = 5;
static const uint8_t MSG_COMMAND_SETTINGS = 6;

static const uint8_t MSG_PACKET_DELIMITER = 0;

Expand Down
34 changes: 26 additions & 8 deletions Embedded/MaxMix/Display.ino
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,28 @@ void DrawSelectionVolumeBar(Adafruit_SSD1306* display, uint8_t volume, bool isMu
display->drawLine(x0, y0, x0, y1, WHITE);
}

void DrawSelectionItemVolume(Adafruit_SSD1306* display, uint8_t volume)
{
uint8_t x0;

if( volume < 10)
x0 = DISPLAY_AREA_CENTER_MARGIN_SIDE + DISPLAY_AREA_CENTER_WIDTH - DISPLAY_CHAR_WIDTH_X2;
else if(volume < 100)
x0 = DISPLAY_AREA_CENTER_MARGIN_SIDE + DISPLAY_AREA_CENTER_WIDTH - DISPLAY_CHAR_WIDTH_X2 * 2 - DISPLAY_CHAR_SPACING_X2 * 2;
else if(volume == 100)
x0 = DISPLAY_AREA_CENTER_MARGIN_SIDE + DISPLAY_AREA_CENTER_WIDTH - DISPLAY_CHAR_WIDTH_X2 * 3 - DISPLAY_CHAR_SPACING_X2 * 3;

display->setTextSize(2);
display->setTextColor(WHITE);
display->setCursor(x0, 0);

display->print(volume);
}

//---------------------------------------------------------
// Draws the output mode screen
//---------------------------------------------------------
void DisplayOutputSelectScreen(Adafruit_SSD1306* display, char* name, uint8_t volume, bool isMuted, uint8_t leftArrow, uint8_t rightArrow, uint8_t modeIndex, uint8_t modeCount)
void DisplayOutputSelectScreen(Adafruit_SSD1306* display, char* name, uint8_t volume, bool isMuted, bool isDefaultEndpoint, uint8_t leftArrow, uint8_t rightArrow, uint8_t modeIndex, uint8_t modeCount)
{
display->clearDisplay();

Expand All @@ -161,21 +179,21 @@ void DisplayOutputSelectScreen(Adafruit_SSD1306* display, char* name, uint8_t vo
DrawSelectionArrows(display, leftArrow, rightArrow);
DrawSelectionVolumeBar(display, volume, isMuted);

if(isDefaultEndpoint)
DrawSelectionChannelName(display, "O");

display->display();
}

void DisplayOutputEditScreen(Adafruit_SSD1306* display, char* name, uint8_t volume, bool isMuted, uint8_t modeIndex, uint8_t modeCount)
{
display->clearDisplay();

DrawDotGroup(display, modeIndex, modeCount);
DrawItemName(display, name, 1, DISPLAY_CHAR_WIDTH_X1, DISPLAY_CHAR_HEIGHT_X1, DISPLAY_CHAR_SPACING_X1, DISPLAY_AREA_CENTER_MARGIN_SIDE, 0, GetTimerDisplayA, ResetTimerDisplayA, DISPLAY_SCROLL_SPEED_X1);
// Clear sides
display->fillRect(0, 0, DISPLAY_AREA_CENTER_MARGIN_SIDE, DISPLAY_CHAR_HEIGHT_X1, BLACK);
display->fillRect(DISPLAY_WIDTH - DISPLAY_AREA_CENTER_MARGIN_SIDE, 0, DISPLAY_AREA_CENTER_MARGIN_SIDE, DISPLAY_CHAR_HEIGHT_X1, BLACK);
DrawItemName(display, "VOL", 2, DISPLAY_CHAR_WIDTH_X2, DISPLAY_CHAR_HEIGHT_X2, DISPLAY_CHAR_SPACING_X2, DISPLAY_AREA_CENTER_MARGIN_SIDE, 0, GetTimerDisplayA, ResetTimerDisplayA, DISPLAY_SCROLL_SPEED_X2);
DrawSelectionItemVolume(display, volume);
DrawSelectionVolumeBar(display, volume, isMuted);

DrawEditVolumeBar(display, volume, isMuted);
DrawEditVolume(display, volume);
display->display();
}

Expand Down
35 changes: 23 additions & 12 deletions Embedded/MaxMix/MaxMix.ino
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ uint8_t encodeBuffer[SEND_BUFFER_SIZE];

// State
uint8_t mode = MODE_OUTPUT;
uint8_t stateOutput = STATE_OUTPUT_NAVIGATE;
uint8_t stateOutput = STATE_OUTPUT_EDIT;
uint8_t stateApplication = STATE_APPLICATION_NAVIGATE;
uint8_t stateGame = STATE_GAME_SELECT_A;
uint8_t stateDisplay = STATE_DISPLAY_AWAKE;
Expand All @@ -82,6 +82,8 @@ int8_t itemIndexApp = 0;
int8_t itemIndexGameA = 0;
int8_t itemIndexGameB = 0;

uint32_t defaultEndpointId;

// Settings
struct Settings settings;

Expand Down Expand Up @@ -221,7 +223,7 @@ void ClearSend()
void ResetState()
{
mode = MODE_OUTPUT;
stateOutput = STATE_OUTPUT_NAVIGATE;
stateOutput = STATE_OUTPUT_EDIT;
stateApplication = STATE_APPLICATION_NAVIGATE;
stateGame = STATE_GAME_SELECT_A;
stateDisplay = STATE_DISPLAY_AWAKE;
Expand Down Expand Up @@ -266,15 +268,6 @@ bool ProcessPackage()
}
else
UpdateItemCommand(decodeBuffer, devices, index);

if(settings.displayNewItem)
{
itemIndexOutput = index;
if(mode == MODE_OUTPUT)
stateOutput = STATE_OUTPUT_NAVIGATE;

return true;
}
}
else
{
Expand Down Expand Up @@ -389,6 +382,19 @@ bool ProcessPackage()
return false;
}
}
else if(command == MSG_COMMAND_SET_DEFAULT_ENDPOINT)
{
uint32_t id = GetIdFromPackage(decodeBuffer);
int8_t index = FindItem(id, devices, deviceCount);
if(index == -1)
return false;

itemIndexOutput = index;
defaultEndpointId = id;

if(mode == MODE_OUTPUT)
return true;
}
else if(command == MSG_COMMAND_SETTINGS)
{
UpdateSettingsCommand(decodeBuffer, &settings);
Expand Down Expand Up @@ -526,6 +532,9 @@ bool ProcessEncoderButton()

if(mode == MODE_OUTPUT)
{
if(stateOutput == STATE_OUTPUT_NAVIGATE)
SendSetDefaultEndpointCommand(&devices[itemIndexOutput], sendBuffer, encodeBuffer);

stateOutput = CycleState(stateOutput, STATE_OUTPUT_COUNT);
TimerDisplayReset();
}
Expand Down Expand Up @@ -652,7 +661,9 @@ void UpdateDisplay()
{
uint8_t scrollLeft = CanScrollLeft(itemIndexOutput, deviceCount, settings.continuousScroll);
uint8_t scrollRight = CanScrollRight(itemIndexOutput, deviceCount, settings.continuousScroll);
DisplayOutputSelectScreen(display, devices[itemIndexOutput].name, devices[itemIndexOutput].volume, devices[itemIndexOutput].isMuted, scrollLeft, scrollRight, mode, MODE_COUNT);
uint8_t isDefaultEndpoint = devices[itemIndexOutput].id == defaultEndpointId;

DisplayOutputSelectScreen(display, devices[itemIndexOutput].name, devices[itemIndexOutput].volume, devices[itemIndexOutput].isMuted, isDefaultEndpoint, scrollLeft, scrollRight, mode, MODE_COUNT);
}
else if(stateOutput == STATE_OUTPUT_EDIT)
DisplayOutputEditScreen(display, devices[itemIndexOutput].name, devices[itemIndexOutput].volume, devices[itemIndexOutput].isMuted, mode, MODE_COUNT);
Expand Down

0 comments on commit c634895

Please sign in to comment.