![]() |
Forum Index : Microcontroller and PC projects : Reading the Apple Tv Remote with Mmbasic…
Author | Message | ||||
Plasmamac![]() Guru ![]() Joined: 31/01/2019 Location: GermanyPosts: 580 |
If i remember me correct anybody in the Forum has released Code to read an AppleTV remote. unfortunately i cant find anything about. who can help? thanks Plasma |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2642 |
It depends on what protocol AppleTV uses. MMBasic has the IR command which decodes NEC or Sony infrared remote control protocols. If it uses Philips RC-5, RC-6 or Samsung32 or JVC-16 there are MMBasic Subs to decode them. They are in these threads. RC-5 and RC-6 NEC, Samsung and JVC If none of the above you will need to log the data transmitted by your remote control as a set of timestamps for rising and falling edges from an IR receiver. CSub LOG can do that but I think it was last compiled for MMBasic V5.08, however IR data is slow enough to capture with MMBasic. I had a program to do just that but can't find it. It went something like this untested prog. 'Untested IR time logging program. Values greater than 50mS indicate no more data.Dim integer m, N=100, IRpin = mm.info(pinno GP0) 'IR input pin, N = number of transitions to record Dim float b(N), T Setpin IRpin, Din Do :Loop Until Not Pin(IRpin) 'Wait for first falling edge then start recording T=timer Do While m<(N-2) 'get pulse/pause times from an IR receiver module Do :Loop Until Pin(IRpin) Or (Timer-T > 50) b(m)=Timer-T 'record time at rising edge T=Timer Inc m Do While Pin(IRpin) And (Timer-T < 51):Loop b(m) = T - Timer 'record negative time at falling edge T=Timer Inc m Loop Math V_Print b() End Repeat for each button on the remote. The part of the data that doesn't change will be the Device code and data that is different for each button will be the Key code. Edit Tested, output from a NEC remote. > RUN 8.9940, -4.4440, 0.5920, -1.5980, 0.5930, -1.6370, 0.5510, -0.5410, 0.5540, -0.5430, 0.5550, -0.5430, 0.5560, -0.5430, 0.5560, -0.5400, 0.5500, -0.5500, 0.5520, -0.5090, 0.5910, -0.5160, 0.5530, -1.6350, 0.5940, -1.6380, 0.5560, -1.6360, 0.5510, -1.6330, 0.5940, -1.5970, 0.5930, -1.6350, 0.5550, -0.5440, 0.5560, -1.6380, 0.5510, -1.6380, 0.5510, -0.5500, 0.5570, -0.5450, 0.5550, -0.5390, 0.5590, -1.6290, 0.5900, -0.5040, 0.5930, -1.6400, 0.5510, -0.5440, 0.5540, -0.5440, 0.5550, -1.6320, 0.5540, -1.6390, 0.5530, -1.6380, 0.5520, -0.5450, 0.5550, -1.6340, 0.5550, -39.6920, 8.9630, -2.2230, 0.5520, -51.0490, 0.0930, -44.4900, 8.9650, -2.1800, 0.5930, -51.0620, 0.0920, -51.0650, 0.0930, -51.0430, 0.0930, -51.0610, 0.0880, -51.0450, 0.0920, -51.0650, 0.0930, -51.0760, 0.0930, -51.0690, 0.0890, -51.0540, 0.0880, -51.0610, 0.0930, -51.0730, 0.0000, 0.0000, 0.0000 > For NEC it starts with a 9mS burst of carrier to allow the receiver to set its AGC level followed by a 4.5mS pause then the start bit followed by Run-Length-Encoded data. 560uS pulses and data encoded in the pauses between pulses. Nominally 560uS = 0 and 1680uS = 1. A reasonable amount of variation is allowed as some IR chips use a rudimentary clock. The -39.6920 pause indicates the end of data and the following bits indicate the button was held down longer than 1 cycle. Most other protocols don't use this feature. The first value over 50 is a time-out at the end of the data. An example of RC-5 Manchester code. Both pulses and spaces vary as each bit is composed of High/Low or Low/High half bits. Adjacent half bits join to make short or long pulses and pauses. No initial carrier burst to set the receiver AGC so the receiver AGC needs to be fast attack slow decay. > RUN 0.9440, -0.8680, 1.7730, -0.8620, 0.8740, -0.8720, 0.8360, -0.9080, 0.8740, -0.8710, 0.8750, -1.7300, 1.7720, -0.8670, 0.8770, -1.7780, 0.8730, -0.8730, 1.7680, -1.7420, 0.8740, -51.0580, 0.0940, -38.5120, 0.8360, -0.9020, 1.7360, -0.9090, 0.8370, -0.9090, 0.8360, -0.8710, 0.9130, -0.8660, 0.8740, -1.7370, 1.7730, -0.8700, 0.8770, -1.7720, 0.8370, -0.9090, 1.7340, -1.7850, 0.8740, -51.0510, 0.0930, -51.0760, 0.0930, -51.0750, 0.0930, -51.0450, 0.0890, -51.0460, 0.0940, -51.0570, 0.0890, -51.0760, 0.0890, -51.0710, 0.0930, -51.0790, 0.0930, -51.0760, 0.0970, -51.0430, 0.0930, -51.0660, 0.0940, -51.0440, 0.0950, -51.0520, 0.0940, -51.0580, 0.0890, -51.0770, 0.0890, -51.0750, 0.0890, -51.0830, 0.0890, -51.0850, 0.0890, -51.0660, 0.0890, -51.0750, 0.0890, -51.0620, 0.0890, -51.0800, 0.0890, -51.0710, 0.0890, -51.0460, 0.0940, -51.0460, 0.0970, -51.0410, 0.0000, 0.0000, 0.0000 > Another difference between protocols is the carrier frequency. Sony - 40kHz NEC and variants - 38kHz Philips - 36kHz. NEC receiver bandwidth is usually wide enough to receive the others. Edited 2025-07-22 18:13 by phil99 |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2642 |
Which type of Apple TV remote do you have? Footnote added 2025-07-26 12:02 by phil99 Found some extra info on WikiPedia. So my NEC_Rx Sub should give the correct Key (command) code (byte 3) but will ignore the ID code (byte 4). The NEC_Tx Sub however sends an inverted copy of the Key code in byte 4, which may confuse an Apple device receiving it. If there is a demand for it I will write Rx and Tx Subs for it. Footnote added 2025-07-27 18:03 by phil99 Found some more info. |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2944 |
Yes, the original Apple Remote (small silver one) I ‘decided’ a few years back and posted on here. I will do a search later today and update (although someone else may find it before I finish work!). It can definitely be done and worked very reliably ….. |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2944 |
Found it! Forum link If link fails for any reason, then search something like‘: Using an Apple Remote with MicroMite’ Hope this helps! NOTE: if I remember correctly, since writing this code back then, I believe the IR command now decodes the Apple Remote (would need to test to confirm as it’s been a long while since using one! Edited 2025-07-23 15:35 by WhiteWizzard |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2642 |
@WhiteWizzard from your link the Apple remote protocol appears to be a variant of NEC so the MMBasic IR command should work. The pulse timing isn't exactly the same, but close enough. 550µS vs. 560µS, same as Samsung32 variant, which does work. The only other difference is byte 4 is a non-inverted repeat of byte 3 instead of inverted. I think the IR command only uses byte 3 for the Key code so it should be ok. The 16 bit Device code is &HEE87 or 61063. Footnote added 2025-07-26 17:32 by phil99 On further investigation that isn't correct. Byte 4 is a "remote ID number" whose purpose isn't explained. Perhaps it is to allow different Apple remotes to control different things in the same room. |
||||
Plasmamac![]() Guru ![]() Joined: 31/01/2019 Location: GermanyPosts: 580 |
Thx a lot ! Will check it tonight! Thx again Plasma |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2642 |
If that doesn't work this should. 'AppleTV (NEC) IR code logging program. Tested on Samsung32 and NEC remotes.Dim integer Key, Dev, IRpin = MM.Info(pinno GP0) 'IR input pin Dim float b(N), T SetPin IRpin, Din Do :Loop Until Not Pin(IRpin) 'Wait for first falling edge then start recording Do While m<(N-1) 'get pulse/pause times from an IR receiver module Do :Loop Until Pin(IRpin) Or (Timer-T > 50) T=Timer Do While Pin(IRpin) And (Timer-T < 51):Loop b(m) = Timer-T 'record time at falling edge Inc m Loop ' Math V_Print b() For m=2 To 17 If b(m)>0.4 And b(m)<0.8 Then Dev = Dev<<1 If b(m)>1.4 And b(m)<1.8 Then Dev = (Dev<<1) +1 Next For m=18 To 25 If b(m)>0.4 And b(m)<0.8 Then Key = Key<<1 If b(m)>1.4 And b(m)<1.8 Then Key = (Key<<1) +1 Next Print "Device =";Dev, "Key = ";Key End Edit. Converted to a Sub for easier inclusion in a program. 'AppleTV (NEC) IR code logging program. Dim integer Dev, Key, IRpin = MM.Info(pinno GP0) 'IR input pin SetPin IRpin, INTL, IR_Rx 'Main Do Pause 5 If Key>-1 And Dev>0 Then Print "Device =";Dev, "Key =";Key 'Dev>0 = Respond to any device 'Replace Dev>0 (above) with Dev=&Hff87 or other device code you want it to respond to Dev=0 'prepare for next input Loop Sub IR_Rx 'get pulse/pause times from an IR receiver module SetPin IRpin, off 'Disable interrupt Local Float b, T, T2 Local Integer m, N=33 T2=Timer : Dev=0 : Key=0 'reset variables SetPin IRpin, Din 'prepare to read Do While m<=N And (Timer-T2 < 99) Do :Loop Until Pin(IRpin) Or (Timer-T > 29) T=Timer 'record time at falling edge Do While Pin(IRpin) And (Timer-T < 29):Loop b=Timer-T 'record pulse duration If b>11 Then Exit Do If m>1 And m<18 Then 'get Device code Inc Dev, Dev 'Shift bits up one, add 0 for short pulse If b>1.4 And b<1.8 Then Inc Dev 'Add 1 for long pulse EndIf If m>17 And m<26 Then 'get Key code Inc Key, Key 'Shift bits up one, add 0 for short pulse If b>1.4 And b<1.8 Then Inc Key 'Add 1 for long pulse EndIf Inc m Loop If m<N Then Key=-1 'missed some pulses, data invalid Pause 200 'prevent repeats SetPin IRpin, INTL, IR_Rx 'restore interrupt End Sub End Edited 2025-07-24 15:14 by phil99 |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2642 |
To see if the inbuilt IR command can read Apple remotes the NECsend Sub linked to in the second post has been updated to include the Apple protocol. The result is the IR command gives adequate output to identify most buttons on most Apple remotes. The main issues are:- 1/ Apple use the same device code for all remotes even though some have different buttons. 2/ Instead the remotes have a ID code in byte 4 which the IR command ignores. NEC use byte 4 for an inverted copy of the Key code for error detection. 3/ The LSB of the Apple Key byte is a parity bit that is calculated from the Key and ID codes. As the NEC protocol is LSB first and the IR command is MSB first the parity bit is the MSB of the key code returned. To get the original key code:- Key = (Key << 1) AND &hFE ' then reverse bit order. Key = ((Key And 255) * &H0202020202 And &H010884422010) Mod 1023 Footnotes added to the third post explain the Apple protocol teased out by others. The IR_Rx_NEC_V Sub has also been updated to decode both Key and ID. The NECsend Sub has been updated to send Apple codes. . Edited 2025-08-01 13:39 by phil99 |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |