Skip to content

Commit

Permalink
AddArduinoView completed
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianLima committed Sep 1, 2017
1 parent 7631160 commit f132547
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 46 deletions.
5 changes: 3 additions & 2 deletions HueHue/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;

namespace HueHue
Expand All @@ -13,7 +14,7 @@ namespace HueHue
public partial class App : Application
{
public static AppSettings settings;
public static List<Device> devices;
public static ObservableCollection<Device> devices;
public static bool isRunning { get; private set; }
public static TrayIcon icon;
public static PaletteHelper helper;
Expand All @@ -23,7 +24,7 @@ private void Application_Startup(object sender, StartupEventArgs e)
settings = new AppSettings();
helper = new PaletteHelper();

devices = new List<Device> { new Arduino(settings.COMPort) };
devices = new ObservableCollection<Device> { new Arduino(settings.COMPort, "Arduino") };

Effects.Setup(App.settings.TotalLeds);
Effects.ColorOne = (LEDBulb)App.settings.ColorOne;
Expand Down
6 changes: 4 additions & 2 deletions HueHue/Helpers/Arduino.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ class Arduino : SerialStream
/// Represents an Arduino running HueHueClient connected to the user's PC
/// </summary>
/// <param name="_COM_PORT">COM Port which the arduino is connected</param>
public Arduino(string _COM_PORT)
/// <param name="_COM_PORT">Name for the device on the list</param>

public Arduino(string _COM_PORT, string _Name)
{
this.Type = "Arduino";
this.Name = "Arduino";
this.Name = _Name;
this.Icon = "/HueHue;component/Icons/Devices/Arduino.png";
COM_PORT = _COM_PORT;
}
Expand Down
9 changes: 9 additions & 0 deletions HueHue/HueHue.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<AppDesigner Include="Properties\" />
<EmbeddedResource Include="Resources\HueHueClient.ino">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
Expand Down Expand Up @@ -229,6 +232,12 @@
<ItemGroup>
<Resource Include="Icons\Devices\RazerChroma.png" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HueHueClient\HueHueClient.vcxproj">
<Project>{c5f80730-f44f-4478-bdae-6634efc2ca88}</Project>
<Name>HueHueClient</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
163 changes: 163 additions & 0 deletions HueHue/Resources/HueHueClient.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
#include "FastLED.h"

#define NUM_LEDS 90
#define LED_DATA_PIN 6
#define NUM_BYTES ((NUM_LEDS*3) + 1) // 3 colors + 1 for brightness

#define BRIGHTNESS 255
#define UPDATES_PER_SECOND 60

#define TIMEOUT 3000

#define MODE_ANIMATION 0
#define MODE_AMBILIGHT 1
#define MODE_BLACK 2
uint8_t mode = MODE_ANIMATION;

byte MESSAGE_PREAMBLE[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09 };
uint8_t PREAMBLE_LENGTH = 10;
uint8_t current_preamble_position = 0;

unsigned long last_serial_available = -1L;

int led_counter = 0;
int byte_counter = 0;

CRGB leds[NUM_LEDS];
byte buffer[NUM_BYTES];

// Filler animation attributes
CRGBPalette16 currentPalette = RainbowColors_p;
TBlendType currentBlending = LINEARBLEND;
uint8_t startIndex = 0;

void setup()
{
Serial.begin(1000000); // 115200
FastLED.clear(true);
FastLED.addLeds<WS2812B, LED_DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
}

void loop()
{
switch (mode) {
case MODE_ANIMATION:
fillLEDsFromPaletteColors();
break;

case MODE_AMBILIGHT:
processIncomingData();
break;

case MODE_BLACK:
showBlack();
break;
}
}

void processIncomingData()
{
if (waitForPreamble(TIMEOUT))
{
Serial.readBytes(buffer, NUM_BYTES);
byte brightness;
//The first byte on the array is the brightness value
if (buffer[byte_counter++] == 1)
{
brightness = (exp(sin(millis() / 2000.0*PI)) - 0.36787944)*108.0;
byte_counter++;
}
else
{
brightness = buffer[byte_counter++];
}

//Let's set the brightness only if the value changed to avoid extra delays on effects that don't include breath mode
FastLED.setBrightness(brightness);

while (byte_counter < NUM_BYTES)
{
byte blue = buffer[byte_counter++];
byte green = buffer[byte_counter++];
byte red = buffer[byte_counter++];

leds[led_counter++] = CRGB(red, green, blue);
}

FastLED.show();

// flush the serial buffer to avoid flickering
while (Serial.available()) { Serial.read(); }

byte_counter = 0;
led_counter = 0;
}
else
{
//if we get here, there must have been data before(so the user already knows, it works!)
//simply go to black!
//mode = MODE_BLACK; //If the arduino stops receiving commands, shut down the LEDs
mode = MODE_ANIMATION; //If the Arduino stops receiving commands, start the rainbow effect
}
}

bool waitForPreamble(int timeout)
{
last_serial_available = millis();
current_preamble_position = 0;
while (current_preamble_position < PREAMBLE_LENGTH)
{
if (Serial.available() > 0)
{
last_serial_available = millis();

if (Serial.read() == MESSAGE_PREAMBLE[current_preamble_position])
{
current_preamble_position++;
}
else
{
current_preamble_position = 0;
}
}

if (millis() - last_serial_available > timeout)
{
return false;
}
}
return true;
}

void fillLEDsFromPaletteColors()
{
startIndex++;

uint8_t colorIndex = startIndex;
for (int i = 0; i < NUM_LEDS; i++) {
leds[i] = ColorFromPalette(currentPalette, colorIndex, BRIGHTNESS, currentBlending);
colorIndex += 3;
}

FastLED.delay(1000 / UPDATES_PER_SECOND);

if (Serial.available() > 0)
{
mode = MODE_AMBILIGHT;
}
}

void showBlack()
{
for (int i = 0; i < NUM_LEDS; i++)
{
leds[i] = CRGB(0, 0, 0);
}
FastLED.delay(1000 / UPDATES_PER_SECOND);

if (Serial.available() > 0)
{
mode = MODE_AMBILIGHT;
}
}
26 changes: 18 additions & 8 deletions HueHue/Views/Devices/AddArduinoView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@
Background="{DynamicResource MaterialDesignPaper}"
FontFamily="Segoe UI"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded">
<Grid>
<Button Content="Add" Margin="0,0,10,10" Height="32" HorizontalAlignment="Right" Width="75" VerticalAlignment="Bottom" Click="Button_Click"/>
<Grid.RowDefinitions>
<RowDefinition Height="90"/>
<RowDefinition/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Button Content="Add" Margin="0,0,10,10" Height="32" HorizontalAlignment="Right" Width="75" VerticalAlignment="Bottom" Click="Button_Add_Click" Grid.Row="2"/>
<Image HorizontalAlignment="Left" Height="50" Margin="10,10,0,0" VerticalAlignment="Top" Width="50" Source="/HueHue;component/Icons/Devices/Arduino.png"/>
<Label Content="Setup a new Arduino" HorizontalAlignment="Left" Margin="65,20,0,0" VerticalAlignment="Top"/>
<ComboBox HorizontalAlignment="Left" Margin="80,227,0,0" VerticalAlignment="Top" Width="120" x:Name="ComboBox_ports"/>
<Label Content="COM Port:" HorizontalAlignment="Left" Margin="10,227,0,0" VerticalAlignment="Top"/>
<Label Content="Name:" HorizontalAlignment="Left" Margin="10,196,0,0" VerticalAlignment="Top"/>
<TextBox Height="90" Margin="10,96,10,0" TextWrapping="Wrap" Text="Much hax wow" VerticalAlignment="Top" x:Name="TextBox_script" BorderBrush="#FFB39DDB" BorderThickness="1"/>
<Label Content="First, upload this script to your Arduino:" HorizontalAlignment="Left" Margin="10,65,0,0" VerticalAlignment="Top"/>
<TextBox HorizontalAlignment="Left" Height="23" Margin="80,199,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<ComboBox Margin="80,27,0,0" VerticalAlignment="Top" x:Name="ComboBox_ports" Grid.Row="2" HorizontalAlignment="Left" Width="120"/>
<Label Content="COM Port:" HorizontalAlignment="Left" Margin="10,30,0,0" VerticalAlignment="Top" Grid.Row="2"/>
<Label Content="Name:" HorizontalAlignment="Left" Margin="10,4,0,0" VerticalAlignment="Top" Grid.Row="2"/>
<TextBox Margin="10,6,10,9" TextWrapping="Wrap" x:Name="TextBox_script" BorderBrush="#FFB39DDB" BorderThickness="1" Grid.Row="1"/>
<Label Content="First, upload this script to your Arduino:" HorizontalAlignment="Left" Margin="10,65,0,0" VerticalAlignment="Top" Grid.RowSpan="2"/>
<TextBox Height="23" Margin="80,4,100,0" TextWrapping="Wrap" VerticalAlignment="Top" Grid.Row="2" Text="Arduino" x:Name="TextBoxName"/>
<Button Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}"
Margin="0,0,10,90" HorizontalAlignment="Right"
ToolTip="Copy to clipboard" VerticalAlignment="Bottom" Click="Button_Clipboard_Click" Grid.Row="1" Grid.RowSpan="2">
<materialDesign:PackIcon Kind="ClipboardText" Height="24" Width="24" />
</Button>

