Commit bd4a3e4b authored by Micha's avatar Micha

initial commit

parent 822ead3b
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace MarmeLED
{
public class MarmeLED
{
private string host = "";
private int port = 0;
private Socket socket;
private IPEndPoint endpoint;
private int numberGlasses = 0;
private int LedPerGlass = 0;
public MarmeLED(string hostName, int portNumber, int numberOfGlasses, int numberOfLedsPerGlass)
{
host = hostName;
port = portNumber;
numberGlasses = numberOfGlasses;
LedPerGlass = numberOfLedsPerGlass;
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress serverAddr = IPAddress.Parse(host);
endpoint = new IPEndPoint(serverAddr, port);
}
private byte[] AddFront(byte[] arr, byte value)
{
byte[] temp = new byte[arr.Length + 1];
temp[0] = value;
Array.Copy(arr, 0, temp, 1, arr.Length);
return temp;
}
private byte[] concat(byte[] arr1, byte[] arr2)
{
byte[] temp = new byte[arr1.Length + arr2.Length];
Array.Copy(arr1, 0, temp, 0, arr1.Length);
Array.Copy(arr2, 0, temp, arr1.Length, arr2.Length);
return temp;
}
private void udpSend(byte[] data) {
socket.SendTo(data, endpoint);
}
public void flush() {
byte[] flushBytes = {255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255};
udpSend(flushBytes);
Thread.Sleep(15);
}
private void setLED(byte adr, byte red, byte green, byte blue)
{
byte[] temp = { adr, red, green, blue };
udpSend(temp);
Thread.Sleep(1);
}
public void setLamp(int index, byte[] led1, byte[] led2, byte[] led3, byte[] led4, byte[] led5, byte[] led6)
{
int baseVal = index * LedPerGlass;
led1 = AddFront(led1, (byte)(baseVal));
led2 = AddFront(led2, (byte)(baseVal+1));
led3 = AddFront(led3, (byte)(baseVal+2));
led4 = AddFront(led4, (byte)(baseVal+3));
led5 = AddFront(led5, (byte)(baseVal+4));
led6 = AddFront(led6, (byte)(baseVal+5));
byte[] temp = concat(led1, concat(led2, concat(led3, concat(led4, concat(led5, led6)))));
var lalelu = (led1.Concat<byte>(led2)).ToArray();
udpSend(temp);
Thread.Sleep(1);
}
public void switchAllOff() {
byte[] temp = {0x0, 0x0, 0x0};
for(int i = 0; i < numberGlasses; i++) {
setLamp(i, temp, temp, temp, temp, temp, temp);
}
flush();
}
public void switchAllOn(byte red, byte green, byte blue)
{
byte[] temp = { red, green, blue };
for (int i = 0; i < numberGlasses; i++)
{
setLamp(i, temp, temp, temp, temp, temp, temp);
}
flush();
}
public void switchLampOff(int index)
{
int baseIndex = index * LedPerGlass;
for (int i = 0; i < LedPerGlass; i++)
{
setLED((byte)(baseIndex + i), 0x0, 0x0, 0x0);
}
flush();
}
private void setSingleLED(int lampIndex, int ledIndex, byte red, byte green, byte blue)
{
for (int i = 0; i < LedPerGlass; i++)
{
if (i == ledIndex)
setLED((byte)(lampIndex * LedPerGlass + i), red, green, blue);
else
setLED((byte)(lampIndex * LedPerGlass + i), 0x0, 0x0, 0x0);
}
}
public void rotateRight(int lampIndex, byte red, byte green, byte blue)
{
for (int i = 0; i < LedPerGlass; i++)
{
int temp1 = (i - 1 < 0) ? (LedPerGlass - 1) : (i - 1);
int temp2 = (i + 1 > LedPerGlass-1) ? (0) : (i + 1);
setSingleLED(lampIndex, temp1, (byte)(red / 2), (byte)(green / 2), (byte)(blue / 2));
setSingleLED(lampIndex, temp2, (byte)(red / 2), (byte)(green / 2), (byte)(blue / 2));
setSingleLED(lampIndex, i, red, green, blue);
flush();
Thread.Sleep(20);
}
}
public void rotateLeft(int lampIndex, byte red, byte green, byte blue)
{
for (int i = 0; i < LedPerGlass; i++)
{
setSingleLED(lampIndex, LedPerGlass - 1 - i, red, green, blue);
flush();
}
}
// def disco(lampIndex, red, green, blue)
// baseIndex = lampIndex*6
// for i in 0..5
// setLED([baseIndex+i, red, green, blue])
// flush()
// end
// for i in 0..5
// setLED([baseIndex+i, 0, 0, 0])
// flush()
// end
// end
// def discoAllRandom()
// for i in 0..5
// for j in 0..12
// setLED([j*6 + i, rand(255), rand(255), rand(255)])
// end
// flush()
// sleep(1.0/500)
// end
// for i in 0..5
// for j in 0..12
// setLED([j*6 + i, 0, 0, 0])
// end
// flush()
// sleep(1.0/500)
// end
// end
// def rotateRight(red, green, blue)
// for i in 0..5
// for j in 0..12
// setSingleLED(j, i, red, green, blue)
// end
// flush()
// end
// end
// def rotateAllLeft(red, green, blue)
// for i in 0..5
// for j in 0..12
// setSingleLED(j, 5-i, red, green, blue)
// end
// flush()
// end
// end
}
public class led
{
public byte adress { get; set; }
byte _red;
public byte red { get { return _red; } set { _red = value; update = true; } }
byte _green;
public byte green { get { return _green; } set { _green = value; update = true; } }
byte _blue;
public byte blue { get { return _blue; } set { _blue = value; update = true; } }
public bool update { get; set; }
public led(byte adr, byte r, byte g, byte b)
{
adress = adr;
red = r;
green = g;
blue = b;
}
public led()
{
adress = 0;
red = 0;
green = 0;
blue = 0;
}
}
public class glass
{
public byte index { get; set; }
public List<led> leds = new List<led>();
public glass(byte ind, int numberOfLeds)
{
index = ind;
for (int i = 0; i < numberOfLeds; i++)
{
led temp = new led();
temp.adress = (byte)((int)index * numberOfLeds + i);
leds.Add(temp);
}
}
}
public class MarmeLED_v2
{
private string host = "";
private int port = 0;
private Socket socket;
private IPEndPoint endpoint;
private int numberGlasses = 0;
private int LedPerGlass = 0;
private List<glass> glasses = new List<glass>();
public MarmeLED_v2(string hostName, int portNumber, int numberOfGlasses, int numberOfLedsPerGlass)
{
host = hostName;
port = portNumber;
numberGlasses = numberOfGlasses;
LedPerGlass = numberOfLedsPerGlass;
init();
}
private void init()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress serverAddr = IPAddress.Parse(host);
endpoint = new IPEndPoint(serverAddr, port);
for (int i = 0; i < numberGlasses; i++)
{
glass temp = new glass((byte)i, LedPerGlass);
glasses.Add(temp);
}
update();
}
public void setAnimation(animation _anim, int numberIterations, int speedPerCent)
{
// set sender
_anim.initMarmeLED(this, numberGlasses, LedPerGlass);
int speed = 0;
if (speedPerCent < 1)
speed = 1;
else if (speedPerCent > 100)
speed = 100;
else
speed = speedPerCent;
const int maxDelay = 1000;
int delay = maxDelay - ((maxDelay / 100) * speed);
_anim.Start();
for (int i = 0; i < numberIterations; i++)
{
_anim.Run();
Thread.Sleep(delay);
}
_anim.Stop();
}
private void udpSend(byte[] data)
{
socket.SendTo(data, endpoint);
}
private void flush()
{
byte[] flushBytes = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };
udpSend(flushBytes);
Thread.Sleep(15);
}
public void update()
{
List<led> sendList = new List<led>();
foreach(glass element in glasses)
{
for (int i = 0; i < LedPerGlass; i++)
{
if (element.leds[i].update)
{
sendList.Add(element.leds[i]);
element.leds[i].update = false;
}
}
}
packAndSend(sendList);
}
private void packAndSend(List<led> data)
{
byte[] sendBuffer = new byte[data.Count * 4];
for(int i = 0; i < data.Count; i++) {
sendBuffer[i * 4] = data[i].adress;
sendBuffer[i * 4 + 1] = data[i].red;
sendBuffer[i * 4 + 2] = data[i].green;
sendBuffer[i * 4 + 3] = data[i].blue;
}
// only send 24 bytes at once (for testing purposes only)
int bytesSent = 0;
int maxBytes = 24;
while (bytesSent < sendBuffer.Length)
{
int bytesToSend = Math.Min(maxBytes, sendBuffer.Length - bytesSent);
byte[] tempArray = new byte[bytesToSend];
Array.Copy(sendBuffer, bytesSent, tempArray, 0, bytesToSend);
bytesSent += bytesToSend;
udpSend(tempArray);
flush();
}
}
public void setLED(int glassIndex, int ledIndex, byte red, byte green, byte blue)
{
glasses[glassIndex].leds[ledIndex].red = red;
glasses[glassIndex].leds[ledIndex].green = green;
glasses[glassIndex].leds[ledIndex].blue = blue;
}
public void setGlass(int glassIndex, byte red, byte green, byte blue)
{
for (int i = 0; i < glasses[glassIndex].leds.Count; i++ )
{
glasses[glassIndex].leds[i].red = red;
glasses[glassIndex].leds[i].green = green;
glasses[glassIndex].leds[i].blue = blue;
}
}
public void switchAllOff()
{
byte[] temp = { 0x0, 0x0, 0x0 };
for (int i = 0; i < numberGlasses; i++)
{
setGlass(i, 0, 0, 0);
}
update();
}
//public void rotateRight(int lampIndex, byte red, byte green, byte blue)
//{
// int Neighbour1 = 0;
// int Neighbour2 = 0;
// int Neighbour3 = 0;
// int Neighbour4 = 0;
// int Neighbour5 = 0;
// for (int k = 0; k < LedPerGlass; k++)
// {
// int i = k % LedPerGlass;
// Neighbour1 = (i - 1 >= 0) ? (i - 1) : LedPerGlass - 1;
// Neighbour2 = (i - 2 >= 0) ? (i - 2) : LedPerGlass - 2;
// Neighbour3 = (i - 3 >= 0) ? (i - 3) : LedPerGlass - 3;
// setLED(lampIndex, Neighbour1, (byte)((int)(red / 2)), (byte)((int)(green / 2)), (byte)((int)(blue / 2)));
// setLED(lampIndex, Neighbour2, (byte)((int)(red / 3)), (byte)((int)(green / 3)), (byte)((int)(blue / 3)));
// setLED(lampIndex, Neighbour3, (byte)((int)(red / 4)), (byte)((int)(green / 4)), (byte)((int)(blue / 4)));
// setLED(lampIndex, Neighbour4, (byte)((int)(red / 5)), (byte)((int)(green / 5)), (byte)((int)(blue / 5)));
// setLED(lampIndex, Neighbour5, 0, 0, 0);
// //setLED(lampIndex, Neighbour6, (byte)((int)(red / 7)), (byte)((int)(green / 7)), (byte)((int)(blue / 7)));
// setLED(lampIndex, i, red, green, blue);
// update();
// Thread.Sleep(60);
// //switchAllOff();
// }
//}
//public void test()
//{
// int lastGlass = 1000, lastLed = 1000;
// for (int i = 0; i < numberGlasses; i++)
// {
// for (int j = 0; j < LedPerGlass; j++)
// {
// setLED(i, j, 255, 255, 255);
// if (lastLed < 1000)
// {
// setLED(lastGlass, lastLed, 0, 0, 0);
// }
// lastGlass = i;
// lastLed = j;
// update();
// Thread.Sleep(50);
// }
// }
//}
}
}
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{04C6ABC2-C041-46DC-9CDE-3FE34E21BFCF}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MarmeLED</RootNamespace>
<AssemblyName>MarmeLED</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="animation.cs" />
<Compile Include="MarmeLED.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</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.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
\ No newline at end of file
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MarmeLED")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Fraunhofer Gesellschaft")]
[assembly: AssemblyProduct("MarmeLED")]
[assembly: AssemblyCopyright("Copyright © Fraunhofer Gesellschaft 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7bfede52-8176-4561-93ad-c38a40823ca8")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
<
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MarmeLED
{
public abstract class animation
{
//public int numberIterations { get; set; }
public MarmeLED_v2 marmeLED { get; set; }
public abstract void Start();
public abstract void Run();
public abstract void Stop();
public int glassesAmount { get; set; }
public int ledsAmount { get; set; }
public void initMarmeLED(MarmeLED_v2 marmeled, int numberOfGlasses, int ledsPerGlass)
{
marmeLED = marmeled;
glassesAmount = numberOfGlasses;
ledsAmount = ledsPerGlass;
}
}
public class waveLight : animation
{
private List<int> values_red;
private List<int> values_green;
private List<int> values_blue;
private byte base_red, base_green, base_blue;
public waveLight(byte red, byte green, byte blue)
//: base(marmeLed)
{
base_red = red;
base_blue = blue;
base_green = green;
values_red = new List<int>();
values_blue = new List<int>();
values_green = new List<int>();
}
public override void Start()
{
for (int i = 0; i < glassesAmount; i++)
{
//values.Add((int)(255 * Math.Sin(Math.PI / glassesAmount * i)));
// scale given color
values_red.Add((int)(base_red * Math.Sin(Math.PI / glassesAmount * i)));
values_green.Add((int)(base_green * Math.Sin(Math.PI / glassesAmount * i)));
values_blue.Add((int)(base_blue * Math.Sin(Math.PI / glassesAmount * i)));
marmeLED.switchAllOff();
}
}
public override void Run()
{
for (int i = 0; i < glassesAmount; i++)
{
marmeLED.setGlass(i, (byte)values_red[i], (byte)values_green[i], (byte)values_blue[i]);
}
marmeLED.update();
rollOne(ref values_red);
rollOne(ref values_green);
rollOne(ref values_blue);
}
public override void Stop()