Page 1 of 1

Code convert vb6 help

PostPosted: Sat Oct 22, 2005 1:14 am
by Hugolain
Hello,
I use this Function to convert the codes of the type: $80, $AB, $C2 ...
Code: Select all
Private Sub CcBtnConvert_Click()
    CcTxtIRCode.Text = ByteToIRCode(Val("&H" & CcTxtHexa.Text))
End Sub

Public Function ByteToIRCode(ByVal vInput As Byte) As String
Const Header As String = "R08008104"
Const Bit0 As String = "2121"
Const Bit1 As String = "808221"
    Do Until vInput = 0
        If vInput Mod 2 = 1 Then
            ByteToIRCode = ByteToIRCode & Bit1
        Else
            ByteToIRCode = ByteToIRCode & Bit0
        End If
        vInput = vInput \ 2
    Loop
    ByteToIRCode = Header & ByteToIRCode
End Function

Can one help me has to adapt my Function for the codes of the type: $300+DIR ,$3AA ,$3F0 ...please?
Cordially
Hugolain

PS:
The IR Carrier is 39.2kHz. Data is modulated using a space coded signal with 12 data bits (data clock is 1200Hz, but actual data rate varies depending on the data).
For modulating the signals yourself, the signal looks something like this:
Timing based on 1/1200 second clock (~.833ms)
Signal is normally high (idle, no IR).
Start: signal goes low for 8/1200 sec.
Data bits: for each of 12 data bits, space encoded signal depending on bit value
Sends the most significant data bit first
If the data bit is 0: signal goes high for 1/1200 sec, and low for 1/1200 sec.
If the data bit is 1: signal goes high for 4/1200 sec, and low for 1/1200 sec.
BTW: the first 4 bits are always "0011" for the Robosapien V2
When completed, signal goes high again.
No explicit stop bit. Minimal between signals is not known.

PostPosted: Mon Oct 24, 2005 6:50 pm
by Hugolain
Is that it correct?

Private Sub CcBtnConvert_Click()
CcTxtIRCode.Text = ByteToIRCode(Val("&H" & CcTxtHexa.Text))
End Sub

Public Function ByteToIRCode(ByVal vInput As Integer) As String
Const Header As String = "R08008104"
Const Bit0 As String = "2121"
Const Bit1 As String = "808221"
Do Until vInput = 0
If vInput Mod 2 = 1 Then
ByteToIRCode = ByteToIRCode & Bit1
Else
ByteToIRCode = ByteToIRCode & Bit0
End If
vInput = vInput \ 2
Loop
ByteToIRCode = Header & ByteToIRCode
End Function

Private Sub Form_Load()

End Sub

PostPosted: Tue Oct 25, 2005 8:37 pm
by Hugolain
Small error!

replace:
Code: Select all
If vInput Mod 2 = 1 Then
ByteToIRCode = ByteToIRCode & Bit1
Else
ByteToIRCode = ByteToIRCode & Bit0
End If

by:
Code: Select all
If vInput Mod 2 = 1 Then
ByteToIRCode = Bit1 & ByteToIRCode
Else
ByteToIRCode = Bit0 & ByteToIRCode
End If

PostPosted: Thu Oct 27, 2005 9:34 am
by Hugolain
appreciate your help please !!
hugolain

PostPosted: Thu Oct 27, 2005 3:35 pm
by jrhees
You have another error -- you'll need the loop to execute 12 times (once for each bit) -- whereas currently the way your code is written, the loop will only repeat 10 times (since no IR code appears to have bits 10 or 11 set to '1'.

-Jon

PostPosted: Thu Oct 27, 2005 8:16 pm
by Hugolain
Is that it correct?

Code: Select all
Private Sub CcBtnConvert_Click()
    CcTxtIRCode.Text = ByteToIRCode(Val("&H" & CcTxtHexa.Text),12)
End Sub


Code: Select all
Public Function ByteToIRCode(ByVal vInput As Integer, Optional ByVal vNbBits As Integer = 8) As String
Const Header As String = "R08008104"
Const Bit0 As String = "2121"
Const Bit1 As String = "808221"

    Do Until vNbBits <= 0
        If vInput Mod 2 = 1 Then
            ByteToIRCode = Bit1 & ByteToIRCode
        Else
            ByteToIRCode = Bit0 & ByteToIRCode
        End If
        vInput = vInput \ 2
        vNbBits = vNbBits - 1
    Loop
    ByteToIRCode = Header & ByteToIRCode
End Function

PostPosted: Fri Oct 28, 2005 12:37 am
by jrhees
I think so...?

-Jon