</Grid>
</UserControl>
40 changes: 27 additions & 13 deletions HueHue/Views/Devices/AddArduinoView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
using HueHue.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Reflection;

namespace HueHue.Views.Devices
{
Expand All @@ -24,12 +14,36 @@ public partial class AddArduinoView : UserControl
public AddArduinoView()
{
InitializeComponent();

ComboBox_ports.ItemsSource = SerialStream.GetPorts();

if (ComboBox_ports.Items.Count > 0)
{
ComboBox_ports.SelectedIndex = ComboBox_ports.Items.Count - 1;
}
}

private void Button_Add_Click(object sender, RoutedEventArgs e)
{
App.devices.Add(new Arduino(ComboBox_ports.Text, TextBoxName.Text));
}

private void Button_Click(object sender, RoutedEventArgs e)
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
var assembly = Assembly.GetExecutingAssembly();

using (Stream stream = assembly.GetManifestResourceStream(@"HueHue.Resources.HueHueClient.ino"))
{
using (StreamReader reader = new StreamReader(stream))
{
TextBox_script.Text = reader.ReadToEnd();
}
}
}

private void Button_Clipboard_Click(object sender, RoutedEventArgs e)
{
Clipboard.SetText(TextBox_script.Text);
}
}
}
1 change: 1 addition & 0 deletions HueHueClient/HueHueClient.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Text Include="HueHueClient.ino">
<FileType>CppCode</FileType>
<DeploymentContent Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</DeploymentContent>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">false</ExcludedFromBuild>
</Text>
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion HueHueClient/__vm/.HueHueClient.vsarduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
#define _VSARDUINO_H_
#define __AVR_ATmega328p__
#define __AVR_ATmega328P__
#define _VMDEBUG 1
#define F_CPU 16000000L
#define ARDUINO 10803
#define ARDUINO 10801
#define ARDUINO_AVR_NANO
#define ARDUINO_ARCH_AVR
#define __cplusplus 201103L
Expand Down
20 changes: 0 additions & 20 deletions HueHueClient/__vm/Compile.vmps.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,6 @@
52 Name = "Serial sending",
53 IsBackground = true
54 };
</Source>
</BreakPoint>
<BreakPoint Index="2" Name="MainWindow.xaml.cs, line 15 character 9" Id="8acd7f57-5b96-4d51-a403-cb89b68e8318" Enabled="1" Message="" MessageTextType="2" PreProcessedMessage="" Condition="" ConditionType="1" CurrentHits="0" File="c:\users\brian\documents\github\huehue\huehue\mainwindow.xaml.cs" FileLine="15" FileColumn="9" FunctionName="HueHue.MainWindow.MainWindow()" FunctionLineOffset="2" FunctionColumnOffset="1" HitCountTarget="1" HitCountType="1" Language="C#" LocationType="3" Tag="&lt;vmbp Id='8acd7f57-5b96-4d51-a403-cb89b68e8318'/&gt;" Type="1" ToString="System.__ComObject" BreakWhenHit="1">
<Source>8 {
9 /// &lt;summary&gt;
10 /// Interaction logic for MainWindow.xaml
11 /// &lt;/summary&gt;
12 public partial class MainWindow : Window
13 {
14 public MainWindow()
--&gt;15 {
16 InitializeComponent();
17
18 GridMain.DataContext = App.settings;
19
20 //The app was auto started by windows from the user's startup folder
21 if (Environment.GetCommandLineArgs() != null)
22 {
23 if (App.settings.AutoStart &amp;&amp; Environment.GetCommandLineArgs().Length &gt; 1)
24 {
</Source>
</BreakPoint>
</BreakPoints>
Expand Down

0 comments on commit f132547

Please sign in to comment.