fl { IR PropForth5.5 2018/08/07 14:41:43 IR sensor:GP1UXC41QS When tranmitted IR exist, snsor-output go to Lo. NEC format Unit Time T=562usec LeaderCode 16T(Lo)+8T(Hi) RepeatCode 16T(Lo)+4T(Hi) Data 32bit CustomCode:16bit Data:16bit(lower8bit is inverted upper8bit) StopBit T(Lo) Bit0 T(Lo)+T(Hi) Bit1 T(Lo)+3T(Hi) AEHA format Unit Time T=425usec LeaderCode 8T(Lo)+4T(Hi) RepeatCode 8T(Lo)+8T(Hi) Data variable Length CustomCode:16bit Parity:4bit Data1:4bit Data2-DataN:Typ:24bit (max48bit) StopBit T(Lo) Bit0 T(Lo)+T(Hi) Bit1 T(Lo)+3T(Hi) Sony format Unit Time T=600usec LeaderCode 4T(Lo) RepeatCode None Data variable Length StopBit None Bit0 T(Hi)+T(Lo) Bit1 T(Hi)+2T(Lo) } \ =========================================================================== \ Constants \ =========================================================================== 0 wconstant IRsensor IRsensor >m constant mIRsensor d8000000 constant 100ms d800000 constant 10ms \ LeaderCode for NEC format d674400 constant NEC15T d764320 constant NEC17T d314720 constant NEC7T d404640 constant NEC9T d89920 constant NEC2T \ LeaderCode for AEHA format d238000 constant AEHA7T d306000 constant AEHA9T d102000 constant AEHA3T d170000 constant AEHA5T d68000 constant AEHA2T \ LeaderCode for Sony format d144000 constant Sony3T d240000 constant Sony5T d24000 constant SonyT- d72000 constant SonyT+ h1F8 wconstant ctra h1F9 wconstant ctrb h1FA wconstant frqa h1FB wconstant frqb h1FC wconstant phsa h1FD wconstant phsb \ =========================================================================== \ Variables \ =========================================================================== variable irVar 8 allot \ =========================================================================== \ Main \ =========================================================================== \ Wait until IRsensor goes to Hi \ ( -- ) : waitUntilHi mIRsensor mIRsensor waitpeq ; \ Wait until IRsensor goes to Lo \ ( -- ) : waitUntilLo mIRsensor mIRsensor waitpne ; \ Save pulse-count in buffer \ ( -- n1 ) n1:last address+4 for saved pulse count(phsa/phsb) : pulseTest 1 frqa COG! h54000000 IRsensor or ctra COG! 1 frqb COG! h68000000 IRsensor or ctrb COG! here W@ alignl 4- \ Keep Hi-state during 100ms cnt COG@ begin ina COG@ mIRsensor and if cnt COG@ over - 100ms > if drop 1 else 0 then else drop cnt COG@ 0 then until 0 phsa COG! waitUntilLo begin 0 phsb COG! waitUntilHi 4+ \ ( buffer_addr+4 ) phsa COG@ over L! \ Get Lo-part 0 phsa COG! \ Check ending of IR-signal begin ina COG@ mIRsensor and if phsb COG@ 10ms > if 1 1 else 0 then else 0 1 then until swap \ ( 0/1 buffer_addr ) 4+ \ ( 0/1 buffer_addr+4 ) phsb COG@ over L! \ Get Hi-part swap \ ( buffer_addr+4 0/1 ) until ; \ Display pulse time(unit:msec) \ ( n1 -- ) n1:n1:last address+4 : pulseTime decimal here W@ alignl - here W@ alignl swap 4 / 0 do dup L@ d80 / . i 2 u/mod drop if cr then 4+ loop drop ; { pulseTest pulseTime } \ Save pulse-count in buffer \ ( -- n1 ) n1:last address+4 for saved pulse count(phsa/phsb) : pulseCount \ buffer 4 - \ ( buffer_addr-4 ) here W@ alignl 4- \ Keep Hi-state during 100ms cnt COG@ begin ina COG@ mIRsensor and if cnt COG@ over - 10ms > if drop 1 else 0 then else drop cnt COG@ 0 then until 0 phsa COG! waitUntilLo begin 0 phsb COG! waitUntilHi 4+ \ ( buffer_addr+4 ) phsa COG@ over L! \ Get Lo-part 0 phsa COG! \ Check ending of IR-signal begin ina COG@ mIRsensor and if phsb COG@ 10ms > if 1 1 else 0 then else 0 1 then until swap \ ( 0/1 buffer_addr ) 4+ \ ( 0/1 buffer_addr+4 ) phsb COG@ over L! \ Get Hi-part swap \ ( buffer_addr+4 0/1 ) until ; \ Shift 1bit of 2long to right \ ( n1 n2 -- ) n1:bit counter n2:address : bitShift dup L@ dup 1 and swap \ ( n1 n2 bit31 [irVar] ) Get bit31 1 rshift \ Shift irVar to right 2 ST@ L! \ ( n1 n2 bit31 ) 2 ST@ d30 > \ ( n1 n2 bit31 1/0 ) if over 4+ L@ dup \ ( n1 n2 bit31 [irVar+4] [irVar+4]) 1 and swap \ ( n1 n2 bit31 bit63 [irVar+4] ) Get bit63 1 rshift rot if h8000_0000 or then \ ( n1 n2 bit63 [b32..b63] ) 2 ST@ 4+ L! \ ( n1 n2 bit63 ) 2 ST@ d62 > \ ( n1 n2 bit63 1/0 ) if over 8 + L@ \ ( n1 n2 bit63 [irVar+8] ) 1 rshift swap if h8000_0000 or then \ ( n1 n2 [b64..b71] ) over 8 + L! \ ( n1 n2 ) else drop \ ( n1 n2 ) then else drop \ ( n1 n2 ) then 2drop ; \ Tst code for bitShift \ ( -- ) : shifttest irVar d12 0 fill h80000000 irVar L! irVar L@ .long irVar 4+ L@ .long irVar 8 + L@ .long cr d71 0 do i irVar bitShift irVar L@ .long irVar 4+ L@ .long irVar 8 + L@ .long cr loop ; \ Display Code \ ( n1 -- ) n1:long(customerCode and Parity are packed in right edge) : dispCode ." CustomrCode:h" dup hFFFF and .word cr ." Parity:h" d16 rshift hF and . cr ; \ Display customerCode and Parity \ ( n1 -- ) n1:bit count : dispCustomer d96 swap - dup d32 < \ ( diff 1/0 ) if irVar 8 + L@ over rshift \ ( diff [shifted irVar+8] ) irVar 4+ L@ rot lshift or dispCode else dup d64 < \ ( diff 1/0 ) if d32 - irVar 4+ L@ over rshift \ ( diff [shifted irVar+4] ) over d12 > \ ( diff [shifted irVar+4] 1/0 ) if irVar L@ rot lshift or then dispCode else d64 - irVar L@ over rshift dispCode then then drop ; \ Display Data \ ( n1 -- ) n1:bit count : dispData ." Data:h" d20 - 8 / 0 do i 0= if irVar L@ d24 then i 4 = if 2drop irVar 4+ L@ d24 then 2dup rshift hFF and .byte 8 - loop 4+ rshift hF and . cr ; \ Check IR-format \ ( -- ) : irCheck pulseCount \ ( last_address ) here W@ alignl 4+ L@ here W@ alignl L@ dup \ ( last_address LeaderCode(Hi) LeaderCode(Lo) LeaderCode(Lo) ) NEC15T NEC17T between \ ( last_address LeaderCode(Hi) LeaderCode(Lo) 1/0 ) if ." NEC format" cr drop NEC7T NEC9T between \ ( last_address 1/0 ) if 0 swap \ ( 0 last_address ) here W@ alignl - d12 - 4 - here W@ alignl d12 + swap bounds do 1 rshift i L@ NEC2T > if h80000000 or then 8 +loop \ b7........b0 b7.b0 b15......b0 \ invertedData Data CustomCode dup ." Received data:" d16 rshift .word dup .word cr dup ." CustomCode:" hFFFF and .word cr ." Data:" d16 rshift dup hFF and over 8 rshift or hFF = if hFF and .byte then cr else ." RepeatCode" cr drop then else dup \ ( last_address LeaderCode(Hi) LeaderCode(Lo) LeaderCode(Lo) ) AEHA7T AEHA9T between \ ( last_address LeaderCode(Hi) LeaderCode(Lo) 1/0 ) if ." AEHA format" cr drop AEHA3T AEHA5T between \ ( last_address 1/0 ) if irVar d12 0 fill \ Clear irVar(3longs) 0 swap \ ( 0 last_address ) here W@ alignl - d12 - 4 - here W@ alignl d12 + swap bounds do dup irVar bitShift i L@ AEHA2T > if irVar L@ h80000000 or irVar L! then 1+ 8 +loop \ ( bitcount ) \ b7..b0 b7..b0 b7..b0 b7..b0 ." Received data:" decimal dup . ." bit " dup hex \ ( bitcount ) irVar L@ .long irVar 4+ L@ .long irVar 8 + L@ .long cr dup dispCustomer dispData else ." RepeatCode" cr then else Sony3T Sony5T between if ." SONY format" cr drop 0 irVar L! \ Clear irVar 0 swap \ ( 0 last_address ) here W@ alignl - 8 - here W@ alignl 8 + swap bounds do irVar L@ 1 rshift irVar L! i L@ SonyT+ > if irVar L@ h80000000 or irVar L! then 1+ 8 +loop ." Received data:" decimal dup . ." bit" space hex irVar L@ .long cr ." Address:7bit " irVar L@ d25 rshift h7F and .byte cr ." Data:" dup 7 - . ." bit h" irVar L@ 7 lshift swap 7 - d32 swap - rshift .word cr else drop ." Unknown format" cr then then then ; : irAnalyze hex 1 frqa COG! h54000000 IRsensor or ctra COG! 1 frqb COG! h68000000 IRsensor or ctrb COG! begin irCheck cr fkey? if h20 = if 1 else 0 then else drop 0 then until ;