Skip to content

Commit

Permalink
v1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
SpReeD committed Jul 21, 2019
1 parent 763c961 commit 16285f8
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 21 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,43 @@ Adalight Wrapper
================
Simple wrapper written in Visual Basic .NET (.NET Framework 4.5.2)

Simple Usage:
__VB__
~~~~
Dim AdalightDevices As List(Of String) = adalight_wrapper.Adalight.GetAdalightDevices
Dim LEDCount As Integer = 50
Using Ada = New adalight_wrapper.Adalight(AdalightDevices(0), LEDCount)
Ada.OpenConn()
For i = 0 To LEDCount - 1
Ada.LEDMatrix(i) = {255, 255, 255}
Next
Ada.Send()
End Using
~~~~

__C#__
~~~~
List<string> AdalightDevices = adalight_wrapper.Adalight.GetAdalightDevices();
int LEDCount = 50;
using (var Ada = new adalight_wrapper.Adalight(AdalightDevices[0], 50))
{
Ada.OpenConn();
for (int i = 0; i < LEDCount; i++)
{
Ada.LEDMatrix[i] = new[] { 255, 255, 255 };
}
Ada.Send();
}
~~~~

#LEDMatrix
This is an array containing the RBG (note: not RGB) information for each LED.
Send RED to LED ##21 would like like this (VB example):
~~~~
Ada.LEDMatrix(21) = {255,0,0}
~~~~

### Enjoying this?
Just star the repo or make a donation.

Expand Down
89 changes: 70 additions & 19 deletions adalight-wrapper/Adalight.vb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,27 @@ Public Class Adalight : Implements IDisposable
Private serialData As Byte()
Private Const magicWord As String = "Ada"

Public Sub New(ByVal COMPort As String, ByVal LEDCount As Integer)
Private _COMPort As String
Public Property COMPort As String
Set(value As String)
_COMPort = value
End Set
Get
Return _COMPort
End Get
End Property

''' <summary>
''' Initialze Instance
''' </summary>
''' <param name="Port">COM Port as String, e.g. "COM3"</param>
''' <param name="LEDCount">Total Count of LEDs on your Stripe</param>
Public Sub New(ByVal Port As String, ByVal LEDCount As Integer)
'Set Properties
LEDS = LEDCount
_COMPort = Port

'Create connection object
COMConn = New SerialPort With {
.PortName = COMPort,
.BaudRate = 115200,
Expand All @@ -18,16 +38,20 @@ Public Class Adalight : Implements IDisposable
.StopBits = StopBits.One
}

LEDS = LEDCount

'Create Matrix Array
LEDMatrix = New ArrayList
For i = 0 To LEDCount - 1
LEDMatrix.Add({0, 0, 0})
Next

ReDim serialData(6 + (LEDS * 3)) 'Change ByteArray length on runtime
'Redefine ByteArray length on runtime of current LED count
ReDim serialData(6 + (LEDS * 3))
End Sub

''' <summary>
''' Tries to open connection
''' </summary>
''' <returns>Boolean value</returns>
Public Function OpenConn() As String
Try
COMConn.Open()
Expand All @@ -37,6 +61,10 @@ Public Class Adalight : Implements IDisposable
End Try
End Function

''' <summary>
''' Tries to close connection
''' </summary>
''' <returns>Boolean value</returns>
Public Function CloseConn() As Boolean
Try
COMConn.Close()
Expand All @@ -46,12 +74,13 @@ Public Class Adalight : Implements IDisposable
End Try
End Function

#Region "Helper routines"
Private Sub WriteHeader()
serialData(0) = Convert.ToByte(magicWord(0))
serialData(0) = Convert.ToByte(magicWord(0)) 'MagicWord
serialData(1) = Convert.ToByte(magicWord(1))
serialData(2) = Convert.ToByte(magicWord(2))
serialData(3) = CByte((LEDS - 1) >> 8)
serialData(4) = CByte(((LEDS - 1) And &HFF))
serialData(3) = CByte((LEDS - 1) >> 8) 'LED count high byte
serialData(4) = CByte(((LEDS - 1) And &HFF)) 'LED count low byte
serialData(5) = CByte(serialData(3) Xor serialData(4) Xor &H55) 'Checksum
End Sub

Expand All @@ -67,6 +96,40 @@ Public Class Adalight : Implements IDisposable
Next
End Sub

''' <summary>
''' Helper Function
''' Returns a list of devices responded with the correct Adalight magicword
''' </summary>
''' <returns></returns>
Public Shared Function GetAdalightDevices() As List(Of String)
Dim output As List(Of String) = New List(Of String)

For Each dev In SerialPort.GetPortNames()
Try
Dim i As SerialPort = New SerialPort With {
.PortName = dev,
.BaudRate = 115200,
.Parity = Parity.None,
.DataBits = 8,
.StopBits = StopBits.One,
.ReadTimeout = 1500
}
i.Open()
If i.ReadLine() = "Ada" Then
output.Add(dev)
End If
i.Close()
Catch ex As Exception
End Try
Next
Return output
End Function
#End Region

''' <summary>
''' Tries to send data to Adalight device
''' </summary>
''' <returns>Boolean value</returns>
Public Function Send() As Boolean
Try
WriteHeader()
Expand Down Expand Up @@ -113,16 +176,4 @@ Public Class Adalight : Implements IDisposable
GC.SuppressFinalize(Me)
End Sub
#End Region

Public Function Debug() As String
Dim output As String = Nothing
For Each dev In System.IO.Ports.SerialPort.GetPortNames()
Dim i As SerialPort = New SerialPort
i.PortName = dev
i.Open()
output = i.ReadLine
i.Close()
Next
Return output
End Function
End Class
8 changes: 6 additions & 2 deletions adalight-wrapper/adalight-wrapper.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
<DefineTrace>true</DefineTrace>
<OutputPath>bin\Debug\</OutputPath>
<DocumentationFile>adalight-wrapper.xml</DocumentationFile>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
<NoWarn>41999,42016,42017,42018,42019,42020,42021,42022,42032,42036</NoWarn>
<WarningsAsErrors>
</WarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -30,7 +32,9 @@
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DocumentationFile>adalight-wrapper.xml</DocumentationFile>
<NoWarn>42016,41999,42017,42018,42019,42032,42036,42020,42021,42022</NoWarn>
<NoWarn>41999,42016,42017,42018,42019,42020,42021,42022,42032,42036</NoWarn>
<WarningsAsErrors>
</WarningsAsErrors>
</PropertyGroup>
<PropertyGroup>
<OptionExplicit>On</OptionExplicit>
Expand Down

0 comments on commit 16285f8

Please sign in to comment.