'PICROMITE R emote C ontrol
option explicit 
'option lcd panel
'option touch
'option wavetime 1.3
option default NONE 
dim integer repeater
DIM KeyCode AS INTEGER
COLOUR RGB(WHITE), RGB(BLACK)
' reference numbers for the controls are defined as constants
CONST numEntry = 1
  
' setup our flags that are set in interupts
DIM INTEGER printmessage1=0,printmessage2=0
CLS
GUI INTERRUPT TouchDown, TouchUp
GUI NUMBERBOX numEntry, 30,40,90,40,RGB(white),RGB(100,100,100)

' Coding for 0 and 1 s
' 1 = short,long,short,short          |-|___|-|_     ie 4 inversions by bitbanger
' 0= short,short,short,long           |-|_|-|___     ie 4 inversions by bitbanger
' The header and footers are added to the front and end of the code respectively.
 
 dim integer Aon%=   &B01000001000011110000000000010001   'A on 
 dim integer Aoff%=  &B01000001000011110000000000000001   'A off
 dim integer Don%=   &B01000001000011110000000000010100   'D on 
 dim integer Doff%=  &B01000001000011110000000000000100   'D off
 dim integer LHon%=  &B01000001000011110000000000011010   'LH on 
 dim integer LHoff%= &B01000001000011110000000000001010   'LH off
 dim integer EBDon%= &B01000001000011110000000000010111  'Dimmer on 
 dim integer EBDoff%=&B01000001000011110000000000000111   'Dimmer off
 dim integer NVSW%=  &B010000011011110000100001   '12Volt LED Stripe on/off
dim integer code%
dim float lengths(132)   ' 128 transitions for the 32 bits, and 2 each for the header and foooter 128+2+2=132 -1
dim float lengths24(98)   ' 96 transitions for the 24 bits, and 2 each for the header and foooter 
dim integer xmitpin=40   ' the pin connected to the 433mhz module data pin
SetPin xmitpin, DOUT          ' set it as an output
PIN(xmitpin)=0                ' set the data pin low


' set up the code and pin details
dim integer numberofbitsincode=32            'number of bits in the code.
dim integer numberofbitsincode24=26            'number of bits in the code.
dim integer pulsecount=numberofbitsincode*4  'number of inversions in the code ie. 1 down + 1 up per bit in the code
dim integer pulsecount24=numberofbitsincode*2  'number of inversions in the code ie. 1 down + 1 up per bit in the code

dim float pulselength=360                  'the number of microseconds for the basic short pulse length A long is 4 times this ie 1200
dim float pulselength24=400                   'the number of microseconds for the basic short pulse length A long is 4 times this ie 1200
dim integer repeat=12                       'number of times to repeat the transmission 

do

if KeyCode = 11 then
   'Pause 500:12VoltLED_ON:
    repeat=12
    code%=NVSW%
    SEND433_24B(pulsecount24,pulselength24,repeat,xmitpin,code%)
    KeyCode = 102
  end if
  if KeyCode = 1 then
   'Pause 500:Valve1_ON:
   repeat=14
   code%=Aon%
   SEND433_32B(pulsecount,pulselength,repeat,xmitpin,code%)
   KeyCode = 101
  end if
  if KeyCode = 2 then
   'Pause 500:Valve1_OFF:
    repeat=12
    code%=Aoff%
    SEND433_32B(pulsecount,pulselength,repeat,xmitpin,code%)
    KeyCode = 102
  end if
  if KeyCode = 3 then
   ':RFC Socked:
repeat=12
    code%=Don%
    SEND433_32B(pulsecount,pulselength,repeat,xmitpin,code%)
    KeyCode = 103
  end if
  if KeyCode = 4 then
  ':RFC Socked:
repeat=12
   code%=Doff%
   SEND433_32B(pulsecount,pulselength,repeat,xmitpin,code%)
   KeyCode = 104
  end if
if KeyCode = 5 then
   ':LH Switch:
     repeat=7
    code%=LHon%
    SEND433_32B(pulsecount,pulselength,repeat,xmitpin,code%)
    KeyCode = 105
  end if
  if KeyCode = 6 then
  ':LH Switch_OFF:
   repeat=7
   code%=LHoff%
   SEND433_32B(pulsecount,pulselength,repeat,xmitpin,code%)
   KeyCode = 106
  end if
