RGB Lighting Interface

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • Geps
    Automated Home Guru
    • Nov 2010
    • 136

    RGB Lighting Interface

    Hi,

    I'm renovating my house at the moment and was very keen on the RGB LED lights. With all my other lightning to be controlled by the quad dimmer modules I couldn't find a way to interface these lights to my (future!) Idranet.

    Talking to toscal on here he said he had a couple of RS232 based RGB lightning controllers so I just needed a way to link Cortex to the serial port.

    Talking to Karam and Viv at Idratek they suggested a method of using a virtual quad dimmer - virtual because it only existed in Cortex (turn off the network enabled option). You can then route commands to the device with Cortex and use the Cortex API to pick up these commands and send data to the serial port.

    So that's what I did!

    Attached below is some very rough VB.net code. My background is embedded C and whilst I had a go at C# I just didn't want to take the time to fully teach myself. At this stage it's purely designed as a proof of concept and I'll be adding features onto it as and when I get time but it works so thought I'd post it here for others to use should they wish.

    I took Karam's idea and extended it....instead of using the individual dimmer values, at the moment I'm using a single dimmer channel equated to a colour. The idea being that if you want the whole house orange you set a single value rather than each individual R G B one making it slightly easier to use.

    In the future I'll be
    • Tidying up this code
    • Adding RS485 interface so multiple units can share the same bus
    • Creating a rough UI so I can change settings without needing to keep hitting compile


    Screenshot:


    VB.net code (compiled with VS2010 Professional SP1):
    Code:
    Imports System.Runtime.InteropServices
    
    Public Class TestCortexAPIForm
        Declare Auto Function SendMessage Lib "user32" ( _
            ByVal hWnd As IntPtr, _
            ByVal Msg As Integer, _
            ByVal wParam As Integer, _
            ByVal lParam As Integer) As Integer
        Declare Auto Function RegisterWindowMessage Lib "User32.Dll" _
           (ByVal lpString As String) As Integer
        Structure CopyDataStruct
            Public ID As String
            Public Length As Int32
            Public Data As String
        End Structure
        Const CortexControlAPIDiscover = "CortexAPIDiscover"
        Const CortexControlAPIAttach = "CortexAPIAttach"
        Const WM_COPYDATA = &H4A
        Dim UM_CortexControlAPIDiscover As UInt32
        Dim UM_CortexControlAPIAttach As UInt32
        Dim HWND_BROADCAST As Long = -1
        Dim MyCortexHandle As IntPtr = IntPtr.Zero
        Dim CortexAPIActive As Boolean
        Dim ReceivedMessage As String
        Dim aCDS As CopyDataStruct
        Dim BoardAddress As Byte
        Dim TransmitBuffer() As Byte = {&HFE, &H4C, &H42, &H2, &HFF}
    
        Private Sub TestCortexAPIForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            UM_CortexControlAPIDiscover = RegisterWindowMessage(CortexControlAPIDiscover)
            UM_CortexControlAPIAttach = RegisterWindowMessage(CortexControlAPIAttach)
            Device_Init()
    
        End Sub
    
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = UM_CortexControlAPIAttach Then
                If (m.LParam = 1) And (m.WParam <> Handle) Then
                    Log("< WM_CortexAPIAttach")
                    MyCortexHandle = m.WParam
                    CortexAPIActive = True
                End If
                m.Result = New IntPtr(1) 'Ensure returned response is correct
            Else
                If m.Msg = UM_CortexControlAPIDiscover Then
                    If m.LParam = 1 Then
                        If (m.WParam <> Me.Handle) Then
                            MyCortexHandle = m.WParam
                            CortexAPIActive = True
                            Log("< WM_CortexAPIDiscover")
                            Log("> WM_CortexAPIAttach")
                            SendMessage(HWND_BROADCAST, UM_CortexControlAPIAttach, Handle, IntPtr.Zero)
                        End If
                    End If
                    m.Result = New IntPtr(1) 'Ensure returned response is correct
                Else
                    If m.Msg = WM_COPYDATA Then
                        If m.WParam = MyCortexHandle Then
                            aCDS = m.GetLParam(GetType(CopyDataStruct))
                            ReceivedMessage = aCDS.Data
                            Log("< " & ReceivedMessage)
                            If ReceivedMessage = "Disconnect" Then
                                CortexAPIActive = False
                            Else
                                If ReceivedMessage = "Close" Then
                                    Me.Close()
                                End If
                            End If
                            m.Result = New IntPtr(1) 'Ensure returned response is correct
                        End If
                    Else
                        MyBase.WndProc(m)
                    End If
                End If
            End If
        End Sub
    
        Private Sub TestCortexAPIForm_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
            Log("> WM_CortexAPIDiscover")
            SendMessage(HWND_BROADCAST, UM_CortexControlAPIDiscover, Handle, IntPtr.Zero)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Command(TextBox1.Text)
        End Sub
    
        Private Sub Command(ByVal theCommand As String) 'USE THIS TO SEND COMMANDS TO CORTEX
            If theCommand <> "" And CortexAPIActive Then
                Log("> " & theCommand)
                aCDS.ID = "1"
                aCDS.Data = theCommand
                aCDS.Length = aCDS.Data.Length + 1
                Dim lParam As IntPtr
                lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(aCDS))
                Marshal.StructureToPtr(aCDS, lParam, False)
                SendMessage(MyCortexHandle, WM_COPYDATA, Me.Handle, lParam)
                Marshal.FreeCoTaskMem(lParam) ' Free allocated memory
            End If
        End Sub
    
        Private Sub Log(ByVal AString As String)
            TextBox2.AppendText(vbCrLf & DateTime.Now.ToLongTimeString & " " & AString)
            LastReceivedResponse.Text = AString
        End Sub
    
        Private Sub TestCortexAPIForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
            Command("Disconnect")
        End Sub
    
        ' Modify to suit your setup
        Private Sub Device_Init()
        SerialPort1.BaudRate = 9600
        SerialPort1.PortName = "COM5"
        SerialPort1.DataBits = 8
        SerialPort1.Parity = IO.Ports.Parity.None
        BoardAddress = &H4C
    
        'Set data
        TransmitBuffer(1) = BoardAddress
        End Sub
    
    Private Sub set_rgb_value(ByVal RedValue, ByVal GreenValue, ByRef BlueValue)
        If (SerialPort1.IsOpen = False) Then
            SerialPort1.Open()
        End If
    
        Debug.Print("Setting RGB Values:" & RedValue & " " & GreenValue & " " & BlueValue)
    
    
        TransmitBuffer(3) = 1
        TransmitBuffer(4) = RedValue
        SerialPort1.Write(TransmitBuffer, 0, TransmitBuffer.Length)
        'System.Threading.Thread.Sleep(20)
    
        TransmitBuffer(3) = 2
        TransmitBuffer(4) = GreenValue
        SerialPort1.Write(TransmitBuffer, 0, TransmitBuffer.Length)
        'System.Threading.Thread.Sleep(20)
    
        TransmitBuffer(3) = 3
        TransmitBuffer(4) = BlueValue
        SerialPort1.Write(TransmitBuffer, 0, TransmitBuffer.Length)
    
        'System.Threading.Thread.Sleep(20)
    
        'SerialPort1.Close()
    End Sub
    
    
    
    
    Private Sub process_command(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LastReceivedResponse.TextChanged
        Dim CommandToProcess(2) As String
    
            If (LastReceivedResponse.Text.Contains("Dimmer")) Then
                Debug.Print("LED RGB")
            End If
            If (LastReceivedResponse.Text.Contains("Lounge Dimmer 1.Set Level (Output)")) Then
                ' Exact colour
            CommandToProcess = LastReceivedResponse.Text.Split("=")
                Select Case Convert.ToDecimal(CommandToProcess(1))
                    Case 100 ' colour
                                set_rgb_value(10, 255, 255)
    
                    Case 10 ' white
                                set_rgb_value(255, 255, 255)
                End Select
            End If
    End Sub
    
    
    
    Private Sub TrackBar2_Scroll(sender As Object, e As System.EventArgs) Handles rgbBlue.Scroll
        set_rgb_value(rgbRed.Value, rgbGreen.Value, rgbBlue.Value)
    End Sub
    
    Private Sub rgbRed_Scroll(sender As System.Object, e As System.EventArgs) Handles rgbRed.Scroll
        set_rgb_value(rgbRed.Value, rgbGreen.Value, rgbBlue.Value)
    End Sub
    
    Private Sub rgbGreen_Scroll(sender As System.Object, e As System.EventArgs) Handles rgbGreen.Scroll
        set_rgb_value(rgbRed.Value, rgbGreen.Value, rgbBlue.Value)
    End Sub
    End Class
    3 trackbars are used to provide a quick method of updating and testing colour combinations.


    Cheers,
    Last edited by Geps; 16 November 2011, 10:46 PM.
  • toscal
    Moderator
    • Oct 2005
    • 2061

    #2
    Does this mean I need to get some more RGB RS232 interfaces in then .
    IF YOU CAN'T FIX IT WITH A HAMMER, YOU'VE GOT AN ELECTRICAL PROBLEM.
    Renovation Spain Blog

    Comment

    • Geps
      Automated Home Guru
      • Nov 2010
      • 136

      #3
      I'm good for the foreseeable future - not looking to get everything up and running until summer next year as there is still loads to do!

      Others may though

      Comment

      Working...
      X