if KeyCode = 7 then
   ':switch_Dimmer_ON:
    repeat=5
    code%=EBDon%
    SEND433_32B(pulsecount,pulselength,repeat,xmitpin,code%)
    KeyCode = 107
  end if
  if KeyCode = 8 then
  ':Switch_Dimmer_OFF:
   repeat=5
   code%=EBDoff%
   SEND433_32B(pulsecount,pulselength,repeat,xmitpin,code%)
   KeyCode = 108
  end if
loop


SUB SEND433_32B(pulsecount as integer,pulselength as float,repeat as integer ,thexmitpin as integer,senddata% as integer)
  print "send32B"
      local integer savedata%=senddata%
      local integer long,short,mini,maxi,prelong
      local integer j,i,repeatpause
      'long=pulselength*3-.15 'fudge for Mx170 at 48 MHz
      short=pulselength      
      mini=pulselength-50
      maxi=pulselength*43
      long=pulselength*4
      prelong=short*9
      repeatpause=long*10/1400   'is millseconds so divide by 1000
     for j=0 to repeat-1
        
        ' send the header bits
         lengths(0)=short
         lengths(1)=prelong
        senddata%=savedata%
        for i=2 to pulsecount+1 step 4
          if senddata% and &H80000000 then
            'One
             lengths(i)=short
             lengths(i+1)=long
             lengths(i+2)=short
             lengths(i+3)=mini
          
          ELSE
             'Zero
             lengths(i)=short
             lengths(i+1)=mini
             lengths(i+2)=short
             lengths(i+3)=long
          end if
          senddata%=senddata%<<1
        next i
        ' send the footer bits
        lengths(pulsecount+2)=short
        lengths(pulsecount+3)=maxi
         rem for j=0 to repeat-1
         BITSTREAM thexmitpin, 131, lengths()
        pause repeatpause
          next j

end SUB

SUB SEND433_24B(pulsecount as integer,pulselength as float,repeat as integer ,thexmitpin as integer,senddata% as integer)
  print "send24B":PIN(xmitpin)=0
      local integer savedata%=senddata%
      local integer long,short,prelong
      local integer repeatpause,j,i
      'long=pulselength*3-.15 'fudge for Mx170 at 48 MHz
      short=pulselength      
      prelong=pulselength*30
      long=pulselength*3
      repeatpause=long*10/1000   'is millseconds so divide by 1000
 ' send the header bits
       lengths(0)=short 'DEBUG
         lengths(1)=prelong 'DEBUG

        senddata%=savedata%
        for i=2 to pulsecount-1 step 2
          if senddata% and &H800000 then ' minus 2 fr 24 bytes &H80000000
            'One
             lengths(i)=long
             lengths(i+1)=short
          
          ELSE
             'Zero
             lengths(i)=short
             lengths(i+1)=long
          END if
          senddata%=senddata%<<1
        next i
        ' send the stop bits
'        lengths(pulsecount+1)=short 'DEBUG
'        lengths(pulsecount+2)=prelong 'DEBUG
         for j=0 to repeat-1
         BITSTREAM thexmitpin, 98, lengths()
         pause repeatpause    
       ' pause between repetitions 
      next j

END SUB

  ' the interrupt routine for touch down
  ' using a select case command it has a different process for each
  ' control
SUB TouchDown
  SELECT CASE TOUCH(REF) ' find out the control touched
    'Do not do too much here! No other interupts will be serviced until
    'this sub is ended. So just set a flag and get out ASAP
    CASE numEntry
       printmessage1=1   
      
  end SELECT
end SUB
  ' interrupt routine when the touch is removed
SUB TouchUp
  SELECT CASE TOUCH(LASTREF) ' use the last reference
    'Do not do too much here! No other interupts will be serviced until
    'this sub is ended. So just set a flag and get out ASAP
    CASE numEntry ' was it the numEntry box
      KeyCode = CTRLVAL(numEntry)
PRINT "Key press = " KeyCode
      printmessage2=1
  end SELECT
end SUB