aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--boards.txt43
-rw-r--r--bootloaders/caterina-Arduino_Robot/Caterina-Robot-Control.hex258
-rw-r--r--bootloaders/caterina-Arduino_Robot/Caterina-Robot-Motor.hex258
-rw-r--r--bootloaders/caterina-Arduino_Robot/Caterina-Robot.txt11
-rw-r--r--bootloaders/caterina-Arduino_Robot/Caterina.c780
-rw-r--r--bootloaders/caterina-Arduino_Robot/Caterina.h106
-rw-r--r--bootloaders/caterina-Arduino_Robot/Descriptors.c270
-rw-r--r--bootloaders/caterina-Arduino_Robot/Descriptors.h139
-rw-r--r--bootloaders/caterina-Arduino_Robot/Makefile738
-rw-r--r--bootloaders/caterina-Arduino_Robot/README.md27
-rwxr-xr-xcores/arduino/Arduino.h2
-rw-r--r--cores/arduino/Stream.h2
-rw-r--r--cores/arduino/WInterrupts.c30
-rw-r--r--cores/arduino/WString.cpp61
-rw-r--r--cores/arduino/WString.h10
-rw-r--r--cores/arduino/wiring_analog.c2
-rwxr-xr-xcores/arduino/wiring_private.h4
-rwxr-xr-xcores/robot/Arduino.h215
-rw-r--r--cores/robot/CDC.cpp239
-rw-r--r--cores/robot/Client.h26
-rw-r--r--cores/robot/HID.cpp520
-rw-r--r--cores/robot/HardwareSerial.cpp508
-rw-r--r--cores/robot/HardwareSerial.h115
-rw-r--r--cores/robot/IPAddress.cpp56
-rw-r--r--cores/robot/IPAddress.h76
-rw-r--r--cores/robot/Platform.h23
-rwxr-xr-xcores/robot/Print.cpp268
-rwxr-xr-xcores/robot/Print.h81
-rw-r--r--cores/robot/Printable.h40
-rw-r--r--cores/robot/Server.h9
-rw-r--r--cores/robot/Stream.cpp270
-rw-r--r--cores/robot/Stream.h96
-rwxr-xr-xcores/robot/Tone.cpp616
-rw-r--r--cores/robot/USBAPI.h196
-rw-r--r--cores/robot/USBCore.cpp684
-rw-r--r--cores/robot/USBCore.h303
-rw-r--r--cores/robot/USBDesc.h63
-rw-r--r--cores/robot/Udp.h88
-rw-r--r--cores/robot/WCharacter.h168
-rw-r--r--cores/robot/WInterrupts.c334
-rw-r--r--cores/robot/WMath.cpp60
-rw-r--r--cores/robot/WString.cpp645
-rw-r--r--cores/robot/WString.h206
-rw-r--r--cores/robot/binary.h515
-rw-r--r--cores/robot/main.cpp20
-rw-r--r--cores/robot/new.cpp28
-rw-r--r--cores/robot/new.h24
-rw-r--r--cores/robot/wiring.c324
-rw-r--r--cores/robot/wiring_analog.c284
-rw-r--r--cores/robot/wiring_digital.c178
-rwxr-xr-xcores/robot/wiring_private.h71
-rwxr-xr-xcores/robot/wiring_pulse.c69
-rwxr-xr-xcores/robot/wiring_shift.c55
-rw-r--r--variants/robot_control/pins_arduino.h274
-rw-r--r--variants/robot_motor/pins_arduino.h271
55 files changed, 10745 insertions, 14 deletions
diff --git a/boards.txt b/boards.txt
index 145d551..de9f4ef 100644
--- a/boards.txt
+++ b/boards.txt
@@ -522,3 +522,46 @@ atmega8.build.mcu=atmega8
atmega8.build.f_cpu=16000000L
atmega8.build.core=arduino
atmega8.build.variant=standard
+
+##############################################################
+
+robotControl.name=Arduino Robot Control
+robotControl.upload.protocol=avr109
+robotControl.upload.maximum_size=28672
+robotControl.upload.speed=57600
+robotControl.upload.disable_flushing=true
+robotControl.bootloader.low_fuses=0xff
+robotControl.bootloader.high_fuses=0xd8
+robotControl.bootloader.extended_fuses=0xcb
+robotControl.bootloader.path=caterina-Arduino_Robot
+robotControl.bootloader.file=Caterina-Robot-Control.hex
+robotControl.bootloader.unlock_bits=0x3F
+robotControl.bootloader.lock_bits=0x2F
+robotControl.build.mcu=atmega32u4
+robotControl.build.f_cpu=16000000L
+robotControl.build.vid=0x2341
+robotControl.build.pid=0x8038
+robotControl.build.core=robot
+robotControl.build.variant=robot_control
+
+##############################################################
+
+robotMotor.name=Arduino Robot Motor
+robotMotor.upload.protocol=avr109
+robotMotor.upload.maximum_size=28672
+robotMotor.upload.speed=57600
+robotMotor.upload.disable_flushing=true
+robotMotor.bootloader.low_fuses=0xff
+robotMotor.bootloader.high_fuses=0xd8
+robotMotor.bootloader.extended_fuses=0xcb
+robotMotor.bootloader.path=caterina-Arduino_Robot
+robotMotor.bootloader.file=Caterina-Robot-Motor.hex
+robotMotor.bootloader.unlock_bits=0x3F
+robotMotor.bootloader.lock_bits=0x2F
+robotMotor.build.mcu=atmega32u4
+robotMotor.build.f_cpu=16000000L
+robotMotor.build.vid=0x2341
+robotMotor.build.pid=0x8039
+robotMotor.build.core=robot
+robotMotor.build.variant=robot_motor
+
diff --git a/bootloaders/caterina-Arduino_Robot/Caterina-Robot-Control.hex b/bootloaders/caterina-Arduino_Robot/Caterina-Robot-Control.hex
new file mode 100644
index 0000000..bc13bbb
--- /dev/null
+++ b/bootloaders/caterina-Arduino_Robot/Caterina-Robot-Control.hex
@@ -0,0 +1,258 @@
+:1070000055C000006EC000006CC000006AC00000E7
+:1070100068C0000066C0000064C0000062C00000DC
+:1070200060C000005EC00000FCC400005AC0000048
+:1070300058C0000056C0000054C0000052C00000FC
+:1070400050C000005DC000004CC000004AC00000FD
+:1070500048C0000046C0000044C0000042C000001C
+:1070600040C000003EC000003CC000003AC000002C
+:1070700038C0000036C0000034C0000032C000003C
+:1070800030C000002EC000002CC000002AC000004C
+:1070900028C0000026C0000024C0000022C000005C
+:1070A00020C000001EC000001CC0000011241FBE34
+:1070B000CFEFDAE0DEBFCDBF11E0A0E0B1E0E6E463
+:1070C000FFE702C005900D92AC3AB107D9F711E085
+:1070D000ACEAB1E001C01D92A53CB107E1F74FD386
+:1070E00030C78ECFF89410926F001092810081E02B
+:1070F00085BF15BE47985D9A289A0C9400000895A4
+:107100001F920F920FB60F9211242F938F939F937C
+:10711000EF93FF9310928500109284008091AC0150
+:107120009091AD01009741F001979093AD0180934C
+:10713000AC01892B09F45D9A8091AE019091AF0169
+:10714000009741F001979093AF018093AE01892B96
+:1071500009F4289A8091B2019091B301019690931D
+:10716000B3018093B201E0E0F0E0859194918F5FEC
+:107170009F4F49F08091B0019091B1010196909399
+:10718000B1018093B001FF91EF919F918F912F9169
+:107190000F900FBE0F901F90189584E08093E90028
+:1071A0000DC08091E8008B778093E80003C08EB318
+:1071B000882351F08091E80082FFF9CF8091E800A8
+:1071C00085FFEFCF8091F1000895982F83E08093A1
+:1071D000E9008091E80085FD0DC08091E8008E7780
+:1071E0008093E80003C08EB3882369F08091E800A3
+:1071F00080FFF9CF9093F1005D9884E690E0909342
+:10720000AD018093AC0108954F925F926F927F928F
+:107210008F929F92AF92BF92CF92DF92EF92FF92A6
+:107220000F931F93CF93DF9384E08093E9008091C5
+:10723000E80082FF57C2289884E690E09093AF015F
+:107240008093AE01AADF182F853481F48CE49DE190
+:107250009093B1018093B00107B600FCFDCFF9997E
+:10726000FECF81E180935700E89503C0843519F47F
+:1072700094DF8DE00DC28C34E1F38035D1F3843797
+:1072800021F484E4A2DF80E003C2813611F489E5B1
+:10729000FFC18134B1F481DF182F7FDF90E0880FC8
+:1072A000991FAA2797FDA095BA2F312F330F20E001
+:1072B000442737FD4095542F822B932BA42BB52BBD
+:1072C000B8C1803711F483E5E3C1833549F4C0E0E8
+:1072D000D1E089917ADF21E0C730D207D1F7D9C157
+:1072E000863521F481E371DF80E3D2C1833731F445
+:1072F00087E86BDF85E969DF8EE1CAC18536B9F4BD
+:10730000E0E0F0E093E085E090935700E89507B661
+:1073100000FCFDCF80935700E89507B600FCFDCF39
+:10732000E058FF4FA0E7E030FA0771F7A2CF8237AD
+:1073300039F4E1E0F0E089E0809357008491A8C13E
+:10734000863439F4E0E0F0E089E0809357008491DE
+:107350009FC18E3439F4E3E0F0E089E08093570078
+:10736000849196C1813539F4E2E0F0E089E08093C0
+:10737000570084918DC1823631F489E526DF80E0A3
+:1073800024DF80E885C1823419F0873609F0E5C032
+:107390001092B1011092B00100DF082FFEDEF82E2E
+:1073A000FCDE682E8554823008F071C1902F80E099
+:1073B000CF2DD0E0C82BD92B10926F00173609F0D3
+:1073C0004BC081E180935700E895DD24CC24C39421
+:1073D0003FC0E090B501F090B6010091B701109167
+:1073E000B801B6E46B16D9F4ED2DF0E0EE29FF29D3
+:1073F000E4918E2FEADEDD2081F082E090E0A0E0D3
+:10740000B0E0E80EF91E0A1F1B1FE092B501F092D2
+:10741000B6010093B7011093B801DC2418C0D8015D
+:10742000C701B695A7959795879575D5CEDE82E06D
+:1074300090E0A0E0B0E0E80EF91E0A1F1B1FE092EA
+:10744000B501F092B6010093B7011093B8012197EE
+:10745000209709F0BECF7DC08090B5019090B60115
+:10746000A090B701B090B80196E4691609F05DC02C
+:1074700083E0F40180935700E89507B600FCFDCF48
+:1074800054C0F6E46F1661F5772031F1E090B50154
+:10749000F090B6010091B7011091B8017EDED82EB0
+:1074A000CC24852D90E08C299D29F7010C01409278
+:1074B0005700E895112482E090E0A0E0B0E0E80EEB
+:1074C000F91E0A1F1B1FE092B501F092B60100934E
+:1074D000B7011093B80102C060DE582E742423C097
+:1074E000E090B501F090B6010091B7011091B8019C
+:1074F00016950795F794E79450DE682FC70113D5CA
+:107500008091B5019091B601A091B701B091B801F9
+:107510000296A11DB11D8093B5019093B601A09371
+:10752000B701B093B801219704C0552477244424AF
+:107530004394209709F0A5CF96E4691641F485E0BD
+:10754000F40180935700E89507B600FCFDCF8DE06D
+:107550003CDE82E080936F009CC0833471F4009124
+:10756000B5011091B60119DE90E021E0F8010C019F
+:1075700020935700E89511247CCE833619F5E090CE
+:10758000B501F090B6010091B7011091B80105DE88
+:10759000F701E16090E021E00C0120935700E895AD
+:1075A000112482E090E0A0E0B0E0E80EF91E0A1F8E
+:1075B0001B1FE092B501F092B6010093B701109342
+:1075C000B80157CE8D3661F4E091B501F091B60166
+:1075D00085E080935700E89507B600FCFDCF49CEC3
+:1075E000823551F4E091B501F091B6010591149105
+:1075F000812FEBDD802F4CC0843421F5E090B50164
+:10760000F090B6010091B7011091B8011695079559
+:10761000F794E794C2DD682FC70185D48091B50146
+:107620009091B601A091B701B091B8010296A11D49
+:10763000B11D8093B5019093B601A093B701B093AB
+:10764000B80117CE843609F5E090B501F090B60187
+:107650000091B7011091B801D801C701B695A7955F
+:107660009795879558D4B1DD82E090E0A0E0B0E036
+:10767000E80EF91E0A1F1B1FE092B501F092B60139
+:107680000093B7011093B80104C08B3111F08FE360
+:107690009CDD83E08093E9009091E8008091E80010
+:1076A0008E778093E80095FF04C010C08EB38823C6
+:1076B000C9F08091E80080FFF9CF8091E8008E77D3
+:1076C0008093E80003C08EB3882361F08091E800C6
+:1076D00080FFF9CF84E08093E9008091E8008B7708
+:1076E0008093E800DF91CF911F910F91FF90EF9071
+:1076F000DF90CF90BF90AF909F908F907F906F90D2
+:107700005F904F9008959091BD01892F8F778132BE
+:1077100049F58091BE018032A1F0813219F5913A8C
+:1077200009F58091E800877F8093E8008CE091E084
+:1077300067E070E027D28091E8008B778093E800C3
+:107740000895913279F48091E800877F8093E80072
+:107750008CE091E067E070E079D28091E8008E776C
+:107760008093E800089582E061EC42E0D1D083E0AC
+:1077700061E842E1CDD084E060E842E1C9C01F93F6
+:10778000209100081092000844B714BE88E10FB69B
+:10779000F89480936000109260000FBE80E8E0E0F3
+:1077A000F0E00FB6F89480936100E09361000FBEA3
+:1077B00031E035BF92E095BF3F9A209A559A809369
+:1077C00061001092610047985D9A289A1092890092
+:1077D0008AEF8093880090936F0083E0809381000C
+:1077E000859194918F5F9F4F11F03093B401942F46
+:1077F00041FF19C0809109012817A9F08093000862
+:10780000789480911301882339F08091B20190918E
+:10781000B3018F5E9240C8F310920008F89481E0A3
+:10782000809313010CC090FF04C08091B4018823A1
+:1078300051F493FF09C080910901281729F0809124
+:10784000B401882309F04EDCD4D078941092B101B1
+:107850001092B0011EEF20C0D7DC4BD38091B00155
+:107860009091B10181549F4110F0109213018091C9
+:10787000B9019091BA0101969093BA018093B90130
+:10788000292F97FF03C0512F591B252F220F28178F
+:1078900010F4479801C0479A809113018823E1F6BC
+:1078A0008091E00081608093E0001CDC80E090E04B
+:1078B0001F910895FA01923049F0933061F09130B0
+:1078C000F9F484E191E022E130E01EC086E291E02B
+:1078D0002EE330E019C0882329F484E691E024E007
+:1078E00030E012C0813029F488E691E028E230E0EF
+:1078F0000BC0823029F482E991E028E130E004C035
+:1079000080E090E020E030E091838083C901089519
+:107910008093E9008091EB0081608093EB001092EE
+:10792000ED006093EC004093ED008091EE00881F25
+:107930008827881F08958091BD0188238CF403C097
+:107940008EB38823B1F08091E80082FFF9CF809157
+:10795000E8008B778093E80008958EB3882349F080
+:107960008091E80080FFF9CF8091E8008E778093C6
+:10797000E8000895EF92FF920F931F9345D04CD0EB
+:1079800008ED10E0F80180818F7780838081806826
+:10799000808380818F7D808319BC1EBA1092BB01C9
+:1079A00080EEE82EF12CF70180818B7F8083F80137
+:1079B00080818160808380E060E042E0A9DFE1EEC9
+:1079C000F0E080818E7F8083E2EEF0E08081816054
+:1079D0008083808188608083F70180818E7F8083AF
+:1079E000F8018081806180831F910F91FF90EF905B
+:1079F0000895E7EDF0E08081816080838AE482BFB2
+:107A000081E08093BC01B6CFE8EDF0E080818E7F0D
+:107A100080831092E20008951092DA001092E10043
+:107A200008951F920F920FB60F9211242F933F9338
+:107A30004F935F936F937F938F939F93AF93BF9376
+:107A4000EF93FF938091DA0080FF1BC08091D800F4
+:107A500080FF17C08091DA008E7F8093DA008091DA
+:107A6000D90080FF0BC080E189BD82E189BD09B4E6
+:107A700000FEFDCF81E08EBB3BD203C019BC1EBA15
+:107A800037D28091E10080FF17C08091E20080FF33
+:107A900013C08091E2008E7F8093E2008091E2002B
+:107AA00080618093E2008091D80080628093D8004A
+:107AB00019BC85E08EBB1CD28091E10084FF2CC0F4
+:107AC0008091E20084FF28C080E189BD82E189BD08
+:107AD00009B400FEFDCF8091D8008F7D8093D8003F
+:107AE0008091E1008F7E8093E1008091E2008F7EA3
+:107AF0008093E2008091E20081608093E2008091B7
+:107B0000BB01882331F48091E30087FD02C081E04E
+:107B100001C084E08EBBECD18091E10083FF21C0E5
+:107B20008091E20083FF1DC08091E100877F8093F8
+:107B3000E10082E08EBB1092BB018091E1008E7F5C
+:107B40008093E1008091E2008E7F8093E20080913B
+:107B5000E20080618093E20080E060E042E0D8DEF5
+:107B6000C7D1FF91EF91BF91AF919F918F917F917D
+:107B70006F915F914F913F912F910F900FBE0F909A
+:107B80001F9018959C014091C3015091C401461764
+:107B9000570718F4F90190E044C06115710511F020
+:107BA000AB01F8CF8091E8008E778093E80040E049
+:107BB00050E0F0CF8EB3882309F444C0853009F437
+:107BC00043C08091E80083FF02C081E00895809166
+:107BD000E80082FD31C08091E80080FF22C08091E2
+:107BE000F3009091F200782F60E0292F30E0262BEF
+:107BF000372B07C081918093F100415050402F5F97
+:107C00003F4F4115510519F02830310598F390E0A8
+:107C10002830310509F491E08091E8008E77809357
+:107C2000E8004115510531F6992321F605C08EB3C0
+:107C3000882341F0853041F08091E80082FFF7CF42
+:107C400080E0089582E0089583E008959C01611525
+:107C5000710529F48091E8008B778093E800F901A1
+:107C600026C08EB3882391F1853091F18091E80090
+:107C700083FF02C081E008958091E80082FFF1CF88
+:107C800006C08091F10081936150704059F02091BD
+:107C9000F3008091F200322F20E090E0822B932BB2
+:107CA000892B79F78091E8008B778093E800611544
+:107CB0007105B9F605C08EB3882341F0853041F0D7
+:107CC0008091E80080FFF7CF80E0089582E008957A
+:107CD00083E008950F931F93DF93CF9300D0CDB728
+:107CE000DEB7EDEBF1E08091F100819381E0E53CBE
+:107CF000F807C9F708DD8091E80083FFE4C08091B0
+:107D0000BD019091BE01953009F46DC0963040F4EC
+:107D1000913081F1913070F0933009F0D4C02AC0D5
+:107D2000983009F4A3C0993009F4B2C0963009F034
+:107D3000CAC07CC0803809F4C6C0823809F0C3C00C
+:107D40008091C10187708093E9008091EB001092CF
+:107D5000E9002091E800277F2093E80090E025E0EB
+:107D6000969587952A95E1F781708093F10010929E
+:107D7000F10087C0882319F0823009F0A4C08F7108
+:107D8000823009F0A0C08091BF01882331F5209195
+:107D9000C101277009F497C02093E9008091EB009E
+:107DA00080FF1BC0933021F48091EB00806213C0F0
+:107DB0008091EB0080618093EB0081E090E002C055
+:107DC000880F991F2A95E2F78093EA001092EA0043
+:107DD0008091EB0088608093EB001092E900809125
+:107DE000E800877F51C0882309F06DC01091BF0162
+:107DF0001F770FB7F8948091E800877F8093E800A1
+:107E00009ADD8091E80080FFFCCF8091E3008078CC
+:107E1000812B8093E30080688093E300112311F4A9
+:107E200082E001C083E08EBB0FBF4DC0805882301E
+:107E300008F049C08091BF019091C0016091C101DB
+:107E4000AE014F5F5F4F36DDBC01009709F43BC0C8
+:107E50008091E800877F8093E80089819A8192DE93
+:107E60008091E8008B778093E8002DC0803859F529
+:107E70008091E800877F8093E8008091BB01809328
+:107E8000F1008091E8008E778093E80054DD1BC0FC
+:107E90008823C9F49091BF019230A8F48091E80042
+:107EA000877F8093E8009093BB0145DD8091BB0103
+:107EB000882331F48091E30087FD02C081E001C096
+:107EC00084E08EBB50DC8091E80083FF0AC0809183
+:107ED000EB0080628093EB008091E800877F8093C5
+:107EE000E8000F900F90CF91DF911F910F910895AF
+:107EF00008951F938EB3882361F01091E9001092CA
+:107F0000E9008091E80083FF01C0E4DE1770109360
+:107F1000E9001F910895F999FECF92BD81BDF89AAD
+:107F2000992780B50895262FF999FECF1FBA92BDE3
+:107F300081BD20BD0FB6F894FA9AF99A0FBE01964A
+:067F40000895F894FFCF44
+:107F46004341544552494E41007700080000000065
+:107F56000000080112011001020000084123390047
+:107F660001000201000109023E00020100803209FF
+:107F7600040000010202010005240010010424028D
+:107F8600040524060001070582030800FF09040111
+:107F960000020A000000070504021000010705831D
+:107FA6000210000104030904280352006F00620056
+:107FB6006F00740020004D006F0074006F007200A7
+:107FC600200042006F006100720064002000200063
+:107FD60000001803410072006400750069006E001D
+:0C7FE6006F0020004C004C004300000025
+:040000030000700089
+:00000001FF
diff --git a/bootloaders/caterina-Arduino_Robot/Caterina-Robot-Motor.hex b/bootloaders/caterina-Arduino_Robot/Caterina-Robot-Motor.hex
new file mode 100644
index 0000000..b5560ba
--- /dev/null
+++ b/bootloaders/caterina-Arduino_Robot/Caterina-Robot-Motor.hex
@@ -0,0 +1,258 @@
+:1070000055C000006EC000006CC000006AC00000E7
+:1070100068C0000066C0000064C0000062C00000DC
+:1070200060C000005EC00000FCC400005AC0000048
+:1070300058C0000056C0000054C0000052C00000FC
+:1070400050C000005DC000004CC000004AC00000FD
+:1070500048C0000046C0000044C0000042C000001C
+:1070600040C000003EC000003CC000003AC000002C
+:1070700038C0000036C0000034C0000032C000003C
+:1070800030C000002EC000002CC000002AC000004C
+:1070900028C0000026C0000024C0000022C000005C
+:1070A00020C000001EC000001CC0000011241FBE34
+:1070B000CFEFDAE0DEBFCDBF11E0A0E0B1E0E6E463
+:1070C000FFE702C005900D92AC3AB107D9F711E085
+:1070D000ACEAB1E001C01D92A53CB107E1F74FD386
+:1070E00030C78ECFF89410926F001092810081E02B
+:1070F00085BF15BE47985D9A289A0C9400000895A4
+:107100001F920F920FB60F9211242F938F939F937C
+:10711000EF93FF9310928500109284008091AC0150
+:107120009091AD01009741F001979093AD0180934C
+:10713000AC01892B09F45D9A8091AE019091AF0169
+:10714000009741F001979093AF018093AE01892B96
+:1071500009F4289A8091B2019091B301019690931D
+:10716000B3018093B201E0E0F0E0859194918F5FEC
+:107170009F4F49F08091B0019091B1010196909399
+:10718000B1018093B001FF91EF919F918F912F9169
+:107190000F900FBE0F901F90189584E08093E90028
+:1071A0000DC08091E8008B778093E80003C08EB318
+:1071B000882351F08091E80082FFF9CF8091E800A8
+:1071C00085FFEFCF8091F1000895982F83E08093A1
+:1071D000E9008091E80085FD0DC08091E8008E7780
+:1071E0008093E80003C08EB3882369F08091E800A3
+:1071F00080FFF9CF9093F1005D9884E690E0909342
+:10720000AD018093AC0108954F925F926F927F928F
+:107210008F929F92AF92BF92CF92DF92EF92FF92A6
+:107220000F931F93CF93DF9384E08093E9008091C5
+:10723000E80082FF57C2289884E690E09093AF015F
+:107240008093AE01AADF182F853481F48CE49DE190
+:107250009093B1018093B00107B600FCFDCFF9997E
+:10726000FECF81E180935700E89503C0843519F47F
+:1072700094DF8DE00DC28C34E1F38035D1F3843797
+:1072800021F484E4A2DF80E003C2813611F489E5B1
+:10729000FFC18134B1F481DF182F7FDF90E0880FC8
+:1072A000991FAA2797FDA095BA2F312F330F20E001
+:1072B000442737FD4095542F822B932BA42BB52BBD
+:1072C000B8C1803711F483E5E3C1833549F4C0E0E8
+:1072D000D1E089917ADF21E0C730D207D1F7D9C157
+:1072E000863521F481E371DF80E3D2C1833731F445
+:1072F00087E86BDF85E969DF8EE1CAC18536B9F4BD
+:10730000E0E0F0E093E085E090935700E89507B661
+:1073100000FCFDCF80935700E89507B600FCFDCF39
+:10732000E058FF4FA0E7E030FA0771F7A2CF8237AD
+:1073300039F4E1E0F0E089E0809357008491A8C13E
+:10734000863439F4E0E0F0E089E0809357008491DE
+:107350009FC18E3439F4E3E0F0E089E08093570078
+:10736000849196C1813539F4E2E0F0E089E08093C0
+:10737000570084918DC1823631F489E526DF80E0A3
+:1073800024DF80E885C1823419F0873609F0E5C032
+:107390001092B1011092B00100DF082FFEDEF82E2E
+:1073A000FCDE682E8554823008F071C1902F80E099
+:1073B000CF2DD0E0C82BD92B10926F00173609F0D3
+:1073C0004BC081E180935700E895DD24CC24C39421
+:1073D0003FC0E090B501F090B6010091B701109167
+:1073E000B801B6E46B16D9F4ED2DF0E0EE29FF29D3
+:1073F000E4918E2FEADEDD2081F082E090E0A0E0D3
+:10740000B0E0E80EF91E0A1F1B1FE092B501F092D2
+:10741000B6010093B7011093B801DC2418C0D8015D
+:10742000C701B695A7959795879575D5CEDE82E06D
+:1074300090E0A0E0B0E0E80EF91E0A1F1B1FE092EA
+:10744000B501F092B6010093B7011093B8012197EE
+:10745000209709F0BECF7DC08090B5019090B60115
+:10746000A090B701B090B80196E4691609F05DC02C
+:1074700083E0F40180935700E89507B600FCFDCF48
+:1074800054C0F6E46F1661F5772031F1E090B50154
+:10749000F090B6010091B7011091B8017EDED82EB0
+:1074A000CC24852D90E08C299D29F7010C01409278
+:1074B0005700E895112482E090E0A0E0B0E0E80EEB
+:1074C000F91E0A1F1B1FE092B501F092B60100934E
+:1074D000B7011093B80102C060DE582E742423C097
+:1074E000E090B501F090B6010091B7011091B8019C
+:1074F00016950795F794E79450DE682FC70113D5CA
+:107500008091B5019091B601A091B701B091B801F9
+:107510000296A11DB11D8093B5019093B601A09371
+:10752000B701B093B801219704C0552477244424AF
+:107530004394209709F0A5CF96E4691641F485E0BD
+:10754000F40180935700E89507B600FCFDCF8DE06D
+:107550003CDE82E080936F009CC0833471F4009124
+:10756000B5011091B60119DE90E021E0F8010C019F
+:1075700020935700E89511247CCE833619F5E090CE
+:10758000B501F090B6010091B7011091B80105DE88
+:10759000F701E16090E021E00C0120935700E895AD
+:1075A000112482E090E0A0E0B0E0E80EF91E0A1F8E
+:1075B0001B1FE092B501F092B6010093B701109342
+:1075C000B80157CE8D3661F4E091B501F091B60166
+:1075D00085E080935700E89507B600FCFDCF49CEC3
+:1075E000823551F4E091B501F091B6010591149105
+:1075F000812FEBDD802F4CC0843421F5E090B50164
+:10760000F090B6010091B7011091B8011695079559
+:10761000F794E794C2DD682FC70185D48091B50146
+:107620009091B601A091B701B091B8010296A11D49
+:10763000B11D8093B5019093B601A093B701B093AB
+:10764000B80117CE843609F5E090B501F090B60187
+:107650000091B7011091B801D801C701B695A7955F
+:107660009795879558D4B1DD82E090E0A0E0B0E036
+:10767000E80EF91E0A1F1B1FE092B501F092B60139
+:107680000093B7011093B80104C08B3111F08FE360
+:107690009CDD83E08093E9009091E8008091E80010
+:1076A0008E778093E80095FF04C010C08EB38823C6
+:1076B000C9F08091E80080FFF9CF8091E8008E77D3
+:1076C0008093E80003C08EB3882361F08091E800C6
+:1076D00080FFF9CF84E08093E9008091E8008B7708
+:1076E0008093E800DF91CF911F910F91FF90EF9071
+:1076F000DF90CF90BF90AF909F908F907F906F90D2
+:107700005F904F9008959091BD01892F8F778132BE
+:1077100049F58091BE018032A1F0813219F5913A8C
+:1077200009F58091E800877F8093E8008CE091E084
+:1077300067E070E027D28091E8008B778093E800C3
+:107740000895913279F48091E800877F8093E80072
+:107750008CE091E067E070E079D28091E8008E776C
+:107760008093E800089582E061EC42E0D1D083E0AC
+:1077700061E842E1CDD084E060E842E1C9C01F93F6
+:10778000209100081092000844B714BE88E10FB69B
+:10779000F89480936000109260000FBE80E8E0E0F3
+:1077A000F0E00FB6F89480936100E09361000FBEA3
+:1077B00031E035BF92E095BF3F9A209A559A809369
+:1077C00061001092610047985D9A289A1092890092
+:1077D0008AEF8093880090936F0083E0809381000C
+:1077E000859194918F5F9F4F11F03093B401942F46
+:1077F00041FF19C0809109012817A9F08093000862
+:10780000789480911301882339F08091B20190918E
+:10781000B3018F5E9240C8F310920008F89481E0A3
+:10782000809313010CC090FF04C08091B4018823A1
+:1078300051F493FF09C080910901281729F0809124
+:10784000B401882309F04EDCD4D078941092B101B1
+:107850001092B0011EEF20C0D7DC4BD38091B00155
+:107860009091B10181549F4110F0109213018091C9
+:10787000B9019091BA0101969093BA018093B90130
+:10788000292F97FF03C0512F591B252F220F28178F
+:1078900010F4479801C0479A809113018823E1F6BC
+:1078A0008091E00081608093E0001CDC80E090E04B
+:1078B0001F910895FA01923049F0933061F09130B0
+:1078C000F9F484E191E022E130E01EC086E291E02B
+:1078D0002EE330E019C0882329F484E691E024E007
+:1078E00030E012C0813029F488E691E028E230E0EF
+:1078F0000BC0823029F482E991E028E130E004C035
+:1079000080E090E020E030E091838083C901089519
+:107910008093E9008091EB0081608093EB001092EE
+:10792000ED006093EC004093ED008091EE00881F25
+:107930008827881F08958091BD0188238CF403C097
+:107940008EB38823B1F08091E80082FFF9CF809157
+:10795000E8008B778093E80008958EB3882349F080
+:107960008091E80080FFF9CF8091E8008E778093C6
+:10797000E8000895EF92FF920F931F9345D04CD0EB
+:1079800008ED10E0F80180818F7780838081806826
+:10799000808380818F7D808319BC1EBA1092BB01C9
+:1079A00080EEE82EF12CF70180818B7F8083F80137
+:1079B00080818160808380E060E042E0A9DFE1EEC9
+:1079C000F0E080818E7F8083E2EEF0E08081816054
+:1079D0008083808188608083F70180818E7F8083AF
+:1079E000F8018081806180831F910F91FF90EF905B
+:1079F0000895E7EDF0E08081816080838AE482BFB2
+:107A000081E08093BC01B6CFE8EDF0E080818E7F0D
+:107A100080831092E20008951092DA001092E10043
+:107A200008951F920F920FB60F9211242F933F9338
+:107A30004F935F936F937F938F939F93AF93BF9376
+:107A4000EF93FF938091DA0080FF1BC08091D800F4
+:107A500080FF17C08091DA008E7F8093DA008091DA
+:107A6000D90080FF0BC080E189BD82E189BD09B4E6
+:107A700000FEFDCF81E08EBB3BD203C019BC1EBA15
+:107A800037D28091E10080FF17C08091E20080FF33
+:107A900013C08091E2008E7F8093E2008091E2002B
+:107AA00080618093E2008091D80080628093D8004A
+:107AB00019BC85E08EBB1CD28091E10084FF2CC0F4
+:107AC0008091E20084FF28C080E189BD82E189BD08
+:107AD00009B400FEFDCF8091D8008F7D8093D8003F
+:107AE0008091E1008F7E8093E1008091E2008F7EA3
+:107AF0008093E2008091E20081608093E2008091B7
+:107B0000BB01882331F48091E30087FD02C081E04E
+:107B100001C084E08EBBECD18091E10083FF21C0E5
+:107B20008091E20083FF1DC08091E100877F8093F8
+:107B3000E10082E08EBB1092BB018091E1008E7F5C
+:107B40008093E1008091E2008E7F8093E20080913B
+:107B5000E20080618093E20080E060E042E0D8DEF5
+:107B6000C7D1FF91EF91BF91AF919F918F917F917D
+:107B70006F915F914F913F912F910F900FBE0F909A
+:107B80001F9018959C014091C3015091C401461764
+:107B9000570718F4F90190E044C06115710511F020
+:107BA000AB01F8CF8091E8008E778093E80040E049
+:107BB00050E0F0CF8EB3882309F444C0853009F437
+:107BC00043C08091E80083FF02C081E00895809166
+:107BD000E80082FD31C08091E80080FF22C08091E2
+:107BE000F3009091F200782F60E0292F30E0262BEF
+:107BF000372B07C081918093F100415050402F5F97
+:107C00003F4F4115510519F02830310598F390E0A8
+:107C10002830310509F491E08091E8008E77809357
+:107C2000E8004115510531F6992321F605C08EB3C0
+:107C3000882341F0853041F08091E80082FFF7CF42
+:107C400080E0089582E0089583E008959C01611525
+:107C5000710529F48091E8008B778093E800F901A1
+:107C600026C08EB3882391F1853091F18091E80090
+:107C700083FF02C081E008958091E80082FFF1CF88
+:107C800006C08091F10081936150704059F02091BD
+:107C9000F3008091F200322F20E090E0822B932BB2
+:107CA000892B79F78091E8008B778093E800611544
+:107CB0007105B9F605C08EB3882341F0853041F0D7
+:107CC0008091E80080FFF7CF80E0089582E008957A
+:107CD00083E008950F931F93DF93CF9300D0CDB728
+:107CE000DEB7EDEBF1E08091F100819381E0E53CBE
+:107CF000F807C9F708DD8091E80083FFE4C08091B0
+:107D0000BD019091BE01953009F46DC0963040F4EC
+:107D1000913081F1913070F0933009F0D4C02AC0D5
+:107D2000983009F4A3C0993009F4B2C0963009F034
+:107D3000CAC07CC0803809F4C6C0823809F0C3C00C
+:107D40008091C10187708093E9008091EB001092CF
+:107D5000E9002091E800277F2093E80090E025E0EB
+:107D6000969587952A95E1F781708093F10010929E
+:107D7000F10087C0882319F0823009F0A4C08F7108
+:107D8000823009F0A0C08091BF01882331F5209195
+:107D9000C101277009F497C02093E9008091EB009E
+:107DA00080FF1BC0933021F48091EB00806213C0F0
+:107DB0008091EB0080618093EB0081E090E002C055
+:107DC000880F991F2A95E2F78093EA001092EA0043
+:107DD0008091EB0088608093EB001092E900809125
+:107DE000E800877F51C0882309F06DC01091BF0162
+:107DF0001F770FB7F8948091E800877F8093E800A1
+:107E00009ADD8091E80080FFFCCF8091E3008078CC
+:107E1000812B8093E30080688093E300112311F4A9
+:107E200082E001C083E08EBB0FBF4DC0805882301E
+:107E300008F049C08091BF019091C0016091C101DB
+:107E4000AE014F5F5F4F36DDBC01009709F43BC0C8
+:107E50008091E800877F8093E80089819A8192DE93
+:107E60008091E8008B778093E8002DC0803859F529
+:107E70008091E800877F8093E8008091BB01809328
+:107E8000F1008091E8008E778093E80054DD1BC0FC
+:107E90008823C9F49091BF019230A8F48091E80042
+:107EA000877F8093E8009093BB0145DD8091BB0103
+:107EB000882331F48091E30087FD02C081E001C096
+:107EC00084E08EBB50DC8091E80083FF0AC0809183
+:107ED000EB0080628093EB008091E800877F8093C5
+:107EE000E8000F900F90CF91DF911F910F910895AF
+:107EF00008951F938EB3882361F01091E9001092CA
+:107F0000E9008091E80083FF01C0E4DE1770109360
+:107F1000E9001F910895F999FECF92BD81BDF89AAD
+:107F2000992780B50895262FF999FECF1FBA92BDE3
+:107F300081BD20BD0FB6F894FA9AF99A0FBE01964A
+:067F40000895F894FFCF44
+:107F46004341544552494E41007700080000000065
+:107F56000000080112011001020000084123380048
+:107F660001000201000109023E00020100803209FF
+:107F7600040000010202010005240010010424028D
+:107F8600040524060001070582030800FF09040111
+:107F960000020A000000070504021000010705831D
+:107FA6000210000104030904280352006F00620056
+:107FB6006F007400200043006F006E0074007200B2
+:107FC6006F006C00200042006F00610072006400C8
+:107FD60000001803410072006400750069006E001D
+:0C7FE6006F0020004C004C004300000025
+:040000030000700089
+:00000001FF
diff --git a/bootloaders/caterina-Arduino_Robot/Caterina-Robot.txt b/bootloaders/caterina-Arduino_Robot/Caterina-Robot.txt
new file mode 100644
index 0000000..5beb659
--- /dev/null
+++ b/bootloaders/caterina-Arduino_Robot/Caterina-Robot.txt
@@ -0,0 +1,11 @@
+Builds against LUFA version 111009
+make version 3.81
+avrdude version 5.11
+
+All AVR tools except avrdude were installed by CrossPack 20100115:
+avr-gcc version 4.3.3 (GCC)
+Thread model: single
+Configured with: ../configure —prefix=/usr/local/CrossPack-AVR-20100115 —disable-dependency-tracking —disable-nls —disable-werror —target=avr —enable-languages=c,c++ —disable-nls —disable-libssp —with-dwarf2
+avr-libc version 1.6.7
+binutils version 2.19
+
diff --git a/bootloaders/caterina-Arduino_Robot/Caterina.c b/bootloaders/caterina-Arduino_Robot/Caterina.c
new file mode 100644
index 0000000..c462420
--- /dev/null
+++ b/bootloaders/caterina-Arduino_Robot/Caterina.c
@@ -0,0 +1,780 @@
+/*
+ LUFA Library
+ Copyright (C) Dean Camera, 2011.
+
+ dean [at] fourwalledcubicle [dot] com
+ www.lufa-lib.org
+*/
+
+/*
+ Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
+
+ Permission to use, copy, modify, distribute, and sell this
+ software and its documentation for any purpose is hereby granted
+ without fee, provided that the above copyright notice appear in
+ all copies and that both that the copyright notice and this
+ permission notice and warranty disclaimer appear in supporting
+ documentation, and that the name of the author not be used in
+ advertising or publicity pertaining to distribution of the
+ software without specific, written prior permission.
+
+ The author disclaim all warranties with regard to this
+ software, including all implied warranties of merchantability
+ and fitness. In no event shall the author be liable for any
+ special, indirect or consequential damages or any damages
+ whatsoever resulting from loss of use, data or profits, whether
+ in an action of contract, negligence or other tortious action,
+ arising out of or in connection with the use or performance of
+ this software.
+*/
+
+/** \file
+ *
+ * Main source file for the CDC class bootloader. This file contains the complete bootloader logic.
+ */
+
+#define INCLUDE_FROM_CATERINA_C
+#include "Caterina.h"
+
+/** Contains the current baud rate and other settings of the first virtual serial port. This must be retained as some
+ * operating systems will not open the port unless the settings can be set successfully.
+ */
+static CDC_LineEncoding_t LineEncoding = { .BaudRateBPS = 0,
+ .CharFormat = CDC_LINEENCODING_OneStopBit,
+ .ParityType = CDC_PARITY_None,
+ .DataBits = 8 };
+
+/** Current address counter. This stores the current address of the FLASH or EEPROM as set by the host,
+ * and is used when reading or writing to the AVRs memory (either FLASH or EEPROM depending on the issued
+ * command.)
+ */
+static uint32_t CurrAddress;
+
+/** Flag to indicate if the bootloader should be running, or should exit and allow the application code to run
+ * via a watchdog reset. When cleared the bootloader will exit, starting the watchdog and entering an infinite
+ * loop until the AVR restarts and the application runs.
+ */
+static bool RunBootloader = true;
+
+/* Pulse generation counters to keep track of the time remaining for each pulse type */
+#define TX_RX_LED_PULSE_PERIOD 100
+uint16_t TxLEDPulse = 0; // time remaining for Tx LED pulse
+uint16_t RxLEDPulse = 0; // time remaining for Rx LED pulse
+
+/* Bootloader timeout timer */
+// MAH 8/15/12- change so timeouts work properly when the chip is running at 8MHz instead of 16.
+#define TIMEOUT_PERIOD 8000
+#define EXT_RESET_TIMEOUT_PERIOD 750
+
+
+/*********************************************************************************************************
+LilyPadUSB bootloader code
+The LilyPadUSB bootloader has been changed to remove the 8-second delay after external reset which is in
+the Leonardo. To enter the bootloader, the user should execute TWO external resets within 750 ms; that is,
+press the reset button twice, quickly.\
+
+Some other changes were made to allow this code to compile tightly enough to fit in the alloted 4k of
+bootloader space.
+*/
+// MAH 8/15/12- added this flag to replace the bulky program memory reads to check for the presence of a sketch
+// at the top of the memory space.
+static bool sketchPresent = false;
+
+// MAH 8/15/12- make this volatile, since we modify it in one place and read it in another, we want to make
+// sure we're always working on the copy in memory and not an erroneous value stored in a cache somewhere.
+// This variable stores the length of time we've been in the bootloader when waiting for the 8 second delay.
+volatile uint16_t Timeout = 0;
+// MAH 8/15/12- added this for delay during startup. Did not use existing Timeout value b/c it only increments
+// when there's a sketch at the top of the memory.
+volatile uint16_t resetTimeout = 0;
+
+// MAH 8/15/12- let's make this an 8-bit value instead of 16- that saves on memory because 16-bit addition and
+// comparison compiles to bulkier code. Note that this does *not* require a change to the Arduino core- we're
+// just sort of ignoring the extra byte that the Arduino core puts at the next location.
+uint8_t bootKey = 0x77;
+volatile uint8_t *const bootKeyPtr = (volatile uint8_t *)0x0800;
+
+// StartSketch() is called to clean up our mess before passing execution to the sketch.
+void StartSketch(void)
+{
+ cli();
+
+ /* Undo TIMER1 setup and clear the count before running the sketch */
+ TIMSK1 = 0;
+ TCCR1B = 0;
+
+ /* Relocate the interrupt vector table to the application section */
+ MCUCR = (1 << IVCE);
+ MCUCR = 0;
+
+ L_LED_OFF();
+ TX_LED_OFF();
+ RX_LED_OFF();
+
+ /* jump to beginning of application space */
+ __asm__ volatile("jmp 0x0000");
+
+}
+
+uint16_t LLEDPulse;
+
+/** Main program entry point. This routine configures the hardware required by the bootloader, then continuously
+ * runs the bootloader processing routine until it times out or is instructed to exit.
+ */
+int main(void)
+{
+ /* Save the value of the boot key memory before it is overwritten */
+ uint8_t bootKeyPtrVal = *bootKeyPtr;
+ *bootKeyPtr = 0;
+
+ /* Check the reason for the reset so we can act accordingly */
+ uint8_t mcusr_state = MCUSR; // store the initial state of the Status register
+ MCUSR = 0; // clear all reset flags
+
+ /* Watchdog may be configured with a 15 ms period so must disable it before going any further */
+ // MAH 8/15/12- I removed this because wdt_disable() is the first thing SetupHardware() does- why
+ // do it twice right in a row?
+ //wdt_disable();
+
+ /* Setup hardware required for the bootloader */
+ // MAH 8/15/12- Moved this up to before the bootloader go/no-go decision tree so I could use the
+ // timer in that decision tree. Removed the USBInit() call from it; if I'm not going to stay in
+ // the bootloader, there's no point spending the time initializing the USB.
+ // SetupHardware();
+ wdt_disable();
+
+ // Disable clock division
+ clock_prescale_set(clock_div_1);
+
+ // Relocate the interrupt vector table to the bootloader section
+ MCUCR = (1 << IVCE);
+ MCUCR = (1 << IVSEL);
+
+ LED_SETUP();
+ CPU_PRESCALE(0);
+ L_LED_OFF();
+ TX_LED_OFF();
+ RX_LED_OFF();
+
+ // Initialize TIMER1 to handle bootloader timeout and LED tasks.
+ // With 16 MHz clock and 1/64 prescaler, timer 1 is clocked at 250 kHz
+ // Our chosen compare match generates an interrupt every 1 ms.
+ // This interrupt is disabled selectively when doing memory reading, erasing,
+ // or writing since SPM has tight timing requirements.
+
+ OCR1AH = 0;
+ OCR1AL = 250;
+ TIMSK1 = (1 << OCIE1A); // enable timer 1 output compare A match interrupt
+ TCCR1B = ((1 << CS11) | (1 << CS10)); // 1/64 prescaler on timer 1 input
+
+
+ // MAH 8/15/12- this replaces bulky pgm_read_word(0) calls later on, to save memory.
+ if (pgm_read_word(0) != 0xFFFF) sketchPresent = true;
+
+// MAH 26 Oct 2012- The "bootload or not?" section has been modified since the code released
+// with Arduino 1.0.1. The simplest modification is the replacement of equivalence checks on
+// the reset bits with masked checks, so if more than one reset occurs before the register is
+// checked, the check doesn't fail and fall through to the bootloader unnecessarily.
+
+// The second, more in depth modification addresses behavior after an external reset (i.e.,
+// user pushes the reset button). The Leonardo treats all external resets as requests to
+// re-enter the bootloader and wait for code to be loaded. It remains in bootloader mode for
+// 8 seconds before continuing on to the sketch (if one is present). By defining RESET_DELAY
+// equal to 1, this behavior will persist.
+
+// However, if RESET_DELAY is defined to 0, the reset timeout before loading the sketch drops
+// to 750ms. If, during that 750ms, another external reset occurs, THEN an 8-second delay
+// in the bootloader will occur.
+
+ // This is the "no-8-second-delay" code. If this is the first time through the loop, we
+ // don't expect to see the bootKey in memory.
+ if ( (mcusr_state & (1<<EXTRF)) && (bootKeyPtrVal != bootKey) ) {
+ *bootKeyPtr = bootKey; // Put the bootKey in memory so if we get back to this
+ // point again, we know to jump into the bootloader
+ sei(); // Enable interrupts, so we can use timer1 to track our time in the bootloader
+ while (RunBootloader)
+ {
+ if (resetTimeout > EXT_RESET_TIMEOUT_PERIOD) // resetTimeout is getting incremeted
+ RunBootloader = false; // in the timer1 ISR.
+ }
+ // If we make it past that while loop, it's sketch loading time!
+ *bootKeyPtr = 0; // clear out the bootKey; from now on, we want to treat a reset like
+ // a normal reset.
+ cli(); // Disable interrupts, in case no sketch is present.
+ RunBootloader = true; // We want to hang out in the bootloader if no sketch is present.
+ if (sketchPresent) StartSketch(); // If a sketch is present, go! Otherwise, wait around
+ // in the bootloader until one is uploaded.
+ }
+ // On a power-on reset, we ALWAYS want to go to the sketch. If there is one.
+ // This is a place where the old code had an equivalence and now there is a mask.
+ else if ( (mcusr_state & (1<<PORF)) && sketchPresent) {
+ StartSketch();
+ }
+ // On a watchdog reset, if the bootKey isn't set, and there's a sketch, we should just
+ // go straight to the sketch.
+ // This is a place where the old code had an equivalence and now there is a mask.
+ else if ( (mcusr_state & (1<<WDRF) ) && (bootKeyPtrVal != bootKey) && sketchPresent) {
+ // If it looks like an "accidental" watchdog reset then start the sketch.
+ StartSketch();
+ }
+
+ /* Initialize USB Subsystem */
+ USB_Init();
+
+ /* Enable global interrupts so that the USB stack can function */
+ sei();
+
+ Timeout = 0;
+
+ while (RunBootloader)
+ {
+ CDC_Task();
+ USB_USBTask();
+ /* Time out and start the sketch if one is present */
+ if (Timeout > TIMEOUT_PERIOD)
+ RunBootloader = false;
+
+ // MAH 8/15/12- This used to be a function call- inlining it saves a few bytes.
+ LLEDPulse++;
+ uint8_t p = LLEDPulse >> 8;
+ if (p > 127)
+ p = 254-p;
+ p += p;
+ if (((uint8_t)LLEDPulse) > p)
+ L_LED_OFF();
+ else
+ L_LED_ON();
+ }
+
+ /* Disconnect from the host - USB interface will be reset later along with the AVR */
+ USB_Detach();
+
+ /* Jump to beginning of application space to run the sketch - do not reset */
+ StartSketch();
+}
+
+// Timer1 is set up to provide periodic interrupts. This is used to flicker the LEDs during
+// programming as well as to generate the clock counts which determine how long the board should
+// remain in bootloading mode.
+
+ISR(TIMER1_COMPA_vect, ISR_BLOCK)
+{
+ /* Reset counter */
+ TCNT1H = 0;
+ TCNT1L = 0;
+
+ /* Check whether the TX or RX LED one-shot period has elapsed. if so, turn off the LED */
+ if (TxLEDPulse && !(--TxLEDPulse))
+ TX_LED_OFF();
+ if (RxLEDPulse && !(--RxLEDPulse))
+ RX_LED_OFF();
+ resetTimeout++; // Needed for the "short reset delay" mode- governs the time the board waits
+ // for a second reset before loading the sketch.
+ if (pgm_read_word(0) != 0xFFFF)
+ Timeout++;
+}
+
+// MAH 29 Oct 2012 Nothing below this point has to change for the LilyPadUSB support
+
+/** Event handler for the USB_ConfigurationChanged event. This configures the device's endpoints ready
+ * to relay data to and from the attached USB host.
+ */
+void EVENT_USB_Device_ConfigurationChanged(void)
+{
+ /* Setup CDC Notification, Rx and Tx Endpoints */
+ Endpoint_ConfigureEndpoint(CDC_NOTIFICATION_EPNUM, EP_TYPE_INTERRUPT,
+ ENDPOINT_DIR_IN, CDC_NOTIFICATION_EPSIZE,
+ ENDPOINT_BANK_SINGLE);
+
+ Endpoint_ConfigureEndpoint(CDC_TX_EPNUM, EP_TYPE_BULK,
+ ENDPOINT_DIR_IN, CDC_TXRX_EPSIZE,
+ ENDPOINT_BANK_SINGLE);
+
+ Endpoint_ConfigureEndpoint(CDC_RX_EPNUM, EP_TYPE_BULK,
+ ENDPOINT_DIR_OUT, CDC_TXRX_EPSIZE,
+ ENDPOINT_BANK_SINGLE);
+}
+
+/** Event handler for the USB_ControlRequest event. This is used to catch and process control requests sent to
+ * the device from the USB host before passing along unhandled control requests to the library for processing
+ * internally.
+ */
+void EVENT_USB_Device_ControlRequest(void)
+{
+ /* Ignore any requests that aren't directed to the CDC interface */
+ if ((USB_ControlRequest.bmRequestType & (CONTROL_REQTYPE_TYPE | CONTROL_REQTYPE_RECIPIENT)) !=
+ (REQTYPE_CLASS | REQREC_INTERFACE))
+ {
+ return;
+ }
+
+ /* Process CDC specific control requests */
+ switch (USB_ControlRequest.bRequest)
+ {
+ case CDC_REQ_GetLineEncoding:
+ if (USB_ControlRequest.bmRequestType == (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE))
+ {
+ Endpoint_ClearSETUP();
+
+ /* Write the line coding data to the control endpoint */
+ Endpoint_Write_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
+ Endpoint_ClearOUT();
+ }
+
+ break;
+ case CDC_REQ_SetLineEncoding:
+ if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE))
+ {
+ Endpoint_ClearSETUP();
+
+ /* Read the line coding data in from the host into the global struct */
+ Endpoint_Read_Control_Stream_LE(&LineEncoding, sizeof(CDC_LineEncoding_t));
+ Endpoint_ClearIN();
+ }
+
+ break;
+ }
+}
+
+#if !defined(NO_BLOCK_SUPPORT)
+/** Reads or writes a block of EEPROM or FLASH memory to or from the appropriate CDC data endpoint, depending
+ * on the AVR910 protocol command issued.
+ *
+ * \param[in] Command Single character AVR910 protocol command indicating what memory operation to perform
+ */
+static void ReadWriteMemoryBlock(const uint8_t Command)
+{
+ uint16_t BlockSize;
+ char MemoryType;
+
+ bool HighByte = false;
+ uint8_t LowByte = 0;
+
+ BlockSize = (FetchNextCommandByte() << 8);
+ BlockSize |= FetchNextCommandByte();
+
+ MemoryType = FetchNextCommandByte();
+
+ if ((MemoryType != 'E') && (MemoryType != 'F'))
+ {
+ /* Send error byte back to the host */
+ WriteNextResponseByte('?');
+
+ return;
+ }
+
+ /* Disable timer 1 interrupt - can't afford to process nonessential interrupts
+ * while doing SPM tasks */
+ TIMSK1 = 0;
+
+ /* Check if command is to read memory */
+ if (Command == 'g')
+ {
+ /* Re-enable RWW section */
+ boot_rww_enable();
+
+ while (BlockSize--)
+ {
+ if (MemoryType == 'F')
+ {
+ /* Read the next FLASH byte from the current FLASH page */
+ #if (FLASHEND > 0xFFFF)
+ WriteNextResponseByte(pgm_read_byte_far(CurrAddress | HighByte));
+ #else
+ WriteNextResponseByte(pgm_read_byte(CurrAddress | HighByte));
+ #endif
+
+ /* If both bytes in current word have been read, increment the address counter */
+ if (HighByte)
+ CurrAddress += 2;
+
+ HighByte = !HighByte;
+ }
+ else
+ {
+ /* Read the next EEPROM byte into the endpoint */
+ WriteNextResponseByte(eeprom_read_byte((uint8_t*)(intptr_t)(CurrAddress >> 1)));
+
+ /* Increment the address counter after use */
+ CurrAddress += 2;
+ }
+ }
+ }
+ else
+ {
+ uint32_t PageStartAddress = CurrAddress;
+
+ if (MemoryType == 'F')
+ {
+ boot_page_erase(PageStartAddress);
+ boot_spm_busy_wait();
+ }
+
+ while (BlockSize--)
+ {
+ if (MemoryType == 'F')
+ {
+ /* If both bytes in current word have been written, increment the address counter */
+ if (HighByte)
+ {
+ /* Write the next FLASH word to the current FLASH page */
+ boot_page_fill(CurrAddress, ((FetchNextCommandByte() << 8) | LowByte));
+
+ /* Increment the address counter after use */
+ CurrAddress += 2;
+ }
+ else
+ {
+ LowByte = FetchNextCommandByte();
+ }
+
+ HighByte = !HighByte;
+ }
+ else
+ {
+ /* Write the next EEPROM byte from the endpoint */
+ eeprom_write_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte());
+
+ /* Increment the address counter after use */
+ CurrAddress += 2;
+ }
+ }
+
+ /* If in FLASH programming mode, commit the page after writing */
+ if (MemoryType == 'F')
+ {
+ /* Commit the flash page to memory */
+ boot_page_write(PageStartAddress);
+
+ /* Wait until write operation has completed */
+ boot_spm_busy_wait();
+ }
+
+ /* Send response byte back to the host */
+ WriteNextResponseByte('\r');
+ }
+
+ /* Re-enable timer 1 interrupt disabled earlier in this routine */
+ TIMSK1 = (1 << OCIE1A);
+}
+#endif
+
+/** Retrieves the next byte from the host in the CDC data OUT endpoint, and clears the endpoint bank if needed
+ * to allow reception of the next data packet from the host.
+ *
+ * \return Next received byte from the host in the CDC data OUT endpoint
+ */
+static uint8_t FetchNextCommandByte(void)
+{
+ /* Select the OUT endpoint so that the next data byte can be read */
+ Endpoint_SelectEndpoint(CDC_RX_EPNUM);
+
+ /* If OUT endpoint empty, clear it and wait for the next packet from the host */
+ while (!(Endpoint_IsReadWriteAllowed()))
+ {
+ Endpoint_ClearOUT();
+
+ while (!(Endpoint_IsOUTReceived()))
+ {
+ if (USB_DeviceState == DEVICE_STATE_Unattached)
+ return 0;
+ }
+ }
+
+ /* Fetch the next byte from the OUT endpoint */
+ return Endpoint_Read_8();
+}
+
+/** Writes the next response byte to the CDC data IN endpoint, and sends the endpoint back if needed to free up the
+ * bank when full ready for the next byte in the packet to the host.
+ *
+ * \param[in] Response Next response byte to send to the host
+ */
+static void WriteNextResponseByte(const uint8_t Response)
+{
+ /* Select the IN endpoint so that the next data byte can be written */
+ Endpoint_SelectEndpoint(CDC_TX_EPNUM);
+
+ /* If IN endpoint full, clear it and wait until ready for the next packet to the host */
+ if (!(Endpoint_IsReadWriteAllowed()))
+ {
+ Endpoint_ClearIN();
+
+ while (!(Endpoint_IsINReady()))
+ {
+ if (USB_DeviceState == DEVICE_STATE_Unattached)
+ return;
+ }
+ }
+
+ /* Write the next byte to the IN endpoint */
+ Endpoint_Write_8(Response);
+
+ TX_LED_ON();
+ TxLEDPulse = TX_RX_LED_PULSE_PERIOD;
+}
+
+#define STK_OK 0x10
+#define STK_INSYNC 0x14 // ' '
+#define CRC_EOP 0x20 // 'SPACE'
+#define STK_GET_SYNC 0x30 // '0'
+
+#define STK_GET_PARAMETER 0x41 // 'A'
+#define STK_SET_DEVICE 0x42 // 'B'
+#define STK_SET_DEVICE_EXT 0x45 // 'E'
+#define STK_LOAD_ADDRESS 0x55 // 'U'
+#define STK_UNIVERSAL 0x56 // 'V'
+#define STK_PROG_PAGE 0x64 // 'd'
+#define STK_READ_PAGE 0x74 // 't'
+#define STK_READ_SIGN 0x75 // 'u'
+
+/** Task to read in AVR910 commands from the CDC data OUT endpoint, process them, perform the required actions
+ * and send the appropriate response back to the host.
+ */
+void CDC_Task(void)
+{
+ /* Select the OUT endpoint */
+ Endpoint_SelectEndpoint(CDC_RX_EPNUM);
+
+ /* Check if endpoint has a command in it sent from the host */
+ if (!(Endpoint_IsOUTReceived()))
+ return;
+
+ RX_LED_ON();
+ RxLEDPulse = TX_RX_LED_PULSE_PERIOD;
+
+ /* Read in the bootloader command (first byte sent from host) */
+ uint8_t Command = FetchNextCommandByte();
+
+ if (Command == 'E')
+ {
+ /* We nearly run out the bootloader timeout clock,
+ * leaving just a few hundred milliseconds so the
+ * bootloder has time to respond and service any
+ * subsequent requests */
+ Timeout = TIMEOUT_PERIOD - 500;
+
+ /* Re-enable RWW section - must be done here in case
+ * user has disabled verification on upload. */
+ boot_rww_enable_safe();
+
+ // Send confirmation byte back to the host
+ WriteNextResponseByte('\r');
+ }
+ else if (Command == 'T')
+ {
+ FetchNextCommandByte();
+
+ // Send confirmation byte back to the host
+ WriteNextResponseByte('\r');
+ }
+ else if ((Command == 'L') || (Command == 'P'))
+ {
+ // Send confirmation byte back to the host
+ WriteNextResponseByte('\r');
+ }
+ else if (Command == 't')
+ {
+ // Return ATMEGA128 part code - this is only to allow AVRProg to use the bootloader
+ WriteNextResponseByte(0x44);
+ WriteNextResponseByte(0x00);
+ }
+ else if (Command == 'a')
+ {
+ // Indicate auto-address increment is supported
+ WriteNextResponseByte('Y');
+ }
+ else if (Command == 'A')
+ {
+ // Set the current address to that given by the host
+ CurrAddress = (FetchNextCommandByte() << 9);
+ CurrAddress |= (FetchNextCommandByte() << 1);
+
+ // Send confirmation byte back to the host
+ WriteNextResponseByte('\r');
+ }
+ else if (Command == 'p')
+ {
+ // Indicate serial programmer back to the host
+ WriteNextResponseByte('S');
+ }
+ else if (Command == 'S')
+ {
+ // Write the 7-byte software identifier to the endpoint
+ for (uint8_t CurrByte = 0; CurrByte < 7; CurrByte++)
+ WriteNextResponseByte(SOFTWARE_IDENTIFIER[CurrByte]);
+ }
+ else if (Command == 'V')
+ {
+ WriteNextResponseByte('0' + BOOTLOADER_VERSION_MAJOR);
+ WriteNextResponseByte('0' + BOOTLOADER_VERSION_MINOR);
+ }
+ else if (Command == 's')
+ {
+ WriteNextResponseByte(AVR_SIGNATURE_3);
+ WriteNextResponseByte(AVR_SIGNATURE_2);
+ WriteNextResponseByte(AVR_SIGNATURE_1);
+ }
+ else if (Command == 'e')
+ {
+ // Clear the application section of flash
+ for (uint32_t CurrFlashAddress = 0; CurrFlashAddress < BOOT_START_ADDR; CurrFlashAddress += SPM_PAGESIZE)
+ {
+ boot_page_erase(CurrFlashAddress);
+ boot_spm_busy_wait();
+ boot_page_write(CurrFlashAddress);
+ boot_spm_busy_wait();
+ }
+
+ // Send confirmation byte back to the host
+ WriteNextResponseByte('\r');
+ }
+ #if !defined(NO_LOCK_BYTE_WRITE_SUPPORT)
+ else if (Command == 'l')
+ {
+ // Set the lock bits to those given by the host
+ boot_lock_bits_set(FetchNextCommandByte());
+
+ // Send confirmation byte back to the host
+ WriteNextResponseByte('\r');
+ }
+ #endif
+ else if (Command == 'r')
+ {
+ WriteNextResponseByte(boot_lock_fuse_bits_get(GET_LOCK_BITS));
+ }
+ else if (Command == 'F')
+ {
+ WriteNextResponseByte(boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS));
+ }
+ else if (Command == 'N')
+ {
+ WriteNextResponseByte(boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS));
+ }
+ else if (Command == 'Q')
+ {
+ WriteNextResponseByte(boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS));
+ }
+ #if !defined(NO_BLOCK_SUPPORT)
+ else if (Command == 'b')
+ {
+ WriteNextResponseByte('Y');
+
+ // Send block size to the host
+ WriteNextResponseByte(SPM_PAGESIZE >> 8);
+ WriteNextResponseByte(SPM_PAGESIZE & 0xFF);
+ }
+ else if ((Command == 'B') || (Command == 'g'))
+ {
+ // Keep resetting the timeout counter if we're receiving self-programming instructions
+ Timeout = 0;
+ // Delegate the block write/read to a separate function for clarity
+ ReadWriteMemoryBlock(Command);
+ }
+ #endif
+ #if !defined(NO_FLASH_BYTE_SUPPORT)
+ else if (Command == 'C')
+ {
+ // Write the high byte to the current flash page
+ boot_page_fill(CurrAddress, FetchNextCommandByte());
+
+ // Send confirmation byte back to the host
+ WriteNextResponseByte('\r');
+ }
+ else if (Command == 'c')
+ {
+ // Write the low byte to the current flash page
+ boot_page_fill(CurrAddress | 0x01, FetchNextCommandByte());
+
+ // Increment the address
+ CurrAddress += 2;
+
+ // Send confirmation byte back to the host
+ WriteNextResponseByte('\r');
+ }
+ else if (Command == 'm')
+ {
+ // Commit the flash page to memory
+ boot_page_write(CurrAddress);
+
+ // Wait until write operation has completed
+ boot_spm_busy_wait();
+
+ // Send confirmation byte back to the host
+ WriteNextResponseByte('\r');
+ }
+ else if (Command == 'R')
+ {
+ #if (FLASHEND > 0xFFFF)
+ uint16_t ProgramWord = pgm_read_word_far(CurrAddress);
+ #else
+ uint16_t ProgramWord = pgm_read_word(CurrAddress);
+ #endif
+
+ WriteNextResponseByte(ProgramWord >> 8);
+ WriteNextResponseByte(ProgramWord & 0xFF);
+ }
+ #endif
+ #if !defined(NO_EEPROM_BYTE_SUPPORT)
+ else if (Command == 'D')
+ {
+ // Read the byte from the endpoint and write it to the EEPROM
+ eeprom_write_byte((uint8_t*)((intptr_t)(CurrAddress >> 1)), FetchNextCommandByte());
+
+ // Increment the address after use
+ CurrAddress += 2;
+
+ // Send confirmation byte back to the host
+ WriteNextResponseByte('\r');
+ }
+ else if (Command == 'd')
+ {
+ // Read the EEPROM byte and write it to the endpoint
+ WriteNextResponseByte(eeprom_read_byte((uint8_t*)((intptr_t)(CurrAddress >> 1))));
+
+ // Increment the address after use
+ CurrAddress += 2;
+ }
+ #endif
+ else if (Command != 27)
+ {
+ // Unknown (non-sync) command, return fail code
+ WriteNextResponseByte('?');
+ }
+
+
+ /* Select the IN endpoint */
+ Endpoint_SelectEndpoint(CDC_TX_EPNUM);
+
+ /* Remember if the endpoint is completely full before clearing it */
+ bool IsEndpointFull = !(Endpoint_IsReadWriteAllowed());
+
+ /* Send the endpoint data to the host */
+ Endpoint_ClearIN();
+
+ /* If a full endpoint's worth of data was sent, we need to send an empty packet afterwards to signal end of transfer */
+ if (IsEndpointFull)
+ {
+ while (!(Endpoint_IsINReady()))
+ {
+ if (USB_DeviceState == DEVICE_STATE_Unattached)
+ return;
+ }
+
+ Endpoint_ClearIN();
+ }
+
+ /* Wait until the data has been sent to the host */
+ while (!(Endpoint_IsINReady()))
+ {
+ if (USB_DeviceState == DEVICE_STATE_Unattached)
+ return;
+ }
+
+ /* Select the OUT endpoint */
+ Endpoint_SelectEndpoint(CDC_RX_EPNUM);
+
+ /* Acknowledge the command from the host */
+ Endpoint_ClearOUT();
+}
+
diff --git a/bootloaders/caterina-Arduino_Robot/Caterina.h b/bootloaders/caterina-Arduino_Robot/Caterina.h
new file mode 100644
index 0000000..5ce80fa
--- /dev/null
+++ b/bootloaders/caterina-Arduino_Robot/Caterina.h
@@ -0,0 +1,106 @@
+/*
+ LUFA Library
+ Copyright (C) Dean Camera, 2011.
+
+ dean [at] fourwalledcubicle [dot] com
+ www.lufa-lib.org
+*/
+
+/*
+ Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
+
+ Permission to use, copy, modify, distribute, and sell this
+ software and its documentation for any purpose is hereby granted
+ without fee, provided that the above copyright notice appear in
+ all copies and that both that the copyright notice and this
+ permission notice and warranty disclaimer appear in supporting
+ documentation, and that the name of the author not be used in
+ advertising or publicity pertaining to distribution of the
+ software without specific, written prior permission.
+
+ The author disclaim all warranties with regard to this
+ software, including all implied warranties of merchantability
+ and fitness. In no event shall the author be liable for any
+ special, indirect or consequential damages or any damages
+ whatsoever resulting from loss of use, data or profits, whether
+ in an action of contract, negligence or other tortious action,
+ arising out of or in connection with the use or performance of
+ this software.
+*/
+
+/** \file
+ *
+ * Header file for BootloaderCDC.c.
+ */
+
+#ifndef _CDC_H_
+#define _CDC_H_
+
+ /* Includes: */
+ #include <avr/io.h>
+ #include <avr/wdt.h>
+ #include <avr/boot.h>
+ #include <avr/eeprom.h>
+ #include <avr/power.h>
+ #include <avr/interrupt.h>
+ #include <stdbool.h>
+
+ #include "Descriptors.h"
+
+ #include <LUFA/Drivers/USB/USB.h>
+ /* Macros: */
+ /** Version major of the CDC bootloader. */
+ #define BOOTLOADER_VERSION_MAJOR 0x01
+
+ /** Version minor of the CDC bootloader. */
+ #define BOOTLOADER_VERSION_MINOR 0x00
+
+ /** Hardware version major of the CDC bootloader. */
+ #define BOOTLOADER_HWVERSION_MAJOR 0x01
+
+ /** Hardware version minor of the CDC bootloader. */
+ #define BOOTLOADER_HWVERSION_MINOR 0x00
+
+ /** Eight character bootloader firmware identifier reported to the host when requested */
+ #define SOFTWARE_IDENTIFIER "CATERINA"
+
+ #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
+ #define LED_SETUP() DDRC |= (1<<7); DDRB |= (1<<0); DDRD |= (1<<5);
+ #define L_LED_OFF() PORTC &= ~(1<<7)
+ #define L_LED_ON() PORTC |= (1<<7)
+ #define L_LED_TOGGLE() PORTC ^= (1<<7)
+ #if DEVICE_PID == 0x0037 // polarity of the RX and TX LEDs is reversed on the Micro
+ #define TX_LED_OFF() PORTD &= ~(1<<5)
+ #define TX_LED_ON() PORTD |= (1<<5)
+ #define RX_LED_OFF() PORTB &= ~(1<<0)
+ #define RX_LED_ON() PORTB |= (1<<0)
+ #else
+ #define TX_LED_OFF() PORTD |= (1<<5)
+ #define TX_LED_ON() PORTD &= ~(1<<5)
+ #define RX_LED_OFF() PORTB |= (1<<0)
+ #define RX_LED_ON() PORTB &= ~(1<<0)
+ #endif
+
+ /* Type Defines: */
+ /** Type define for a non-returning pointer to the start of the loaded application in flash memory. */
+ typedef void (*AppPtr_t)(void) ATTR_NO_RETURN;
+
+ /* Function Prototypes: */
+ void StartSketch(void);
+ void LEDPulse(void);
+
+ void CDC_Task(void);
+ void SetupHardware(void);
+
+ void EVENT_USB_Device_ConfigurationChanged(void);
+
+ #if defined(INCLUDE_FROM_CATERINA_C) || defined(__DOXYGEN__)
+ #if !defined(NO_BLOCK_SUPPORT)
+ static void ReadWriteMemoryBlock(const uint8_t Command);
+ #endif
+ static uint8_t FetchNextCommandByte(void);
+ static void WriteNextResponseByte(const uint8_t Response);
+ #endif
+
+#endif
+
diff --git a/bootloaders/caterina-Arduino_Robot/Descriptors.c b/bootloaders/caterina-Arduino_Robot/Descriptors.c
new file mode 100644
index 0000000..57f39d9
--- /dev/null
+++ b/bootloaders/caterina-Arduino_Robot/Descriptors.c
@@ -0,0 +1,270 @@
+/*
+ LUFA Library
+ Copyright (C) Dean Camera, 2011.
+
+ dean [at] fourwalledcubicle [dot] com
+ www.lufa-lib.org
+*/
+
+/*
+ Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
+
+ Permission to use, copy, modify, distribute, and sell this
+ software and its documentation for any purpose is hereby granted
+ without fee, provided that the above copyright notice appear in
+ all copies and that both that the copyright notice and this
+ permission notice and warranty disclaimer appear in supporting
+ documentation, and that the name of the author not be used in
+ advertising or publicity pertaining to distribution of the
+ software without specific, written prior permission.
+
+ The author disclaim all warranties with regard to this
+ software, including all implied warranties of merchantability
+ and fitness. In no event shall the author be liable for any
+ special, indirect or consequential damages or any damages
+ whatsoever resulting from loss of use, data or profits, whether
+ in an action of contract, negligence or other tortious action,
+ arising out of or in connection with the use or performance of
+ this software.
+*/
+
+/** \file
+ *
+ * USB Device Descriptors, for library use when in USB device mode. Descriptors are special
+ * computer-readable structures which the host requests upon device enumeration, to determine
+ * the device's capabilities and functions.
+ */
+
+#include "Descriptors.h"
+
+/** Device descriptor structure. This descriptor, located in SRAM memory, describes the overall
+ * device characteristics, including the supported USB version, control endpoint size and the
+ * number of device configurations. The descriptor is read out by the USB host when the enumeration
+ * process begins.
+ */
+const USB_Descriptor_Device_t DeviceDescriptor =
+{
+ .Header = {.Size = sizeof(USB_Descriptor_Device_t), .Type = DTYPE_Device},
+
+ .USBSpecification = VERSION_BCD(01.10),
+ .Class = CDC_CSCP_CDCClass,
+ .SubClass = CDC_CSCP_NoSpecificSubclass,
+ .Protocol = CDC_CSCP_NoSpecificProtocol,
+
+ .Endpoint0Size = FIXED_CONTROL_ENDPOINT_SIZE,
+
+ .VendorID = DEVICE_VID,
+ .ProductID = DEVICE_PID,
+ .ReleaseNumber = VERSION_BCD(00.01),
+
+ .ManufacturerStrIndex = 0x02,
+ .ProductStrIndex = 0x01,
+ .SerialNumStrIndex = NO_DESCRIPTOR,
+
+ .NumberOfConfigurations = FIXED_NUM_CONFIGURATIONS
+};
+
+/** Configuration descriptor structure. This descriptor, located in SRAM memory, describes the usage
+ * of the device in one of its supported configurations, including information about any device interfaces
+ * and endpoints. The descriptor is read out by the USB host during the enumeration process when selecting
+ * a configuration so that the host may correctly communicate with the USB device.
+ */
+const USB_Descriptor_Configuration_t ConfigurationDescriptor =
+{
+ .Config =
+ {
+ .Header = {.Size = sizeof(USB_Descriptor_Configuration_Header_t), .Type = DTYPE_Configuration},
+
+ .TotalConfigurationSize = sizeof(USB_Descriptor_Configuration_t),
+ .TotalInterfaces = 2,
+
+ .ConfigurationNumber = 1,
+ .ConfigurationStrIndex = NO_DESCRIPTOR,
+
+ .ConfigAttributes = USB_CONFIG_ATTR_BUSPOWERED,
+
+ .MaxPowerConsumption = USB_CONFIG_POWER_MA(100)
+ },
+
+ .CDC_CCI_Interface =
+ {
+ .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
+
+ .InterfaceNumber = 0,
+ .AlternateSetting = 0,
+
+ .TotalEndpoints = 1,
+
+ .Class = CDC_CSCP_CDCClass,
+ .SubClass = CDC_CSCP_ACMSubclass,
+ .Protocol = CDC_CSCP_ATCommandProtocol,
+
+ .InterfaceStrIndex = NO_DESCRIPTOR
+ },
+
+ .CDC_Functional_Header =
+ {
+ .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalHeader_t), .Type = DTYPE_CSInterface},
+ .Subtype = 0x00,
+
+ .CDCSpecification = VERSION_BCD(01.10),
+ },
+
+ .CDC_Functional_ACM =
+ {
+ .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalACM_t), .Type = DTYPE_CSInterface},
+ .Subtype = 0x02,
+
+ .Capabilities = 0x04,
+ },
+
+ .CDC_Functional_Union =
+ {
+ .Header = {.Size = sizeof(USB_CDC_Descriptor_FunctionalUnion_t), .Type = DTYPE_CSInterface},
+ .Subtype = 0x06,
+
+ .MasterInterfaceNumber = 0,
+ .SlaveInterfaceNumber = 1,
+ },
+
+ .CDC_NotificationEndpoint =
+ {
+ .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
+
+ .EndpointAddress = (ENDPOINT_DIR_IN | CDC_NOTIFICATION_EPNUM),
+ .Attributes = (EP_TYPE_INTERRUPT | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
+ .EndpointSize = CDC_NOTIFICATION_EPSIZE,
+ .PollingIntervalMS = 0xFF
+ },
+
+ .CDC_DCI_Interface =
+ {
+ .Header = {.Size = sizeof(USB_Descriptor_Interface_t), .Type = DTYPE_Interface},
+
+ .InterfaceNumber = 1,
+ .AlternateSetting = 0,
+
+ .TotalEndpoints = 2,
+
+ .Class = CDC_CSCP_CDCDataClass,
+ .SubClass = CDC_CSCP_NoDataSubclass,
+ .Protocol = CDC_CSCP_NoDataProtocol,
+
+ .InterfaceStrIndex = NO_DESCRIPTOR
+ },
+
+ .CDC_DataOutEndpoint =
+ {
+ .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
+
+ .EndpointAddress = (ENDPOINT_DIR_OUT | CDC_RX_EPNUM),
+ .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
+ .EndpointSize = CDC_TXRX_EPSIZE,
+ .PollingIntervalMS = 0x01
+ },
+
+ .CDC_DataInEndpoint =
+ {
+ .Header = {.Size = sizeof(USB_Descriptor_Endpoint_t), .Type = DTYPE_Endpoint},
+
+ .EndpointAddress = (ENDPOINT_DIR_IN | CDC_TX_EPNUM),
+ .Attributes = (EP_TYPE_BULK | ENDPOINT_ATTR_NO_SYNC | ENDPOINT_USAGE_DATA),
+ .EndpointSize = CDC_TXRX_EPSIZE,
+ .PollingIntervalMS = 0x01
+ }
+};
+
+/** Language descriptor structure. This descriptor, located in SRAM memory, is returned when the host requests
+ * the string descriptor with index 0 (the first index). It is actually an array of 16-bit integers, which indicate
+ * via the language ID table available at USB.org what languages the device supports for its string descriptors.
+ */
+const USB_Descriptor_String_t LanguageString =
+{
+ .Header = {.Size = USB_STRING_LEN(1), .Type = DTYPE_String},
+
+ .UnicodeString = {LANGUAGE_ID_ENG}
+};
+
+/** Product descriptor string. This is a Unicode string containing the product's details in human readable form,
+ * and is read out upon request by the host when the appropriate string ID is requested, listed in the Device
+ * Descriptor.
+ */
+const USB_Descriptor_String_t ProductString =
+{
+ .Header = {.Size = USB_STRING_LEN(19), .Type = DTYPE_String},
+
+ #if DEVICE_PID == 0x0036
+ .UnicodeString = L"Arduino Leonardo"
+ #elif DEVICE_PID == 0x0037
+ .UnicodeString = L"Arduino Micro "
+ #elif DEVICE_PID == 0x0038
+ .UnicodeString = L"Robot Control Board"
+ #elif DEVICE_PID == 0x0039
+ .UnicodeString = L"Robot Motor Board "
+ #elif DEVICE_PID == 0x003C
+ .UnicodeString = L"Arduino Esplora "
+ #else
+ .UnicodeString = L"USB IO board "
+ #endif
+};
+
+const USB_Descriptor_String_t ManufacturerString =
+{
+ .Header = {.Size = USB_STRING_LEN(11), .Type = DTYPE_String},
+
+ #if DEVICE_VID == 0x2341
+ .UnicodeString = L"Arduino LLC"
+ #else
+ .UnicodeString = L"Unknown "
+ #endif
+};
+
+/** This function is called by the library when in device mode, and must be overridden (see LUFA library "USB Descriptors"
+ * documentation) by the application code so that the address and size of a requested descriptor can be given
+ * to the USB library. When the device receives a Get Descriptor request on the control endpoint, this function
+ * is called so that the descriptor details can be passed back and the appropriate descriptor sent back to the
+ * USB host.
+ */
+uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
+ const uint8_t wIndex,
+ const void** const DescriptorAddress)
+{
+ const uint8_t DescriptorType = (wValue >> 8);
+ const uint8_t DescriptorNumber = (wValue & 0xFF);
+
+ const void* Address = NULL;
+ uint16_t Size = NO_DESCRIPTOR;
+
+ switch (DescriptorType)
+ {
+ case DTYPE_Device:
+ Address = &DeviceDescriptor;
+ Size = sizeof(USB_Descriptor_Device_t);
+ break;
+ case DTYPE_Configuration:
+ Address = &ConfigurationDescriptor;
+ Size = sizeof(USB_Descriptor_Configuration_t);
+ break;
+ case DTYPE_String:
+ if (!(DescriptorNumber))
+ {
+ Address = &LanguageString;
+ Size = LanguageString.Header.Size;
+ }
+ else if (DescriptorNumber == DeviceDescriptor.ProductStrIndex)
+ {
+ Address = &ProductString;
+ Size = ProductString.Header.Size;
+ } else if (DescriptorNumber == DeviceDescriptor.ManufacturerStrIndex)
+ {
+ Address = &ManufacturerString;
+ Size = ManufacturerString.Header.Size;
+ }
+
+ break;
+ }
+
+ *DescriptorAddress = Address;
+ return Size;
+}
+
diff --git a/bootloaders/caterina-Arduino_Robot/Descriptors.h b/bootloaders/caterina-Arduino_Robot/Descriptors.h
new file mode 100644
index 0000000..94091ae
--- /dev/null
+++ b/bootloaders/caterina-Arduino_Robot/Descriptors.h
@@ -0,0 +1,139 @@
+/*
+ LUFA Library
+ Copyright (C) Dean Camera, 2011.
+
+ dean [at] fourwalledcubicle [dot] com
+ www.lufa-lib.org
+*/
+
+/*
+ Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
+
+ Permission to use, copy, modify, distribute, and sell this
+ software and its documentation for any purpose is hereby granted
+ without fee, provided that the above copyright notice appear in
+ all copies and that both that the copyright notice and this
+ permission notice and warranty disclaimer appear in supporting
+ documentation, and that the name of the author not be used in
+ advertising or publicity pertaining to distribution of the
+ software without specific, written prior permission.
+
+ The author disclaim all warranties with regard to this
+ software, including all implied warranties of merchantability
+ and fitness. In no event shall the author be liable for any
+ special, indirect or consequential damages or any damages
+ whatsoever resulting from loss of use, data or profits, whether
+ in an action of contract, negligence or other tortious action,
+ arising out of or in connection with the use or performance of
+ this software.
+*/
+
+/** \file
+ *
+ * Header file for Descriptors.c.
+ */
+
+#ifndef _DESCRIPTORS_H_
+#define _DESCRIPTORS_H_
+
+ /* Includes: */
+ #include <LUFA/Drivers/USB/USB.h>
+
+ /* Macros: */
+ #if defined(__AVR_AT90USB1287__)
+ #define AVR_SIGNATURE_1 0x1E
+ #define AVR_SIGNATURE_2 0x97
+ #define AVR_SIGNATURE_3 0x82
+ #elif defined(__AVR_AT90USB647__)
+ #define AVR_SIGNATURE_1 0x1E
+ #define AVR_SIGNATURE_2 0x96
+ #define AVR_SIGNATURE_3 0x82
+ #elif defined(__AVR_AT90USB1286__)
+ #define AVR_SIGNATURE_1 0x1E
+ #define AVR_SIGNATURE_2 0x97
+ #define AVR_SIGNATURE_3 0x82
+ #elif defined(__AVR_AT90USB646__)
+ #define AVR_SIGNATURE_1 0x1E
+ #define AVR_SIGNATURE_2 0x96
+ #define AVR_SIGNATURE_3 0x82
+ #elif defined(__AVR_ATmega32U6__)
+ #define AVR_SIGNATURE_1 0x1E
+ #define AVR_SIGNATURE_2 0x95
+ #define AVR_SIGNATURE_3 0x88
+ #elif defined(__AVR_ATmega32U4__)
+ #define AVR_SIGNATURE_1 0x1E
+ #define AVR_SIGNATURE_2 0x95
+ #define AVR_SIGNATURE_3 0x87
+ #elif defined(__AVR_ATmega16U4__)
+ #define AVR_SIGNATURE_1 0x1E
+ #define AVR_SIGNATURE_2 0x94
+ #define AVR_SIGNATURE_3 0x88
+ #elif defined(__AVR_ATmega32U2__)
+ #define AVR_SIGNATURE_1 0x1E
+ #define AVR_SIGNATURE_2 0x95
+ #define AVR_SIGNATURE_3 0x8A
+ #elif defined(__AVR_ATmega16U2__)
+ #define AVR_SIGNATURE_1 0x1E
+ #define AVR_SIGNATURE_2 0x94
+ #define AVR_SIGNATURE_3 0x89
+ #elif defined(__AVR_AT90USB162__)
+ #define AVR_SIGNATURE_1 0x1E
+ #define AVR_SIGNATURE_2 0x94
+ #define AVR_SIGNATURE_3 0x82
+ #elif defined(__AVR_ATmega8U2__)
+ #define AVR_SIGNATURE_1 0x1E
+ #define AVR_SIGNATURE_2 0x93
+ #define AVR_SIGNATURE_3 0x89
+ #elif defined(__AVR_AT90USB82__)
+ #define AVR_SIGNATURE_1 0x1E
+ #define AVR_SIGNATURE_2 0x94
+ #define AVR_SIGNATURE_3 0x82
+ #else
+ #error The selected AVR part is not currently supported by this bootloader.
+ #endif
+
+ /** Endpoint number for the CDC control interface event notification endpoint. */
+ #define CDC_NOTIFICATION_EPNUM 2
+
+ /** Endpoint number for the CDC data interface TX (data IN) endpoint. */
+ #define CDC_TX_EPNUM 3
+
+ /** Endpoint number for the CDC data interface RX (data OUT) endpoint. */
+ #define CDC_RX_EPNUM 4
+
+ /** Size of the CDC data interface TX and RX data endpoint banks, in bytes. */
+ #define CDC_TXRX_EPSIZE 16
+
+ /** Size of the CDC control interface notification endpoint bank, in bytes. */
+ #define CDC_NOTIFICATION_EPSIZE 8
+
+ /* Type Defines: */
+ /** Type define for the device configuration descriptor structure. This must be defined in the
+ * application code, as the configuration descriptor contains several sub-descriptors which
+ * vary between devices, and which describe the device's usage to the host.
+ */
+ typedef struct
+ {
+ USB_Descriptor_Configuration_Header_t Config;
+
+ // CDC Control Interface
+ USB_Descriptor_Interface_t CDC_CCI_Interface;
+ USB_CDC_Descriptor_FunctionalHeader_t CDC_Functional_Header;
+ USB_CDC_Descriptor_FunctionalACM_t CDC_Functional_ACM;
+ USB_CDC_Descriptor_FunctionalUnion_t CDC_Functional_Union;
+ USB_Descriptor_Endpoint_t CDC_NotificationEndpoint;
+
+ // CDC Data Interface
+ USB_Descriptor_Interface_t CDC_DCI_Interface;
+ USB_Descriptor_Endpoint_t CDC_DataOutEndpoint;
+ USB_Descriptor_Endpoint_t CDC_DataInEndpoint;
+ } USB_Descriptor_Configuration_t;
+
+ /* Function Prototypes: */
+ uint16_t CALLBACK_USB_GetDescriptor(const uint16_t wValue,
+ const uint8_t wIndex,
+ const void** const DescriptorAddress)
+ ATTR_WARN_UNUSED_RESULT ATTR_NON_NULL_PTR_ARG(3);
+
+#endif
+
diff --git a/bootloaders/caterina-Arduino_Robot/Makefile b/bootloaders/caterina-Arduino_Robot/Makefile
new file mode 100644
index 0000000..af9990e
--- /dev/null
+++ b/bootloaders/caterina-Arduino_Robot/Makefile
@@ -0,0 +1,738 @@
+# Hey Emacs, this is a -*- makefile -*-
+#----------------------------------------------------------------------------
+# WinAVR Makefile Template written by Eric B. Weddington, Jörg Wunsch, et al.
+# >> Modified for use with the LUFA project. <<
+#
+# Released to the Public Domain
+#
+# Additional material for this makefile was written by:
+# Peter Fleury
+# Tim Henigan
+# Colin O'Flynn
+# Reiner Patommel
+# Markus Pfaff
+# Sander Pool
+# Frederik Rouleau
+# Carlos Lamas
+# Dean Camera
+# Opendous Inc.
+# Denver Gingerich
+#
+#----------------------------------------------------------------------------
+# On command line:
+#
+# make all = Make software.
+#
+# make clean = Clean out built project files.
+#
+# make coff = Convert ELF to AVR COFF.
+#
+# make extcoff = Convert ELF to AVR Extended COFF.
+#
+# make program = Download the hex file to the device, using avrdude.
+# Please customize the avrdude settings below first!
+#
+# make doxygen = Generate DoxyGen documentation for the project (must have
+# DoxyGen installed)
+#
+# make debug = Start either simulavr or avarice as specified for debugging,
+# with avr-gdb or avr-insight as the front end for debugging.
+#
+# make filename.s = Just compile filename.c into the assembler code only.
+#
+# make filename.i = Create a preprocessed source file for use in submitting
+# bug reports to the GCC project.
+#
+# To rebuild project do "make clean" then "make all".
+#----------------------------------------------------------------------------
+
+# USB vendor ID (VID)
+# reuse of this VID by others is forbidden by USB-IF
+# official Arduino LLC VID
+VID = 0x2341
+
+
+# USB product ID (PID)
+# official Leonardo PID
+# PID = 0x0036
+# official Micro PID
+# PID = 0x0037
+# official Arduino Robot Control Board PID
+PID = 0x0038
+# official Arduino Robot Motor Board PID
+# PID = 0x0039
+# official Esplora PID
+# PID = 0x003C
+
+# MCU name
+MCU = atmega32u4
+
+
+# Target architecture (see library "Board Types" documentation).
+ARCH = AVR8
+
+
+# Target board (see library "Board Types" documentation, NONE for projects not requiring
+# LUFA board drivers). If USER is selected, put custom board drivers in a directory called
+# "Board" inside the application directory.
+BOARD = USER
+
+
+# Processor frequency.
+# This will define a symbol, F_CPU, in all source code files equal to the
+# processor frequency in Hz. You can then use this symbol in your source code to
+# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
+# automatically to create a 32-bit value in your source code.
+#
+# This will be an integer division of F_USB below, as it is sourced by
+# F_USB after it has run through any CPU prescalers. Note that this value
+# does not *change* the processor frequency - it should merely be updated to
+# reflect the processor speed set externally so that the code can use accurate
+# software delays.
+F_CPU = 16000000
+
+
+# Input clock frequency.
+# This will define a symbol, F_USB, in all source code files equal to the
+# input clock frequency (before any prescaling is performed) in Hz. This value may
+# differ from F_CPU if prescaling is used on the latter, and is required as the
+# raw input clock is fed directly to the PLL sections of the AVR for high speed
+# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
+# at the end, this will be done automatically to create a 32-bit value in your
+# source code.
+#
+# If no clock division is performed on the input clock inside the AVR (via the
+# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
+F_USB = $(F_CPU)
+
+
+# Starting byte address of the bootloader, as a byte address - computed via the formula
+# BOOT_START = ((FLASH_SIZE_KB - BOOT_SECTION_SIZE_KB) * 1024)
+#
+# Note that the bootloader size and start address given in AVRStudio is in words and not
+# bytes, and so will need to be doubled to obtain the byte address needed by AVR-GCC.
+FLASH_SIZE_KB = 32
+BOOT_SECTION_SIZE_KB = 4
+BOOT_START = 0x$(shell echo "obase=16; ($(FLASH_SIZE_KB) - $(BOOT_SECTION_SIZE_KB)) * 1024" | bc)
+
+
+# Output format. (can be srec, ihex, binary)
+FORMAT = ihex
+
+
+# Target file name (without extension).
+TARGET = Caterina
+
+
+# Object files directory
+# To put object files in current directory, use a dot (.), do NOT make
+# this an empty or blank macro!
+OBJDIR = .
+
+
+# Path to the LUFA library
+LUFA_PATH = LUFA-111009
+
+
+# LUFA library compile-time options and predefined tokens
+LUFA_OPTS = -D USB_DEVICE_ONLY
+LUFA_OPTS += -D DEVICE_STATE_AS_GPIOR=0
+LUFA_OPTS += -D ORDERED_EP_CONFIG
+LUFA_OPTS += -D FIXED_CONTROL_ENDPOINT_SIZE=8
+LUFA_OPTS += -D FIXED_NUM_CONFIGURATIONS=1
+LUFA_OPTS += -D USE_RAM_DESCRIPTORS
+LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"
+LUFA_OPTS += -D NO_INTERNAL_SERIAL
+LUFA_OPTS += -D NO_DEVICE_SELF_POWER
+LUFA_OPTS += -D NO_DEVICE_REMOTE_WAKEUP
+LUFA_OPTS += -D NO_SOF_EVENTS
+
+#LUFA_OPTS += -D NO_BLOCK_SUPPORT
+#LUFA_OPTS += -D NO_EEPROM_BYTE_SUPPORT
+#LUFA_OPTS += -D NO_FLASH_BYTE_SUPPORT
+LUFA_OPTS += -D NO_LOCK_BYTE_WRITE_SUPPORT
+
+
+# Create the LUFA source path variables by including the LUFA root makefile
+include $(LUFA_PATH)/LUFA/makefile
+
+
+# List C source files here. (C dependencies are automatically generated.)
+SRC = $(TARGET).c \
+ Descriptors.c \
+ $(LUFA_SRC_USB) \
+
+
+# List C++ source files here. (C dependencies are automatically generated.)
+CPPSRC =
+
+
+# List Assembler source files here.
+# Make them always end in a capital .S. Files ending in a lowercase .s
+# will not be considered source files but generated files (assembler
+# output from the compiler), and will be deleted upon "make clean"!
+# Even though the DOS/Win* filesystem matches both .s and .S the same,
+# it will preserve the spelling of the filenames, and gcc itself does
+# care about how the name is spelled on its command-line.
+ASRC =
+
+
+# Optimization level, can be [0, 1, 2, 3, s].
+# 0 = turn off optimization. s = optimize for size.
+# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
+OPT = s
+
+
+# Debugging format.
+# Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs.
+# AVR Studio 4.10 requires dwarf-2.
+# AVR [Extended] COFF format requires stabs, plus an avr-objcopy run.
+DEBUG = dwarf-2
+
+
+# List any extra directories to look for include files here.
+# Each directory must be seperated by a space.
+# Use forward slashes for directory separators.
+# For a directory that has spaces, enclose it in quotes.
+EXTRAINCDIRS = $(LUFA_PATH)/
+
+
+# Compiler flag to set the C Standard level.
+# c89 = "ANSI" C
+# gnu89 = c89 plus GCC extensions
+# c99 = ISO C99 standard (not yet fully implemented)
+# gnu99 = c99 plus GCC extensions
+CSTANDARD = -std=c99
+
+
+# Place -D or -U options here for C sources
+CDEFS = -DF_CPU=$(F_CPU)UL
+CDEFS += -DF_USB=$(F_USB)UL
+CDEFS += -DBOARD=BOARD_$(BOARD) -DARCH=ARCH_$(ARCH)
+CDEFS += -DBOOT_START_ADDR=$(BOOT_START)UL
+CDEFS += -DDEVICE_VID=$(VID)UL
+CDEFS += -DDEVICE_PID=$(PID)UL
+CDEFS += $(LUFA_OPTS)
+
+
+# Place -D or -U options here for ASM sources
+ADEFS = -DF_CPU=$(F_CPU)
+ADEFS += -DF_USB=$(F_USB)UL
+ADEFS += -DBOARD=BOARD_$(BOARD)
+ADEFS += -DBOOT_START_ADDR=$(BOOT_START)UL
+ADEFS += $(LUFA_OPTS)
+
+
+# Place -D or -U options here for C++ sources
+CPPDEFS = -DF_CPU=$(F_CPU)UL
+CPPDEFS += -DF_USB=$(F_USB)UL
+CPPDEFS += -DBOARD=BOARD_$(BOARD)
+CPPDEFS += -DBOOT_START_ADDR=$(BOOT_START)UL
+CPPDEFS += $(LUFA_OPTS)
+#CPPDEFS += -D__STDC_LIMIT_MACROS
+#CPPDEFS += -D__STDC_CONSTANT_MACROS
+
+
+
+#---------------- Compiler Options C ----------------
+# -g*: generate debugging information
+# -O*: optimization level
+# -f...: tuning, see GCC manual and avr-libc documentation
+# -Wall...: warning level
+# -Wa,...: tell GCC to pass this to the assembler.
+# -adhlns...: create assembler listing
+CFLAGS = -g$(DEBUG)
+CFLAGS += $(CDEFS)
+CFLAGS += -O$(OPT)
+CFLAGS += -funsigned-char
+CFLAGS += -funsigned-bitfields
+CFLAGS += -ffunction-sections
+CFLAGS += -fno-inline-small-functions
+CFLAGS += -fpack-struct
+CFLAGS += -fshort-enums
+CFLAGS += -fno-strict-aliasing
+CFLAGS += -Wall
+CFLAGS += -Wstrict-prototypes
+#CFLAGS += -mshort-calls
+#CFLAGS += -fno-unit-at-a-time
+#CFLAGS += -Wundef
+#CFLAGS += -Wunreachable-code
+#CFLAGS += -Wsign-compare
+CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
+CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
+CFLAGS += $(CSTANDARD)
+
+
+#---------------- Compiler Options C++ ----------------
+# -g*: generate debugging information
+# -O*: optimization level
+# -f...: tuning, see GCC manual and avr-libc documentation
+# -Wall...: warning level
+# -Wa,...: tell GCC to pass this to the assembler.
+# -adhlns...: create assembler listing
+CPPFLAGS = -g$(DEBUG)
+CPPFLAGS += $(CPPDEFS)
+CPPFLAGS += -O$(OPT)
+CPPFLAGS += -funsigned-char
+CPPFLAGS += -funsigned-bitfields
+CPPFLAGS += -fpack-struct
+CPPFLAGS += -fshort-enums
+CPPFLAGS += -fno-exceptions
+CPPFLAGS += -Wall
+CPPFLAGS += -Wundef
+#CPPFLAGS += -mshort-calls
+#CPPFLAGS += -fno-unit-at-a-time
+#CPPFLAGS += -Wstrict-prototypes
+#CPPFLAGS += -Wunreachable-code
+#CPPFLAGS += -Wsign-compare
+CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst)
+CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
+#CPPFLAGS += $(CSTANDARD)
+
+
+#---------------- Assembler Options ----------------
+# -Wa,...: tell GCC to pass this to the assembler.
+# -adhlns: create listing
+# -gstabs: have the assembler create line number information; note that
+# for use in COFF files, additional information about filenames
+# and function names needs to be present in the assembler source
+# files -- see avr-libc docs [FIXME: not yet described there]
+# -listing-cont-lines: Sets the maximum number of continuation lines of hex
+# dump that will be displayed for a given single line of source input.
+ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100
+
+
+#---------------- Library Options ----------------
+# Minimalistic printf version
+PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
+
+# Floating point printf version (requires MATH_LIB = -lm below)
+PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
+
+# If this is left blank, then it will use the Standard printf version.
+PRINTF_LIB =
+#PRINTF_LIB = $(PRINTF_LIB_MIN)
+#PRINTF_LIB = $(PRINTF_LIB_FLOAT)
+
+
+# Minimalistic scanf version
+SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
+
+# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
+SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
+
+# If this is left blank, then it will use the Standard scanf version.
+SCANF_LIB =
+#SCANF_LIB = $(SCANF_LIB_MIN)
+#SCANF_LIB = $(SCANF_LIB_FLOAT)
+
+
+MATH_LIB = -lm
+
+
+# List any extra directories to look for libraries here.
+# Each directory must be seperated by a space.
+# Use forward slashes for directory separators.
+# For a directory that has spaces, enclose it in quotes.
+EXTRALIBDIRS =
+
+
+
+#---------------- External Memory Options ----------------
+
+# 64 KB of external RAM, starting after internal RAM (ATmega128!),
+# used for variables (.data/.bss) and heap (malloc()).
+#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
+
+# 64 KB of external RAM, starting after internal RAM (ATmega128!),
+# only used for heap (malloc()).
+#EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
+
+EXTMEMOPTS =
+
+
+
+#---------------- Linker Options ----------------
+# -Wl,...: tell GCC to pass this to linker.
+# -Map: create map file
+# --cref: add cross reference to map file
+LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
+LDFLAGS += -Wl,--section-start=.text=$(BOOT_START)
+LDFLAGS += -Wl,--relax
+LDFLAGS += -Wl,--gc-sections
+LDFLAGS += $(EXTMEMOPTS)
+LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
+LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
+#LDFLAGS += -T linker_script.x
+
+
+
+#---------------- Programming Options (avrdude) ----------------
+
+# Programming hardware
+# Type: avrdude -c ?
+# to get a full listing.
+#
+#AVRDUDE_PROGRAMMER = avrispmkII
+AVRDUDE_PROGRAMMER = usbtiny
+
+# com1 = serial port. Use lpt1 to connect to parallel port.
+AVRDUDE_PORT = usb
+
+AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
+#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
+
+
+# Uncomment the following if you want avrdude's erase cycle counter.
+# Note that this counter needs to be initialized first using -Yn,
+# see avrdude manual.
+#AVRDUDE_ERASE_COUNTER = -y
+
+# Uncomment the following if you do /not/ wish a verification to be
+# performed after programming the device.
+#AVRDUDE_NO_VERIFY = -V
+
+# Increase verbosity level. Please use this when submitting bug
+# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
+# to submit bug reports.
+#AVRDUDE_VERBOSE = -v -v
+
+AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
+AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
+AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
+AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)
+
+
+
+#---------------- Debugging Options ----------------
+
+# For simulavr only - target MCU frequency.
+DEBUG_MFREQ = $(F_CPU)
+
+# Set the DEBUG_UI to either gdb or insight.
+# DEBUG_UI = gdb
+DEBUG_UI = insight
+
+# Set the debugging back-end to either avarice, simulavr.
+DEBUG_BACKEND = avarice
+#DEBUG_BACKEND = simulavr
+
+# GDB Init Filename.
+GDBINIT_FILE = __avr_gdbinit
+
+# When using avarice settings for the JTAG
+JTAG_DEV = /dev/com1
+
+# Debugging port used to communicate between GDB / avarice / simulavr.
+DEBUG_PORT = 4242
+
+# Debugging host used to communicate between GDB / avarice / simulavr, normally
+# just set to localhost unless doing some sort of crazy debugging when
+# avarice is running on a different computer.
+DEBUG_HOST = localhost
+
+
+
+#============================================================================
+
+
+# Define programs and commands.
+SHELL = sh
+CC = avr-gcc
+OBJCOPY = avr-objcopy
+OBJDUMP = avr-objdump
+SIZE = avr-size
+AR = avr-ar rcs
+NM = avr-nm
+#AVRDUDE = /Applications/avrdude -C /Applications/avrdude.conf -B 1
+AVRDUDE = /home/david/tmp/Arduino-master/build/linux/dist/tools/avrdude -B 1 -C /home/david/tmp/Arduino-master/build/linux/dist/tools/avrdude.conf
+REMOVE = rm -f
+REMOVEDIR = rm -rf
+COPY = cp
+WINSHELL = cmd
+
+
+# Define Messages
+# English
+MSG_ERRORS_NONE = Errors: none
+MSG_BEGIN = -------- begin --------
+MSG_END = -------- end --------
+MSG_SIZE_BEFORE = Size before:
+MSG_SIZE_AFTER = Size after:
+MSG_COFF = Converting to AVR COFF:
+MSG_EXTENDED_COFF = Converting to AVR Extended COFF:
+MSG_FLASH = Creating load file for Flash:
+MSG_EEPROM = Creating load file for EEPROM:
+MSG_EXTENDED_LISTING = Creating Extended Listing:
+MSG_SYMBOL_TABLE = Creating Symbol Table:
+MSG_LINKING = Linking:
+MSG_COMPILING = Compiling C:
+MSG_COMPILING_CPP = Compiling C++:
+MSG_ASSEMBLING = Assembling:
+MSG_CLEANING = Cleaning project:
+MSG_CREATING_LIBRARY = Creating library:
+
+
+
+
+# Define all object files.
+OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
+
+# Define all listing files.
+LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
+
+
+# Compiler flags to generate dependency files.
+GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
+
+
+# Combine all necessary flags and optional flags.
+# Add target processor to flags.
+ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
+ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS)
+ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
+
+
+
+
+
+# Default target.
+all: begin gccversion sizebefore build sizeafter end
+
+# Change the build target to build a HEX file or a library.
+build: elf hex eep lss sym
+#build: lib
+
+
+elf: $(TARGET).elf
+hex: $(TARGET).hex
+eep: $(TARGET).eep
+lss: $(TARGET).lss
+sym: $(TARGET).sym
+LIBNAME=lib$(TARGET).a
+lib: $(LIBNAME)
+
+
+
+# Eye candy.
+# AVR Studio 3.x does not check make's exit code but relies on
+# the following magic strings to be generated by the compile job.
+begin:
+ @echo
+ @echo $(MSG_BEGIN)
+
+end:
+ @echo $(MSG_END)
+ @echo
+
+
+# Display size of file.
+HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
+ELFSIZE = $(SIZE) $(MCU_FLAG) $(FORMAT_FLAG) $(TARGET).elf
+MCU_FLAG = $(shell $(SIZE) --help | grep -- --mcu > /dev/null && echo --mcu=$(MCU) )
+FORMAT_FLAG = $(shell $(SIZE) --help | grep -- --format=.*avr > /dev/null && echo --format=avr )
+
+
+sizebefore:
+ @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
+ 2>/dev/null; echo; fi
+
+sizeafter:
+ @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
+ 2>/dev/null; echo; fi
+
+
+
+# Display compiler version information.
+gccversion :
+ @$(CC) --version
+
+
+# Program the device.
+program: $(TARGET).hex $(TARGET).eep
+ $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
+
+
+# Generate avr-gdb config/init file which does the following:
+# define the reset signal, load the target file, connect to target, and set
+# a breakpoint at main().
+gdb-config:
+ @$(REMOVE) $(GDBINIT_FILE)
+ @echo define reset >> $(GDBINIT_FILE)
+ @echo SIGNAL SIGHUP >> $(GDBINIT_FILE)
+ @echo end >> $(GDBINIT_FILE)
+ @echo file $(TARGET).elf >> $(GDBINIT_FILE)
+ @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE)
+ifeq ($(DEBUG_BACKEND),simulavr)
+ @echo load >> $(GDBINIT_FILE)
+endif
+ @echo break main >> $(GDBINIT_FILE)
+
+debug: gdb-config $(TARGET).elf
+ifeq ($(DEBUG_BACKEND), avarice)
+ @echo Starting AVaRICE - Press enter when "waiting to connect" message displays.
+ @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \
+ $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT)
+ @$(WINSHELL) /c pause
+
+else
+ @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \
+ $(DEBUG_MFREQ) --port $(DEBUG_PORT)
+endif
+ @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE)
+
+
+
+
+# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
+COFFCONVERT = $(OBJCOPY) --debugging
+COFFCONVERT += --change-section-address .data-0x800000
+COFFCONVERT += --change-section-address .bss-0x800000
+COFFCONVERT += --change-section-address .noinit-0x800000
+COFFCONVERT += --change-section-address .eeprom-0x810000
+
+
+
+coff: $(TARGET).elf
+ @echo
+ @echo $(MSG_COFF) $(TARGET).cof
+ $(COFFCONVERT) -O coff-avr $< $(TARGET).cof
+
+
+extcoff: $(TARGET).elf
+ @echo
+ @echo $(MSG_EXTENDED_COFF) $(TARGET).cof
+ $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof
+
+
+
+# Create final output files (.hex, .eep) from ELF output file.
+%.hex: %.elf
+ @echo
+ @echo $(MSG_FLASH) $@
+ $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock $< $@
+
+%.eep: %.elf
+ @echo
+ @echo $(MSG_EEPROM) $@
+ -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
+ --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
+
+# Create extended listing file from ELF output file.
+%.lss: %.elf
+ @echo
+ @echo $(MSG_EXTENDED_LISTING) $@
+ $(OBJDUMP) -h -S -z $< > $@
+
+# Create a symbol table from ELF output file.
+%.sym: %.elf
+ @echo
+ @echo $(MSG_SYMBOL_TABLE) $@
+ $(NM) -n $< > $@
+
+
+
+# Create library from object files.
+.SECONDARY : $(TARGET).a
+.PRECIOUS : $(OBJ)
+%.a: $(OBJ)
+ @echo
+ @echo $(MSG_CREATING_LIBRARY) $@
+ $(AR) $@ $(OBJ)
+
+
+# Link: create ELF output file from object files.
+.SECONDARY : $(TARGET).elf
+.PRECIOUS : $(OBJ)
+%.elf: $(OBJ)
+ @echo
+ @echo $(MSG_LINKING) $@
+ $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
+
+
+# Compile: create object files from C source files.
+$(OBJDIR)/%.o : %.c
+ @echo
+ @echo $(MSG_COMPILING) $<
+ $(CC) -c $(ALL_CFLAGS) $< -o $@
+
+
+# Compile: create object files from C++ source files.
+$(OBJDIR)/%.o : %.cpp
+ @echo
+ @echo $(MSG_COMPILING_CPP) $<
+ $(CC) -c $(ALL_CPPFLAGS) $< -o $@
+
+
+# Compile: create assembler files from C source files.
+%.s : %.c
+ $(CC) -S $(ALL_CFLAGS) $< -o $@
+
+
+# Compile: create assembler files from C++ source files.
+%.s : %.cpp
+ $(CC) -S $(ALL_CPPFLAGS) $< -o $@
+
+
+# Assemble: create object files from assembler source files.
+$(OBJDIR)/%.o : %.S
+ @echo
+ @echo $(MSG_ASSEMBLING) $<
+ $(CC) -c $(ALL_ASFLAGS) $< -o $@
+
+
+# Create preprocessed source for use in sending a bug report.
+%.i : %.c
+ $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@
+
+
+# Target: clean project.
+clean: begin clean_list end
+
+clean_list :
+ @echo
+ @echo $(MSG_CLEANING)
+ $(REMOVE) $(TARGET).hex
+ $(REMOVE) $(TARGET).eep
+ $(REMOVE) $(TARGET).cof
+ $(REMOVE) $(TARGET).elf
+ $(REMOVE) $(TARGET).map
+ $(REMOVE) $(TARGET).sym
+ $(REMOVE) $(TARGET).lss
+ $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o)
+ $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst)
+ $(REMOVE) $(SRC:.c=.s)
+ $(REMOVE) $(SRC:.c=.d)
+ $(REMOVE) $(SRC:.c=.i)
+ $(REMOVEDIR) .dep
+
+doxygen:
+ @echo Generating Project Documentation \($(TARGET)\)...
+ @doxygen Doxygen.conf
+ @echo Documentation Generation Complete.
+
+clean_doxygen:
+ rm -rf Documentation
+
+checksource:
+ @for f in $(SRC) $(CPPSRC) $(ASRC); do \
+ if [ -f $$f ]; then \
+ echo "Found Source File: $$f" ; \
+ else \
+ echo "Source File Not Found: $$f" ; \
+ fi; done
+
+
+# Create object files directory
+$(shell mkdir $(OBJDIR) 2>/dev/null)
+
+
+# Include the dependency files.
+-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
+
+
+# Listing of phony targets.
+.PHONY : all begin finish end sizebefore sizeafter gccversion \
+build elf hex eep lss sym coff extcoff doxygen clean \
+clean_list clean_doxygen program debug gdb-config checksource
+
diff --git a/bootloaders/caterina-Arduino_Robot/README.md b/bootloaders/caterina-Arduino_Robot/README.md
new file mode 100644
index 0000000..08235e0
--- /dev/null
+++ b/bootloaders/caterina-Arduino_Robot/README.md
@@ -0,0 +1,27 @@
+Building the bootloader for the Arduino Robot
+=============================================
+
+The Arduino Robot has two boards featuring the atmega32U4 processor from Atmel. Each one of them is identified as a different board at the USB level and has a different bootloader.
+
+The Arduino Robot Control board has the USB identifier 0x0038. This is the value configured by default in the Makefile.
+
+The Arduino Robot Motor board has the USB identifier 0x0039. If you want to compile/upload this version of the bootloader, you will have to edit the Makefile, comment away the like dedicated to the PID and uncomment the one that configures such variable accordingly.
+
+The general conditions for using these bootloaders require downloading a specific version of LUFA as explained here:
+
+1. Download the LUFA-111009 file (http://fourwalledcubicle.com/blog/2011/10/lufa-111009-released/).
+2. Extract that file directly to the Caterina-Arduino_Robot bootloader directory.
+3. Open a command prompt in the Caterina-Arduino_Robot bootloader directory.
+4. Type 'make'.
+5. Enjoy!
+
+Programming the bootloader for one of the Arduino Robot boards
+1. Open a command prompt in the Caterina-Arduino_Robot folder.
+2. Connect your programmer- use a 2x3 .1" header, pressed against the programming vias.
+3. Edit the make file for it to include the right programmer (e.g. in my lab I have AVRMKII and USBTINY ISP)
+4. Type 'make program' into the command prompt.
+
+Differences between this bootoloader and the standard one for Leonardo boards
+=============================================================================
+
+This bootloader is different from the one on the standard Leonardo boards. To enter the bootloader, you need to double click the reset button. You need to click twice in less that 3/4 of a second (easy uh?). This bootloader, designed in the first place for the LilypadUSB, seems to be optimal for situations when users are e.g. using their robots in soccer competitions where they make direct manipulation of the board as it runs.
diff --git a/cores/arduino/Arduino.h b/cores/arduino/Arduino.h
index b265825..3b9ccca 100755
--- a/cores/arduino/Arduino.h
+++ b/cores/arduino/Arduino.h
@@ -46,7 +46,7 @@ extern "C"{
#define EXTERNAL 1
#define INTERNAL 2
#else
-#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644P__)
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
#define INTERNAL1V1 2
#define INTERNAL2V56 3
#else
diff --git a/cores/arduino/Stream.h b/cores/arduino/Stream.h
index 58bbf75..007b4bc 100644
--- a/cores/arduino/Stream.h
+++ b/cores/arduino/Stream.h
@@ -37,7 +37,7 @@ readBytesBetween( pre_string, terminator, buffer, length)
class Stream : public Print
{
- private:
+ protected:
unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
unsigned long _startMillis; // used for timeout measurement
int timedRead(); // private method to read stream with timeout
diff --git a/cores/arduino/WInterrupts.c b/cores/arduino/WInterrupts.c
index de49cd1..d3fbf10 100644
--- a/cores/arduino/WInterrupts.c
+++ b/cores/arduino/WInterrupts.c
@@ -51,14 +51,14 @@ void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
// I hate doing this, but the register assignment differs between the 1280/2560
// and the 32U4. Since avrlib defines registers PCMSK1 and PCMSK2 that aren't
// even present on the 32U4 this is the only way to distinguish between them.
- case 0:
- EICRA = (EICRA & ~((1<<ISC00) | (1<<ISC01))) | (mode << ISC00);
- EIMSK |= (1<<INT0);
- break;
- case 1:
- EICRA = (EICRA & ~((1<<ISC10) | (1<<ISC11))) | (mode << ISC10);
- EIMSK |= (1<<INT1);
- break;
+ case 0:
+ EICRA = (EICRA & ~((1<<ISC00) | (1<<ISC01))) | (mode << ISC00);
+ EIMSK |= (1<<INT0);
+ break;
+ case 1:
+ EICRA = (EICRA & ~((1<<ISC10) | (1<<ISC11))) | (mode << ISC10);
+ EIMSK |= (1<<INT1);
+ break;
case 2:
EICRA = (EICRA & ~((1<<ISC20) | (1<<ISC21))) | (mode << ISC20);
EIMSK |= (1<<INT2);
@@ -67,6 +67,10 @@ void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
EICRA = (EICRA & ~((1<<ISC30) | (1<<ISC31))) | (mode << ISC30);
EIMSK |= (1<<INT3);
break;
+ case 4:
+ EICRB = (EICRB & ~((1<<ISC60) | (1<<ISC61))) | (mode << ISC60);
+ EIMSK |= (1<<INT6);
+ break;
#elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
case 2:
EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
@@ -166,7 +170,10 @@ void detachInterrupt(uint8_t interruptNum) {
break;
case 3:
EIMSK &= ~(1<<INT3);
- break;
+ break;
+ case 4:
+ EIMSK &= ~(1<<INT6);
+ break;
#elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
case 2:
EIMSK &= ~(1 << INT0);
@@ -250,6 +257,11 @@ ISR(INT3_vect) {
intFunc[EXTERNAL_INT_3]();
}
+ISR(INT6_vect) {
+ if(intFunc[EXTERNAL_INT_4])
+ intFunc[EXTERNAL_INT_4]();
+}
+
#elif defined(EICRA) && defined(EICRB)
ISR(INT0_vect) {
diff --git a/cores/arduino/WString.cpp b/cores/arduino/WString.cpp
index c6839fc..e19f543 100644
--- a/cores/arduino/WString.cpp
+++ b/cores/arduino/WString.cpp
@@ -100,6 +100,19 @@ String::String(unsigned long value, unsigned char base)
*this = buf;
}
+String::String(float value, int decimalPlaces)
+{
+ init();
+ char buf[33];
+ *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
+}
+
+String::String(double value, int decimalPlaces)
+{
+ init();
+ char buf[33];
+ *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
+}
String::~String()
{
free(buffer);
@@ -283,6 +296,20 @@ unsigned char String::concat(unsigned long num)
return concat(buf, strlen(buf));
}
+unsigned char String::concat(float num)
+{
+ char buf[20];
+ char* string = dtostrf(num, 8, 6, buf);
+ return concat(string, strlen(string));
+}
+
+unsigned char String::concat(double num)
+{
+ char buf[20];
+ char* string = dtostrf(num, 8, 6, buf);
+ return concat(string, strlen(string));
+}
+
/*********************************************/
/* Concatenate */
/*********************************************/
@@ -343,6 +370,19 @@ StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
return a;
}
+StringSumHelper & operator + (const StringSumHelper &lhs, float num)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(num)) a.invalidate();
+ return a;
+}
+
+StringSumHelper & operator + (const StringSumHelper &lhs, double num)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(num)) a.invalidate();
+ return a;
+}
/*********************************************/
/* Comparison */
/*********************************************/
@@ -604,6 +644,22 @@ void String::replace(const String& find, const String& replace)
}
}
+void String::remove(unsigned int index){
+ if (index >= len) { return; }
+ int count = len - index;
+ remove(index, count);
+}
+
+void String::remove(unsigned int index, unsigned int count){
+ if (index >= len) { return; }
+ if (count <= 0) { return; }
+ if (index + count > len) { count = len - index; }
+ char *writeTo = buffer + index;
+ len = len - count;
+ strncpy(writeTo, buffer + index + count,len - index);
+ buffer[len] = 0;
+}
+
void String::toLowerCase(void)
{
if (!buffer) return;
@@ -643,3 +699,8 @@ long String::toInt(void) const
}
+float String::toFloat(void) const
+{
+ if (buffer) return float(atof(buffer));
+ return 0;
+}
diff --git a/cores/arduino/WString.h b/cores/arduino/WString.h
index 947325e..2d372c5 100644
--- a/cores/arduino/WString.h
+++ b/cores/arduino/WString.h
@@ -68,6 +68,8 @@ public:
explicit String(unsigned int, unsigned char base=10);
explicit String(long, unsigned char base=10);
explicit String(unsigned long, unsigned char base=10);
+ explicit String(float, int decimalPlaces=6);
+ explicit String(double, int decimalPlaces=6);
~String(void);
// memory management
@@ -100,6 +102,8 @@ public:
unsigned char concat(unsigned int num);
unsigned char concat(long num);
unsigned char concat(unsigned long num);
+ unsigned char concat(float num);
+ unsigned char concat(double num);
// if there's not enough memory for the concatenated value, the string
// will be left unchanged (but this isn't signalled in any way)
@@ -120,6 +124,8 @@ public:
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
+ friend StringSumHelper & operator + (const StringSumHelper &lhs, float num);
+ friend StringSumHelper & operator + (const StringSumHelper &lhs, double num);
// comparison (only works w/ Strings and "strings")
operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
@@ -147,6 +153,7 @@ public:
void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
{getBytes((unsigned char *)buf, bufsize, index);}
+ const char * c_str() const { return buffer; }
// search
int indexOf( char ch ) const;
@@ -163,12 +170,15 @@ public:
// modification
void replace(char find, char replace);
void replace(const String& find, const String& replace);
+ void remove(unsigned int index);
+ void remove(unsigned int index, unsigned int count);
void toLowerCase(void);
void toUpperCase(void);
void trim(void);
// parsing/conversion
long toInt(void) const;
+ float toFloat(void) const;
protected:
char *buffer; // the actual char array
diff --git a/cores/arduino/wiring_analog.c b/cores/arduino/wiring_analog.c
index c99325f..8feead9 100644
--- a/cores/arduino/wiring_analog.c
+++ b/cores/arduino/wiring_analog.c
@@ -50,7 +50,7 @@ int analogRead(uint8_t pin)
if (pin >= 54) pin -= 54; // allow for channel or pin numbers
#elif defined(__AVR_ATmega32U4__)
if (pin >= 18) pin -= 18; // allow for channel or pin numbers
-#elif defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644P__)
+#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
if (pin >= 24) pin -= 24; // allow for channel or pin numbers
#else
if (pin >= 14) pin -= 14; // allow for channel or pin numbers
diff --git a/cores/arduino/wiring_private.h b/cores/arduino/wiring_private.h
index f678265..c366005 100755
--- a/cores/arduino/wiring_private.h
+++ b/cores/arduino/wiring_private.h
@@ -54,10 +54,10 @@ extern "C"{
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define EXTERNAL_NUM_INTERRUPTS 8
-#elif defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644P__)
+#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
#define EXTERNAL_NUM_INTERRUPTS 3
#elif defined(__AVR_ATmega32U4__)
-#define EXTERNAL_NUM_INTERRUPTS 4
+#define EXTERNAL_NUM_INTERRUPTS 5
#else
#define EXTERNAL_NUM_INTERRUPTS 2
#endif
diff --git a/cores/robot/Arduino.h b/cores/robot/Arduino.h
new file mode 100755
index 0000000..3b9ccca
--- /dev/null
+++ b/cores/robot/Arduino.h
@@ -0,0 +1,215 @@
+#ifndef Arduino_h
+#define Arduino_h
+
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+
+#include <avr/pgmspace.h>
+#include <avr/io.h>
+#include <avr/interrupt.h>
+
+#include "binary.h"
+
+#ifdef __cplusplus
+extern "C"{
+#endif
+
+#define HIGH 0x1
+#define LOW 0x0
+
+#define INPUT 0x0
+#define OUTPUT 0x1
+#define INPUT_PULLUP 0x2
+
+#define true 0x1
+#define false 0x0
+
+#define PI 3.1415926535897932384626433832795
+#define HALF_PI 1.5707963267948966192313216916398
+#define TWO_PI 6.283185307179586476925286766559
+#define DEG_TO_RAD 0.017453292519943295769236907684886
+#define RAD_TO_DEG 57.295779513082320876798154814105
+
+#define SERIAL 0x0
+#define DISPLAY 0x1
+
+#define LSBFIRST 0
+#define MSBFIRST 1
+
+#define CHANGE 1
+#define FALLING 2
+#define RISING 3
+
+#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) || defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
+#define DEFAULT 0
+#define EXTERNAL 1
+#define INTERNAL 2
+#else
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
+#define INTERNAL1V1 2
+#define INTERNAL2V56 3
+#else
+#define INTERNAL 3
+#endif
+#define DEFAULT 1
+#define EXTERNAL 0
+#endif
+
+// undefine stdlib's abs if encountered
+#ifdef abs
+#undef abs
+#endif
+
+#define min(a,b) ((a)<(b)?(a):(b))
+#define max(a,b) ((a)>(b)?(a):(b))
+#define abs(x) ((x)>0?(x):-(x))
+#define constrain(amt,low,high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
+#define round(x) ((x)>=0?(long)((x)+0.5):(long)((x)-0.5))
+#define radians(deg) ((deg)*DEG_TO_RAD)
+#define degrees(rad) ((rad)*RAD_TO_DEG)
+#define sq(x) ((x)*(x))
+
+#define interrupts() sei()
+#define noInterrupts() cli()
+
+#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
+#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
+#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
+
+#define lowByte(w) ((uint8_t) ((w) & 0xff))
+#define highByte(w) ((uint8_t) ((w) >> 8))
+
+#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
+#define bitSet(value, bit) ((value) |= (1UL << (bit)))
+#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
+#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
+
+
+typedef unsigned int word;
+
+#define bit(b) (1UL << (b))
+
+typedef uint8_t boolean;
+typedef uint8_t byte;
+
+void init(void);
+
+void pinMode(uint8_t, uint8_t);
+void digitalWrite(uint8_t, uint8_t);
+int digitalRead(uint8_t);
+int analogRead(uint8_t);
+void analogReference(uint8_t mode);
+void analogWrite(uint8_t, int);
+
+unsigned long millis(void);
+unsigned long micros(void);
+void delay(unsigned long);
+void delayMicroseconds(unsigned int us);
+unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout);
+
+void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
+uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
+
+void attachInterrupt(uint8_t, void (*)(void), int mode);
+void detachInterrupt(uint8_t);
+
+void setup(void);
+void loop(void);
+
+// Get the bit location within the hardware port of the given virtual pin.
+// This comes from the pins_*.c file for the active board configuration.
+
+#define analogInPinToBit(P) (P)
+
+// On the ATmega1280, the addresses of some of the port registers are
+// greater than 255, so we can't store them in uint8_t's.
+extern const uint16_t PROGMEM port_to_mode_PGM[];
+extern const uint16_t PROGMEM port_to_input_PGM[];
+extern const uint16_t PROGMEM port_to_output_PGM[];
+
+extern const uint8_t PROGMEM digital_pin_to_port_PGM[];
+// extern const uint8_t PROGMEM digital_pin_to_bit_PGM[];
+extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[];
+extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
+
+// Get the bit location within the hardware port of the given virtual pin.
+// This comes from the pins_*.c file for the active board configuration.
+//
+// These perform slightly better as macros compared to inline functions
+//
+#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
+#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
+#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
+#define analogInPinToBit(P) (P)
+#define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
+#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
+#define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )
+
+#define NOT_A_PIN 0
+#define NOT_A_PORT 0
+
+#ifdef ARDUINO_MAIN
+#define PA 1
+#define PB 2
+#define PC 3
+#define PD 4
+#define PE 5
+#define PF 6
+#define PG 7
+#define PH 8
+#define PJ 10
+#define PK 11
+#define PL 12
+#endif
+
+#define NOT_ON_TIMER 0
+#define TIMER0A 1
+#define TIMER0B 2
+#define TIMER1A 3
+#define TIMER1B 4
+#define TIMER2 5
+#define TIMER2A 6
+#define TIMER2B 7
+
+#define TIMER3A 8
+#define TIMER3B 9
+#define TIMER3C 10
+#define TIMER4A 11
+#define TIMER4B 12
+#define TIMER4C 13
+#define TIMER4D 14
+#define TIMER5A 15
+#define TIMER5B 16
+#define TIMER5C 17
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+#ifdef __cplusplus
+#include "WCharacter.h"
+#include "WString.h"
+#include "HardwareSerial.h"
+
+uint16_t makeWord(uint16_t w);
+uint16_t makeWord(byte h, byte l);
+
+#define word(...) makeWord(__VA_ARGS__)
+
+unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
+
+void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
+void noTone(uint8_t _pin);
+
+// WMath prototypes
+long random(long);
+long random(long, long);
+void randomSeed(unsigned int);
+long map(long, long, long, long, long);
+
+#endif
+
+#include "pins_arduino.h"
+
+#endif
diff --git a/cores/robot/CDC.cpp b/cores/robot/CDC.cpp
new file mode 100644
index 0000000..701e483
--- /dev/null
+++ b/cores/robot/CDC.cpp
@@ -0,0 +1,239 @@
+
+
+/* Copyright (c) 2011, Peter Barrett
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#include "Platform.h"
+#include "USBAPI.h"
+#include <avr/wdt.h>
+
+#if defined(USBCON)
+#ifdef CDC_ENABLED
+
+#if (RAMEND < 1000)
+#define SERIAL_BUFFER_SIZE 16
+#else
+#define SERIAL_BUFFER_SIZE 64
+#endif
+
+struct ring_buffer
+{
+ unsigned char buffer[SERIAL_BUFFER_SIZE];
+ volatile int head;
+ volatile int tail;
+};
+
+ring_buffer cdc_rx_buffer = { { 0 }, 0, 0};
+
+typedef struct
+{
+ u32 dwDTERate;
+ u8 bCharFormat;
+ u8 bParityType;
+ u8 bDataBits;
+ u8 lineState;
+} LineInfo;
+
+static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
+
+#define WEAK __attribute__ ((weak))
+
+extern const CDCDescriptor _cdcInterface PROGMEM;
+const CDCDescriptor _cdcInterface =
+{
+ D_IAD(0,2,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,1),
+
+ // CDC communication interface
+ D_INTERFACE(CDC_ACM_INTERFACE,1,CDC_COMMUNICATION_INTERFACE_CLASS,CDC_ABSTRACT_CONTROL_MODEL,0),
+ D_CDCCS(CDC_HEADER,0x10,0x01), // Header (1.10 bcd)
+ D_CDCCS(CDC_CALL_MANAGEMENT,1,1), // Device handles call management (not)
+ D_CDCCS4(CDC_ABSTRACT_CONTROL_MANAGEMENT,6), // SET_LINE_CODING, GET_LINE_CODING, SET_CONTROL_LINE_STATE supported
+ D_CDCCS(CDC_UNION,CDC_ACM_INTERFACE,CDC_DATA_INTERFACE), // Communication interface is master, data interface is slave 0
+ D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_ACM),USB_ENDPOINT_TYPE_INTERRUPT,0x10,0x40),
+
+ // CDC data interface
+ D_INTERFACE(CDC_DATA_INTERFACE,2,CDC_DATA_INTERFACE_CLASS,0,0),
+ D_ENDPOINT(USB_ENDPOINT_OUT(CDC_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,0x40,0),
+ D_ENDPOINT(USB_ENDPOINT_IN (CDC_ENDPOINT_IN ),USB_ENDPOINT_TYPE_BULK,0x40,0)
+};
+
+int WEAK CDC_GetInterface(u8* interfaceNum)
+{
+ interfaceNum[0] += 2; // uses 2
+ return USB_SendControl(TRANSFER_PGM,&_cdcInterface,sizeof(_cdcInterface));
+}
+
+bool WEAK CDC_Setup(Setup& setup)
+{
+ u8 r = setup.bRequest;
+ u8 requestType = setup.bmRequestType;
+
+ if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
+ {
+ if (CDC_GET_LINE_CODING == r)
+ {
+ USB_SendControl(0,(void*)&_usbLineInfo,7);
+ return true;
+ }
+ }
+
+ if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
+ {
+ if (CDC_SET_LINE_CODING == r)
+ {
+ USB_RecvControl((void*)&_usbLineInfo,7);
+ return true;
+ }
+
+ if (CDC_SET_CONTROL_LINE_STATE == r)
+ {
+ _usbLineInfo.lineState = setup.wValueL;
+
+ // auto-reset into the bootloader is triggered when the port, already
+ // open at 1200 bps, is closed. this is the signal to start the watchdog
+ // with a relatively long period so it can finish housekeeping tasks
+ // like servicing endpoints before the sketch ends
+ if (1200 == _usbLineInfo.dwDTERate) {
+ // We check DTR state to determine if host port is open (bit 0 of lineState).
+ if ((_usbLineInfo.lineState & 0x01) == 0) {
+ *(uint16_t *)0x0800 = 0x7777;
+ wdt_enable(WDTO_120MS);
+ } else {
+ // Most OSs do some intermediate steps when configuring ports and DTR can
+ // twiggle more than once before stabilizing.
+ // To avoid spurious resets we set the watchdog to 250ms and eventually
+ // cancel if DTR goes back high.
+
+ wdt_disable();
+ wdt_reset();
+ *(uint16_t *)0x0800 = 0x0;
+ }
+ }
+ return true;
+ }
+ }
+ return false;
+}
+
+
+int _serialPeek = -1;
+void Serial_::begin(uint16_t baud_count)
+{
+}
+
+void Serial_::end(void)
+{
+}
+
+void Serial_::accept(void)
+{
+ ring_buffer *buffer = &cdc_rx_buffer;
+ int i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
+
+ // if we should be storing the received character into the location
+ // just before the tail (meaning that the head would advance to the
+ // current location of the tail), we're about to overflow the buffer
+ // and so we don't write the character or advance the head.
+
+ // while we have room to store a byte
+ while (i != buffer->tail) {
+ int c = USB_Recv(CDC_RX);
+ if (c == -1)
+ break; // no more data
+ buffer->buffer[buffer->head] = c;
+ buffer->head = i;
+
+ i = (unsigned int)(buffer->head+1) % SERIAL_BUFFER_SIZE;
+ }
+}
+
+int Serial_::available(void)
+{
+ ring_buffer *buffer = &cdc_rx_buffer;
+ return (unsigned int)(SERIAL_BUFFER_SIZE + buffer->head - buffer->tail) % SERIAL_BUFFER_SIZE;
+}
+
+int Serial_::peek(void)
+{
+ ring_buffer *buffer = &cdc_rx_buffer;
+ if (buffer->head == buffer->tail) {
+ return -1;
+ } else {
+ return buffer->buffer[buffer->tail];
+ }
+}
+
+int Serial_::read(void)
+{
+ ring_buffer *buffer = &cdc_rx_buffer;
+ // if the head isn't ahead of the tail, we don't have any characters
+ if (buffer->head == buffer->tail) {
+ return -1;
+ } else {
+ unsigned char c = buffer->buffer[buffer->tail];
+ buffer->tail = (unsigned int)(buffer->tail + 1) % SERIAL_BUFFER_SIZE;
+ return c;
+ }
+}
+
+void Serial_::flush(void)
+{
+ USB_Flush(CDC_TX);
+}
+
+size_t Serial_::write(uint8_t c)
+{
+ /* only try to send bytes if the high-level CDC connection itself
+ is open (not just the pipe) - the OS should set lineState when the port
+ is opened and clear lineState when the port is closed.
+ bytes sent before the user opens the connection or after
+ the connection is closed are lost - just like with a UART. */
+
+ // TODO - ZE - check behavior on different OSes and test what happens if an
+ // open connection isn't broken cleanly (cable is yanked out, host dies
+ // or locks up, or host virtual serial port hangs)
+ if (_usbLineInfo.lineState > 0) {
+ int r = USB_Send(CDC_TX,&c,1);
+ if (r > 0) {
+ return r;
+ } else {
+ setWriteError();
+ return 0;
+ }
+ }
+ setWriteError();
+ return 0;
+}
+
+// This operator is a convenient way for a sketch to check whether the
+// port has actually been configured and opened by the host (as opposed
+// to just being connected to the host). It can be used, for example, in
+// setup() before printing to ensure that an application on the host is
+// actually ready to receive and display the data.
+// We add a short delay before returning to fix a bug observed by Federico
+// where the port is configured (lineState != 0) but not quite opened.
+Serial_::operator bool() {
+ bool result = false;
+ if (_usbLineInfo.lineState > 0)
+ result = true;
+ delay(10);
+ return result;
+}
+
+Serial_ Serial;
+
+#endif
+#endif /* if defined(USBCON) */
diff --git a/cores/robot/Client.h b/cores/robot/Client.h
new file mode 100644
index 0000000..ea13483
--- /dev/null
+++ b/cores/robot/Client.h
@@ -0,0 +1,26 @@
+#ifndef client_h
+#define client_h
+#include "Print.h"
+#include "Stream.h"
+#include "IPAddress.h"
+
+class Client : public Stream {
+
+public:
+ virtual int connect(IPAddress ip, uint16_t port) =0;
+ virtual int connect(const char *host, uint16_t port) =0;
+ virtual size_t write(uint8_t) =0;
+ virtual size_t write(const uint8_t *buf, size_t size) =0;
+ virtual int available() = 0;
+ virtual int read() = 0;
+ virtual int read(uint8_t *buf, size_t size) = 0;
+ virtual int peek() = 0;
+ virtual void flush() = 0;
+ virtual void stop() = 0;
+ virtual uint8_t connected() = 0;
+ virtual operator bool() = 0;
+protected:
+ uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
+};
+
+#endif
diff --git a/cores/robot/HID.cpp b/cores/robot/HID.cpp
new file mode 100644
index 0000000..ac63608
--- /dev/null
+++ b/cores/robot/HID.cpp
@@ -0,0 +1,520 @@
+
+
+/* Copyright (c) 2011, Peter Barrett
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#include "Platform.h"
+#include "USBAPI.h"
+#include "USBDesc.h"
+
+#if defined(USBCON)
+#ifdef HID_ENABLED
+
+//#define RAWHID_ENABLED
+
+// Singletons for mouse and keyboard
+
+Mouse_ Mouse;
+Keyboard_ Keyboard;
+
+//================================================================================
+//================================================================================
+
+// HID report descriptor
+
+#define LSB(_x) ((_x) & 0xFF)
+#define MSB(_x) ((_x) >> 8)
+
+#define RAWHID_USAGE_PAGE 0xFFC0
+#define RAWHID_USAGE 0x0C00
+#define RAWHID_TX_SIZE 64
+#define RAWHID_RX_SIZE 64
+
+extern const u8 _hidReportDescriptor[] PROGMEM;
+const u8 _hidReportDescriptor[] = {
+
+ // Mouse
+ 0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 54
+ 0x09, 0x02, // USAGE (Mouse)
+ 0xa1, 0x01, // COLLECTION (Application)
+ 0x09, 0x01, // USAGE (Pointer)
+ 0xa1, 0x00, // COLLECTION (Physical)
+ 0x85, 0x01, // REPORT_ID (1)
+ 0x05, 0x09, // USAGE_PAGE (Button)
+ 0x19, 0x01, // USAGE_MINIMUM (Button 1)
+ 0x29, 0x03, // USAGE_MAXIMUM (Button 3)
+ 0x15, 0x00, // LOGICAL_MINIMUM (0)
+ 0x25, 0x01, // LOGICAL_MAXIMUM (1)
+ 0x95, 0x03, // REPORT_COUNT (3)
+ 0x75, 0x01, // REPORT_SIZE (1)
+ 0x81, 0x02, // INPUT (Data,Var,Abs)
+ 0x95, 0x01, // REPORT_COUNT (1)
+ 0x75, 0x05, // REPORT_SIZE (5)
+ 0x81, 0x03, // INPUT (Cnst,Var,Abs)
+ 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
+ 0x09, 0x30, // USAGE (X)
+ 0x09, 0x31, // USAGE (Y)
+ 0x09, 0x38, // USAGE (Wheel)
+ 0x15, 0x81, // LOGICAL_MINIMUM (-127)
+ 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
+ 0x75, 0x08, // REPORT_SIZE (8)
+ 0x95, 0x03, // REPORT_COUNT (3)
+ 0x81, 0x06, // INPUT (Data,Var,Rel)
+ 0xc0, // END_COLLECTION
+ 0xc0, // END_COLLECTION
+
+ // Keyboard
+ 0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 47
+ 0x09, 0x06, // USAGE (Keyboard)
+ 0xa1, 0x01, // COLLECTION (Application)
+ 0x85, 0x02, // REPORT_ID (2)
+ 0x05, 0x07, // USAGE_PAGE (Keyboard)
+
+ 0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
+ 0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
+ 0x15, 0x00, // LOGICAL_MINIMUM (0)
+ 0x25, 0x01, // LOGICAL_MAXIMUM (1)
+ 0x75, 0x01, // REPORT_SIZE (1)
+
+ 0x95, 0x08, // REPORT_COUNT (8)
+ 0x81, 0x02, // INPUT (Data,Var,Abs)
+ 0x95, 0x01, // REPORT_COUNT (1)
+ 0x75, 0x08, // REPORT_SIZE (8)
+ 0x81, 0x03, // INPUT (Cnst,Var,Abs)
+
+ 0x95, 0x06, // REPORT_COUNT (6)
+ 0x75, 0x08, // REPORT_SIZE (8)
+ 0x15, 0x00, // LOGICAL_MINIMUM (0)
+ 0x25, 0x65, // LOGICAL_MAXIMUM (101)
+ 0x05, 0x07, // USAGE_PAGE (Keyboard)
+
+ 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
+ 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
+ 0x81, 0x00, // INPUT (Data,Ary,Abs)
+ 0xc0, // END_COLLECTION
+
+#if RAWHID_ENABLED
+ // RAW HID
+ 0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30
+ 0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
+
+ 0xA1, 0x01, // Collection 0x01
+ 0x85, 0x03, // REPORT_ID (3)
+ 0x75, 0x08, // report size = 8 bits
+ 0x15, 0x00, // logical minimum = 0
+ 0x26, 0xFF, 0x00, // logical maximum = 255
+
+ 0x95, 64, // report count TX
+ 0x09, 0x01, // usage
+ 0x81, 0x02, // Input (array)
+
+ 0x95, 64, // report count RX
+ 0x09, 0x02, // usage
+ 0x91, 0x02, // Output (array)
+ 0xC0 // end collection
+#endif
+};
+
+extern const HIDDescriptor _hidInterface PROGMEM;
+const HIDDescriptor _hidInterface =
+{
+ D_INTERFACE(HID_INTERFACE,1,3,0,0),
+ D_HIDREPORT(sizeof(_hidReportDescriptor)),
+ D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x01)
+};
+
+//================================================================================
+//================================================================================
+// Driver
+
+u8 _hid_protocol = 1;
+u8 _hid_idle = 1;
+
+#define WEAK __attribute__ ((weak))
+
+int WEAK HID_GetInterface(u8* interfaceNum)
+{
+ interfaceNum[0] += 1; // uses 1
+ return USB_SendControl(TRANSFER_PGM,&_hidInterface,sizeof(_hidInterface));
+}
+
+int WEAK HID_GetDescriptor(int i)
+{
+ return USB_SendControl(TRANSFER_PGM,_hidReportDescriptor,sizeof(_hidReportDescriptor));
+}
+
+void WEAK HID_SendReport(u8 id, const void* data, int len)
+{
+ USB_Send(HID_TX, &id, 1);
+ USB_Send(HID_TX | TRANSFER_RELEASE,data,len);
+}
+
+bool WEAK HID_Setup(Setup& setup)
+{
+ u8 r = setup.bRequest;
+ u8 requestType = setup.bmRequestType;
+ if (REQUEST_DEVICETOHOST_CLASS_INTERFACE == requestType)
+ {
+ if (HID_GET_REPORT == r)
+ {
+ //HID_GetReport();
+ return true;
+ }
+ if (HID_GET_PROTOCOL == r)
+ {
+ //Send8(_hid_protocol); // TODO
+ return true;
+ }
+ }
+
+ if (REQUEST_HOSTTODEVICE_CLASS_INTERFACE == requestType)
+ {
+ if (HID_SET_PROTOCOL == r)
+ {
+ _hid_protocol = setup.wValueL;
+ return true;
+ }
+
+ if (HID_SET_IDLE == r)
+ {
+ _hid_idle = setup.wValueL;
+ return true;
+ }
+ }
+ return false;
+}
+
+//================================================================================
+//================================================================================
+// Mouse
+
+Mouse_::Mouse_(void) : _buttons(0)
+{
+}
+
+void Mouse_::begin(void)
+{
+}
+
+void Mouse_::end(void)
+{
+}
+
+void Mouse_::click(uint8_t b)
+{
+ _buttons = b;
+ move(0,0,0);
+ _buttons = 0;
+ move(0,0,0);
+}
+
+void Mouse_::move(signed char x, signed char y, signed char wheel)
+{
+ u8 m[4];
+ m[0] = _buttons;
+ m[1] = x;
+ m[2] = y;
+ m[3] = wheel;
+ HID_SendReport(1,m,4);
+}
+
+void Mouse_::buttons(uint8_t b)
+{
+ if (b != _buttons)
+ {
+ _buttons = b;
+ move(0,0,0);
+ }
+}
+
+void Mouse_::press(uint8_t b)
+{
+ buttons(_buttons | b);
+}
+
+void Mouse_::release(uint8_t b)
+{
+ buttons(_buttons & ~b);
+}
+
+bool Mouse_::isPressed(uint8_t b)
+{
+ if ((b & _buttons) > 0)
+ return true;
+ return false;
+}
+
+//================================================================================
+//================================================================================
+// Keyboard
+
+Keyboard_::Keyboard_(void)
+{
+}
+
+void Keyboard_::begin(void)
+{
+}
+
+void Keyboard_::end(void)
+{
+}
+
+void Keyboard_::sendReport(KeyReport* keys)
+{
+ HID_SendReport(2,keys,sizeof(KeyReport));
+}
+
+extern
+const uint8_t _asciimap[128] PROGMEM;
+
+#define SHIFT 0x80
+const uint8_t _asciimap[128] =
+{
+ 0x00, // NUL
+ 0x00, // SOH
+ 0x00, // STX
+ 0x00, // ETX
+ 0x00, // EOT
+ 0x00, // ENQ
+ 0x00, // ACK
+ 0x00, // BEL
+ 0x2a, // BS Backspace
+ 0x2b, // TAB Tab
+ 0x28, // LF Enter
+ 0x00, // VT
+ 0x00, // FF
+ 0x00, // CR
+ 0x00, // SO
+ 0x00, // SI
+ 0x00, // DEL
+ 0x00, // DC1
+ 0x00, // DC2
+ 0x00, // DC3
+ 0x00, // DC4
+ 0x00, // NAK
+ 0x00, // SYN
+ 0x00, // ETB
+ 0x00, // CAN
+ 0x00, // EM
+ 0x00, // SUB
+ 0x00, // ESC
+ 0x00, // FS
+ 0x00, // GS
+ 0x00, // RS
+ 0x00, // US
+
+ 0x2c, // ' '
+ 0x1e|SHIFT, // !
+ 0x34|SHIFT, // "
+ 0x20|SHIFT, // #
+ 0x21|SHIFT, // $
+ 0x22|SHIFT, // %
+ 0x24|SHIFT, // &
+ 0x34, // '
+ 0x26|SHIFT, // (
+ 0x27|SHIFT, // )
+ 0x25|SHIFT, // *
+ 0x2e|SHIFT, // +
+ 0x36, // ,
+ 0x2d, // -
+ 0x37, // .
+ 0x38, // /
+ 0x27, // 0
+ 0x1e, // 1
+ 0x1f, // 2
+ 0x20, // 3
+ 0x21, // 4
+ 0x22, // 5
+ 0x23, // 6
+ 0x24, // 7
+ 0x25, // 8
+ 0x26, // 9
+ 0x33|SHIFT, // :
+ 0x33, // ;
+ 0x36|SHIFT, // <
+ 0x2e, // =
+ 0x37|SHIFT, // >
+ 0x38|SHIFT, // ?
+ 0x1f|SHIFT, // @
+ 0x04|SHIFT, // A
+ 0x05|SHIFT, // B
+ 0x06|SHIFT, // C
+ 0x07|SHIFT, // D
+ 0x08|SHIFT, // E
+ 0x09|SHIFT, // F
+ 0x0a|SHIFT, // G
+ 0x0b|SHIFT, // H
+ 0x0c|SHIFT, // I
+ 0x0d|SHIFT, // J
+ 0x0e|SHIFT, // K
+ 0x0f|SHIFT, // L
+ 0x10|SHIFT, // M
+ 0x11|SHIFT, // N
+ 0x12|SHIFT, // O
+ 0x13|SHIFT, // P
+ 0x14|SHIFT, // Q
+ 0x15|SHIFT, // R
+ 0x16|SHIFT, // S
+ 0x17|SHIFT, // T
+ 0x18|SHIFT, // U
+ 0x19|SHIFT, // V
+ 0x1a|SHIFT, // W
+ 0x1b|SHIFT, // X
+ 0x1c|SHIFT, // Y
+ 0x1d|SHIFT, // Z
+ 0x2f, // [
+ 0x31, // bslash
+ 0x30, // ]
+ 0x23|SHIFT, // ^
+ 0x2d|SHIFT, // _
+ 0x35, // `
+ 0x04, // a
+ 0x05, // b
+ 0x06, // c
+ 0x07, // d
+ 0x08, // e
+ 0x09, // f
+ 0x0a, // g
+ 0x0b, // h
+ 0x0c, // i
+ 0x0d, // j
+ 0x0e, // k
+ 0x0f, // l
+ 0x10, // m
+ 0x11, // n
+ 0x12, // o
+ 0x13, // p
+ 0x14, // q
+ 0x15, // r
+ 0x16, // s
+ 0x17, // t
+ 0x18, // u
+ 0x19, // v
+ 0x1a, // w
+ 0x1b, // x
+ 0x1c, // y
+ 0x1d, // z
+ 0x2f|SHIFT, //
+ 0x31|SHIFT, // |
+ 0x30|SHIFT, // }
+ 0x35|SHIFT, // ~
+ 0 // DEL
+};
+
+uint8_t USBPutChar(uint8_t c);
+
+// press() adds the specified key (printing, non-printing, or modifier)
+// to the persistent key report and sends the report. Because of the way
+// USB HID works, the host acts like the key remains pressed until we
+// call release(), releaseAll(), or otherwise clear the report and resend.
+size_t Keyboard_::press(uint8_t k)
+{
+ uint8_t i;
+ if (k >= 136) { // it's a non-printing key (not a modifier)
+ k = k - 136;
+ } else if (k >= 128) { // it's a modifier key
+ _keyReport.modifiers |= (1<<(k-128));
+ k = 0;
+ } else { // it's a printing key
+ k = pgm_read_byte(_asciimap + k);
+ if (!k) {
+ setWriteError();
+ return 0;
+ }
+ if (k & 0x80) { // it's a capital letter or other character reached with shift
+ _keyReport.modifiers |= 0x02; // the left shift modifier
+ k &= 0x7F;
+ }
+ }
+
+ // Add k to the key report only if it's not already present
+ // and if there is an empty slot.
+ if (_keyReport.keys[0] != k && _keyReport.keys[1] != k &&
+ _keyReport.keys[2] != k && _keyReport.keys[3] != k &&
+ _keyReport.keys[4] != k && _keyReport.keys[5] != k) {
+
+ for (i=0; i<6; i++) {
+ if (_keyReport.keys[i] == 0x00) {
+ _keyReport.keys[i] = k;
+ break;
+ }
+ }
+ if (i == 6) {
+ setWriteError();
+ return 0;
+ }
+ }
+ sendReport(&_keyReport);
+ return 1;
+}
+
+// release() takes the specified key out of the persistent key report and
+// sends the report. This tells the OS the key is no longer pressed and that
+// it shouldn't be repeated any more.
+size_t Keyboard_::release(uint8_t k)
+{
+ uint8_t i;
+ if (k >= 136) { // it's a non-printing key (not a modifier)
+ k = k - 136;
+ } else if (k >= 128) { // it's a modifier key
+ _keyReport.modifiers &= ~(1<<(k-128));
+ k = 0;
+ } else { // it's a printing key
+ k = pgm_read_byte(_asciimap + k);
+ if (!k) {
+ return 0;
+ }
+ if (k & 0x80) { // it's a capital letter or other character reached with shift
+ _keyReport.modifiers &= ~(0x02); // the left shift modifier
+ k &= 0x7F;
+ }
+ }
+
+ // Test the key report to see if k is present. Clear it if it exists.
+ // Check all positions in case the key is present more than once (which it shouldn't be)
+ for (i=0; i<6; i++) {
+ if (0 != k && _keyReport.keys[i] == k) {
+ _keyReport.keys[i] = 0x00;
+ }
+ }
+
+ sendReport(&_keyReport);
+ return 1;
+}
+
+void Keyboard_::releaseAll(void)
+{
+ _keyReport.keys[0] = 0;
+ _keyReport.keys[1] = 0;
+ _keyReport.keys[2] = 0;
+ _keyReport.keys[3] = 0;
+ _keyReport.keys[4] = 0;
+ _keyReport.keys[5] = 0;
+ _keyReport.modifiers = 0;
+ sendReport(&_keyReport);
+}
+
+size_t Keyboard_::write(uint8_t c)
+{
+ uint8_t p = press(c); // Keydown
+ uint8_t r = release(c); // Keyup
+ return (p); // just return the result of press() since release() almost always returns 1
+}
+
+#endif
+
+#endif /* if defined(USBCON) */ \ No newline at end of file
diff --git a/cores/robot/HardwareSerial.cpp b/cores/robot/HardwareSerial.cpp
new file mode 100644
index 0000000..eb2365f
--- /dev/null
+++ b/cores/robot/HardwareSerial.cpp
@@ -0,0 +1,508 @@
+/*
+ HardwareSerial.cpp - Hardware serial library for Wiring
+ Copyright (c) 2006 Nicholas Zambetti. All right reserved.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+ Modified 23 November 2006 by David A. Mellis
+ Modified 28 September 2010 by Mark Sproul
+ Modified 14 August 2012 by Alarus
+*/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <inttypes.h>
+#include "Arduino.h"
+#include "wiring_private.h"
+
+// this next line disables the entire HardwareSerial.cpp,
+// this is so I can support Attiny series and any other chip without a uart
+#if defined(UBRRH) || defined(UBRR0H) || defined(UBRR1H) || defined(UBRR2H) || defined(UBRR3H)
+
+#include "HardwareSerial.h"
+
+/*
+ * on ATmega8, the uart and its bits are not numbered, so there is no "TXC0"
+ * definition.
+ */
+#if !defined(TXC0)
+#if defined(TXC)
+#define TXC0 TXC
+#elif defined(TXC1)
+// Some devices have uart1 but no uart0
+#define TXC0 TXC1
+#else
+#error TXC0 not definable in HardwareSerial.h
+#endif
+#endif
+
+// Define constants and variables for buffering incoming serial data. We're
+// using a ring buffer (I think), in which head is the index of the location
+// to which to write the next incoming character and tail is the index of the
+// location from which to read.
+#if (RAMEND < 1000)
+ #define SERIAL_BUFFER_SIZE 16
+#else
+ #define SERIAL_BUFFER_SIZE 64
+#endif
+
+struct ring_buffer
+{
+ unsigned char buffer[SERIAL_BUFFER_SIZE];
+ volatile unsigned int head;
+ volatile unsigned int tail;
+};
+
+#if defined(USBCON)
+ ring_buffer rx_buffer = { { 0 }, 0, 0};
+ ring_buffer tx_buffer = { { 0 }, 0, 0};
+#endif
+#if defined(UBRRH) || defined(UBRR0H)
+ ring_buffer rx_buffer = { { 0 }, 0, 0 };
+ ring_buffer tx_buffer = { { 0 }, 0, 0 };
+#endif
+#if defined(UBRR1H)
+ ring_buffer rx_buffer1 = { { 0 }, 0, 0 };
+ ring_buffer tx_buffer1 = { { 0 }, 0, 0 };
+#endif
+#if defined(UBRR2H)
+ ring_buffer rx_buffer2 = { { 0 }, 0, 0 };
+ ring_buffer tx_buffer2 = { { 0 }, 0, 0 };
+#endif
+#if defined(UBRR3H)
+ ring_buffer rx_buffer3 = { { 0 }, 0, 0 };
+ ring_buffer tx_buffer3 = { { 0 }, 0, 0 };
+#endif
+
+inline void store_char(unsigned char c, ring_buffer *buffer)
+{
+ int i = (unsigned int)(buffer->head + 1) % SERIAL_BUFFER_SIZE;
+
+ // if we should be storing the received character into the location
+ // just before the tail (meaning that the head would advance to the
+ // current location of the tail), we're about to overflow the buffer
+ // and so we don't write the character or advance the head.
+ if (i != buffer->tail) {
+ buffer->buffer[buffer->head] = c;
+ buffer->head = i;
+ }
+}
+
+#if !defined(USART0_RX_vect) && defined(USART1_RX_vect)
+// do nothing - on the 32u4 the first USART is USART1
+#else
+#if !defined(USART_RX_vect) && !defined(USART0_RX_vect) && \
+ !defined(USART_RXC_vect)
+ #error "Don't know what the Data Received vector is called for the first UART"
+#else
+ void serialEvent() __attribute__((weak));
+ void serialEvent() {}
+ #define serialEvent_implemented
+#if defined(USART_RX_vect)
+ ISR(USART_RX_vect)
+#elif defined(USART0_RX_vect)
+ ISR(USART0_RX_vect)
+#elif defined(USART_RXC_vect)
+ ISR(USART_RXC_vect) // ATmega8
+#endif
+ {
+ #if defined(UDR0)
+ if (bit_is_clear(UCSR0A, UPE0)) {
+ unsigned char c = UDR0;
+ store_char(c, &rx_buffer);
+ } else {
+ unsigned char c = UDR0;
+ };
+ #elif defined(UDR)
+ if (bit_is_clear(UCSRA, PE)) {
+ unsigned char c = UDR;
+ store_char(c, &rx_buffer);
+ } else {
+ unsigned char c = UDR;
+ };
+ #else
+ #error UDR not defined
+ #endif
+ }
+#endif
+#endif
+
+#if defined(USART1_RX_vect)
+ void serialEvent1() __attribute__((weak));
+ void serialEvent1() {}
+ #define serialEvent1_implemented
+ ISR(USART1_RX_vect)
+ {
+ if (bit_is_clear(UCSR1A, UPE1)) {
+ unsigned char c = UDR1;
+ store_char(c, &rx_buffer1);
+ } else {
+ unsigned char c = UDR1;
+ };
+ }
+#endif
+
+#if defined(USART2_RX_vect) && defined(UDR2)
+ void serialEvent2() __attribute__((weak));
+ void serialEvent2() {}
+ #define serialEvent2_implemented
+ ISR(USART2_RX_vect)
+ {
+ if (bit_is_clear(UCSR2A, UPE2)) {
+ unsigned char c = UDR2;
+ store_char(c, &rx_buffer2);
+ } else {
+ unsigned char c = UDR2;
+ };
+ }
+#endif
+
+#if defined(USART3_RX_vect) && defined(UDR3)
+ void serialEvent3() __attribute__((weak));
+ void serialEvent3() {}
+ #define serialEvent3_implemented
+ ISR(USART3_RX_vect)
+ {
+ if (bit_is_clear(UCSR3A, UPE3)) {
+ unsigned char c = UDR3;
+ store_char(c, &rx_buffer3);
+ } else {
+ unsigned char c = UDR3;
+ };
+ }
+#endif
+
+void serialEventRun(void)
+{
+#ifdef serialEvent_implemented
+ if (Serial.available()) serialEvent();
+#endif
+#ifdef serialEvent1_implemented
+ if (Serial1.available()) serialEvent1();
+#endif
+#ifdef serialEvent2_implemented
+ if (Serial2.available()) serialEvent2();
+#endif
+#ifdef serialEvent3_implemented
+ if (Serial3.available()) serialEvent3();
+#endif
+}
+
+
+#if !defined(USART0_UDRE_vect) && defined(USART1_UDRE_vect)
+// do nothing - on the 32u4 the first USART is USART1
+#else
+#if !defined(UART0_UDRE_vect) && !defined(UART_UDRE_vect) && !defined(USART0_UDRE_vect) && !defined(USART_UDRE_vect)
+ #error "Don't know what the Data Register Empty vector is called for the first UART"
+#else
+#if defined(UART0_UDRE_vect)
+ISR(UART0_UDRE_vect)
+#elif defined(UART_UDRE_vect)
+ISR(UART_UDRE_vect)
+#elif defined(USART0_UDRE_vect)
+ISR(USART0_UDRE_vect)
+#elif defined(USART_UDRE_vect)
+ISR(USART_UDRE_vect)
+#endif
+{
+ if (tx_buffer.head == tx_buffer.tail) {
+ // Buffer empty, so disable interrupts
+#if defined(UCSR0B)
+ cbi(UCSR0B, UDRIE0);
+#else
+ cbi(UCSRB, UDRIE);
+#endif
+ }
+ else {
+ // There is more data in the output buffer. Send the next byte
+ unsigned char c = tx_buffer.buffer[tx_buffer.tail];
+ tx_buffer.tail = (tx_buffer.tail + 1) % SERIAL_BUFFER_SIZE;
+
+ #if defined(UDR0)
+ UDR0 = c;
+ #elif defined(UDR)
+ UDR = c;
+ #else
+ #error UDR not defined
+ #endif
+ }
+}
+#endif
+#endif
+
+#ifdef USART1_UDRE_vect
+ISR(USART1_UDRE_vect)
+{
+ if (tx_buffer1.head == tx_buffer1.tail) {
+ // Buffer empty, so disable interrupts
+ cbi(UCSR1B, UDRIE1);
+ }
+ else {
+ // There is more data in the output buffer. Send the next byte
+ unsigned char c = tx_buffer1.buffer[tx_buffer1.tail];
+ tx_buffer1.tail = (tx_buffer1.tail + 1) % SERIAL_BUFFER_SIZE;
+
+ UDR1 = c;
+ }
+}
+#endif
+
+#ifdef USART2_UDRE_vect
+ISR(USART2_UDRE_vect)
+{
+ if (tx_buffer2.head == tx_buffer2.tail) {
+ // Buffer empty, so disable interrupts
+ cbi(UCSR2B, UDRIE2);
+ }
+ else {
+ // There is more data in the output buffer. Send the next byte
+ unsigned char c = tx_buffer2.buffer[tx_buffer2.tail];
+ tx_buffer2.tail = (tx_buffer2.tail + 1) % SERIAL_BUFFER_SIZE;
+
+ UDR2 = c;
+ }
+}
+#endif
+
+#ifdef USART3_UDRE_vect
+ISR(USART3_UDRE_vect)
+{
+ if (tx_buffer3.head == tx_buffer3.tail) {
+ // Buffer empty, so disable interrupts
+ cbi(UCSR3B, UDRIE3);
+ }
+ else {
+ // There is more data in the output buffer. Send the next byte
+ unsigned char c = tx_buffer3.buffer[tx_buffer3.tail];
+ tx_buffer3.tail = (tx_buffer3.tail + 1) % SERIAL_BUFFER_SIZE;
+
+ UDR3 = c;
+ }
+}
+#endif
+
+
+// Constructors ////////////////////////////////////////////////////////////////
+
+HardwareSerial::HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
+ volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
+ volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
+ volatile uint8_t *ucsrc, volatile uint8_t *udr,
+ uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x)
+{
+ _rx_buffer = rx_buffer;
+ _tx_buffer = tx_buffer;
+ _ubrrh = ubrrh;
+ _ubrrl = ubrrl;
+ _ucsra = ucsra;
+ _ucsrb = ucsrb;
+ _ucsrc = ucsrc;
+ _udr = udr;
+ _rxen = rxen;
+ _txen = txen;
+ _rxcie = rxcie;
+ _udrie = udrie;
+ _u2x = u2x;
+}
+
+// Public Methods //////////////////////////////////////////////////////////////
+
+void HardwareSerial::begin(unsigned long baud)
+{
+ uint16_t baud_setting;
+ bool use_u2x = true;
+
+#if F_CPU == 16000000UL
+ // hardcoded exception for compatibility with the bootloader shipped
+ // with the Duemilanove and previous boards and the firmware on the 8U2
+ // on the Uno and Mega 2560.
+ if (baud == 57600) {
+ use_u2x = false;
+ }
+#endif
+
+try_again:
+
+ if (use_u2x) {
+ *_ucsra = 1 << _u2x;
+ baud_setting = (F_CPU / 4 / baud - 1) / 2;
+ } else {
+ *_ucsra = 0;
+ baud_setting = (F_CPU / 8 / baud - 1) / 2;
+ }
+
+ if ((baud_setting > 4095) && use_u2x)
+ {
+ use_u2x = false;
+ goto try_again;
+ }
+
+ // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
+ *_ubrrh = baud_setting >> 8;
+ *_ubrrl = baud_setting;
+
+ transmitting = false;
+
+ sbi(*_ucsrb, _rxen);
+ sbi(*_ucsrb, _txen);
+ sbi(*_ucsrb, _rxcie);
+ cbi(*_ucsrb, _udrie);
+}
+
+void HardwareSerial::begin(unsigned long baud, byte config)
+{
+ uint16_t baud_setting;
+ uint8_t current_config;
+ bool use_u2x = true;
+
+#if F_CPU == 16000000UL
+ // hardcoded exception for compatibility with the bootloader shipped
+ // with the Duemilanove and previous boards and the firmware on the 8U2
+ // on the Uno and Mega 2560.
+ if (baud == 57600) {
+ use_u2x = false;
+ }
+#endif
+
+try_again:
+
+ if (use_u2x) {
+ *_ucsra = 1 << _u2x;
+ baud_setting = (F_CPU / 4 / baud - 1) / 2;
+ } else {
+ *_ucsra = 0;
+ baud_setting = (F_CPU / 8 / baud - 1) / 2;
+ }
+
+ if ((baud_setting > 4095) && use_u2x)
+ {
+ use_u2x = false;
+ goto try_again;
+ }
+
+ // assign the baud_setting, a.k.a. ubbr (USART Baud Rate Register)
+ *_ubrrh = baud_setting >> 8;
+ *_ubrrl = baud_setting;
+
+ //set the data bits, parity, and stop bits
+#if defined(__AVR_ATmega8__)
+ config |= 0x80; // select UCSRC register (shared with UBRRH)
+#endif
+ *_ucsrc = config;
+
+ sbi(*_ucsrb, _rxen);
+ sbi(*_ucsrb, _txen);
+ sbi(*_ucsrb, _rxcie);
+ cbi(*_ucsrb, _udrie);
+}
+
+void HardwareSerial::end()
+{
+ // wait for transmission of outgoing data
+ while (_tx_buffer->head != _tx_buffer->tail)
+ ;
+
+ cbi(*_ucsrb, _rxen);
+ cbi(*_ucsrb, _txen);
+ cbi(*_ucsrb, _rxcie);
+ cbi(*_ucsrb, _udrie);
+
+ // clear any received data
+ _rx_buffer->head = _rx_buffer->tail;
+}
+
+int HardwareSerial::available(void)
+{
+ return (unsigned int)(SERIAL_BUFFER_SIZE + _rx_buffer->head - _rx_buffer->tail) % SERIAL_BUFFER_SIZE;
+}
+
+int HardwareSerial::peek(void)
+{
+ if (_rx_buffer->head == _rx_buffer->tail) {
+ return -1;
+ } else {
+ return _rx_buffer->buffer[_rx_buffer->tail];
+ }
+}
+
+int HardwareSerial::read(void)
+{
+ // if the head isn't ahead of the tail, we don't have any characters
+ if (_rx_buffer->head == _rx_buffer->tail) {
+ return -1;
+ } else {
+ unsigned char c = _rx_buffer->buffer[_rx_buffer->tail];
+ _rx_buffer->tail = (unsigned int)(_rx_buffer->tail + 1) % SERIAL_BUFFER_SIZE;
+ return c;
+ }
+}
+
+void HardwareSerial::flush()
+{
+ // UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT
+ while (transmitting && ! (*_ucsra & _BV(TXC0)));
+ transmitting = false;
+}
+
+size_t HardwareSerial::write(uint8_t c)
+{
+ int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
+
+ // If the output buffer is full, there's nothing for it other than to
+ // wait for the interrupt handler to empty it a bit
+ // ???: return 0 here instead?
+ while (i == _tx_buffer->tail)
+ ;
+
+ _tx_buffer->buffer[_tx_buffer->head] = c;
+ _tx_buffer->head = i;
+
+ sbi(*_ucsrb, _udrie);
+ // clear the TXC bit -- "can be cleared by writing a one to its bit location"
+ transmitting = true;
+ sbi(*_ucsra, TXC0);
+
+ return 1;
+}
+
+HardwareSerial::operator bool() {
+ return true;
+}
+
+// Preinstantiate Objects //////////////////////////////////////////////////////
+
+#if defined(UBRRH) && defined(UBRRL)
+ HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR, RXEN, TXEN, RXCIE, UDRIE, U2X);
+#elif defined(UBRR0H) && defined(UBRR0L)
+ HardwareSerial Serial(&rx_buffer, &tx_buffer, &UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0, RXEN0, TXEN0, RXCIE0, UDRIE0, U2X0);
+#elif defined(USBCON)
+ // do nothing - Serial object and buffers are initialized in CDC code
+#else
+ #error no serial port defined (port 0)
+#endif
+
+#if defined(UBRR1H)
+ HardwareSerial Serial1(&rx_buffer1, &tx_buffer1, &UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1, RXEN1, TXEN1, RXCIE1, UDRIE1, U2X1);
+#endif
+#if defined(UBRR2H)
+ HardwareSerial Serial2(&rx_buffer2, &tx_buffer2, &UBRR2H, &UBRR2L, &UCSR2A, &UCSR2B, &UCSR2C, &UDR2, RXEN2, TXEN2, RXCIE2, UDRIE2, U2X2);
+#endif
+#if defined(UBRR3H)
+ HardwareSerial Serial3(&rx_buffer3, &tx_buffer3, &UBRR3H, &UBRR3L, &UCSR3A, &UCSR3B, &UCSR3C, &UDR3, RXEN3, TXEN3, RXCIE3, UDRIE3, U2X3);
+#endif
+
+#endif // whole file
+
diff --git a/cores/robot/HardwareSerial.h b/cores/robot/HardwareSerial.h
new file mode 100644
index 0000000..a73117f
--- /dev/null
+++ b/cores/robot/HardwareSerial.h
@@ -0,0 +1,115 @@
+/*
+ HardwareSerial.h - Hardware serial library for Wiring
+ Copyright (c) 2006 Nicholas Zambetti. All right reserved.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+ Modified 28 September 2010 by Mark Sproul
+ Modified 14 August 2012 by Alarus
+*/
+
+#ifndef HardwareSerial_h
+#define HardwareSerial_h
+
+#include <inttypes.h>
+
+#include "Stream.h"
+
+struct ring_buffer;
+
+class HardwareSerial : public Stream
+{
+ private:
+ ring_buffer *_rx_buffer;
+ ring_buffer *_tx_buffer;
+ volatile uint8_t *_ubrrh;
+ volatile uint8_t *_ubrrl;
+ volatile uint8_t *_ucsra;
+ volatile uint8_t *_ucsrb;
+ volatile uint8_t *_ucsrc;
+ volatile uint8_t *_udr;
+ uint8_t _rxen;
+ uint8_t _txen;
+ uint8_t _rxcie;
+ uint8_t _udrie;
+ uint8_t _u2x;
+ bool transmitting;
+ public:
+ HardwareSerial(ring_buffer *rx_buffer, ring_buffer *tx_buffer,
+ volatile uint8_t *ubrrh, volatile uint8_t *ubrrl,
+ volatile uint8_t *ucsra, volatile uint8_t *ucsrb,
+ volatile uint8_t *ucsrc, volatile uint8_t *udr,
+ uint8_t rxen, uint8_t txen, uint8_t rxcie, uint8_t udrie, uint8_t u2x);
+ void begin(unsigned long);
+ void begin(unsigned long, uint8_t);
+ void end();
+ virtual int available(void);
+ virtual int peek(void);
+ virtual int read(void);
+ virtual void flush(void);
+ virtual size_t write(uint8_t);
+ inline size_t write(unsigned long n) { return write((uint8_t)n); }
+ inline size_t write(long n) { return write((uint8_t)n); }
+ inline size_t write(unsigned int n) { return write((uint8_t)n); }
+ inline size_t write(int n) { return write((uint8_t)n); }
+ using Print::write; // pull in write(str) and write(buf, size) from Print
+ operator bool();
+};
+
+// Define config for Serial.begin(baud, config);
+#define SERIAL_5N1 0x00
+#define SERIAL_6N1 0x02
+#define SERIAL_7N1 0x04
+#define SERIAL_8N1 0x06
+#define SERIAL_5N2 0x08
+#define SERIAL_6N2 0x0A
+#define SERIAL_7N2 0x0C
+#define SERIAL_8N2 0x0E
+#define SERIAL_5E1 0x20
+#define SERIAL_6E1 0x22
+#define SERIAL_7E1 0x24
+#define SERIAL_8E1 0x26
+#define SERIAL_5E2 0x28
+#define SERIAL_6E2 0x2A
+#define SERIAL_7E2 0x2C
+#define SERIAL_8E2 0x2E
+#define SERIAL_5O1 0x30
+#define SERIAL_6O1 0x32
+#define SERIAL_7O1 0x34
+#define SERIAL_8O1 0x36
+#define SERIAL_5O2 0x38
+#define SERIAL_6O2 0x3A
+#define SERIAL_7O2 0x3C
+#define SERIAL_8O2 0x3E
+
+#if defined(UBRRH) || defined(UBRR0H)
+ extern HardwareSerial Serial;
+#elif defined(USBCON)
+ #include "USBAPI.h"
+// extern HardwareSerial Serial_;
+#endif
+#if defined(UBRR1H)
+ extern HardwareSerial Serial1;
+#endif
+#if defined(UBRR2H)
+ extern HardwareSerial Serial2;
+#endif
+#if defined(UBRR3H)
+ extern HardwareSerial Serial3;
+#endif
+
+extern void serialEventRun(void) __attribute__((weak));
+
+#endif
diff --git a/cores/robot/IPAddress.cpp b/cores/robot/IPAddress.cpp
new file mode 100644
index 0000000..fe3deb7
--- /dev/null
+++ b/cores/robot/IPAddress.cpp
@@ -0,0 +1,56 @@
+
+#include <Arduino.h>
+#include <IPAddress.h>
+
+IPAddress::IPAddress()
+{
+ memset(_address, 0, sizeof(_address));
+}
+
+IPAddress::IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet)
+{
+ _address[0] = first_octet;
+ _address[1] = second_octet;
+ _address[2] = third_octet;
+ _address[3] = fourth_octet;
+}
+
+IPAddress::IPAddress(uint32_t address)
+{
+ memcpy(_address, &address, sizeof(_address));
+}
+
+IPAddress::IPAddress(const uint8_t *address)
+{
+ memcpy(_address, address, sizeof(_address));
+}
+
+IPAddress& IPAddress::operator=(const uint8_t *address)
+{
+ memcpy(_address, address, sizeof(_address));
+ return *this;
+}
+
+IPAddress& IPAddress::operator=(uint32_t address)
+{
+ memcpy(_address, (const uint8_t *)&address, sizeof(_address));
+ return *this;
+}
+
+bool IPAddress::operator==(const uint8_t* addr)
+{
+ return memcmp(addr, _address, sizeof(_address)) == 0;
+}
+
+size_t IPAddress::printTo(Print& p) const
+{
+ size_t n = 0;
+ for (int i =0; i < 3; i++)
+ {
+ n += p.print(_address[i], DEC);
+ n += p.print('.');
+ }
+ n += p.print(_address[3], DEC);
+ return n;
+}
+
diff --git a/cores/robot/IPAddress.h b/cores/robot/IPAddress.h
new file mode 100644
index 0000000..2585aec
--- /dev/null
+++ b/cores/robot/IPAddress.h
@@ -0,0 +1,76 @@
+/*
+ *
+ * MIT License:
+ * Copyright (c) 2011 Adrian McEwen
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * adrianm@mcqn.com 1/1/2011
+ */
+
+#ifndef IPAddress_h
+#define IPAddress_h
+
+#include <Printable.h>
+
+// A class to make it easier to handle and pass around IP addresses
+
+class IPAddress : public Printable {
+private:
+ uint8_t _address[4]; // IPv4 address
+ // Access the raw byte array containing the address. Because this returns a pointer
+ // to the internal structure rather than a copy of the address this function should only
+ // be used when you know that the usage of the returned uint8_t* will be transient and not
+ // stored.
+ uint8_t* raw_address() { return _address; };
+
+public:
+ // Constructors
+ IPAddress();
+ IPAddress(uint8_t first_octet, uint8_t second_octet, uint8_t third_octet, uint8_t fourth_octet);
+ IPAddress(uint32_t address);
+ IPAddress(const uint8_t *address);
+
+ // Overloaded cast operator to allow IPAddress objects to be used where a pointer
+ // to a four-byte uint8_t array is expected
+ operator uint32_t() { return *((uint32_t*)_address); };
+ bool operator==(const IPAddress& addr) { return (*((uint32_t*)_address)) == (*((uint32_t*)addr._address)); };
+ bool operator==(const uint8_t* addr);
+
+ // Overloaded index operator to allow getting and setting individual octets of the address
+ uint8_t operator[](int index) const { return _address[index]; };
+ uint8_t& operator[](int index) { return _address[index]; };
+
+ // Overloaded copy operators to allow initialisation of IPAddress objects from other types
+ IPAddress& operator=(const uint8_t *address);
+ IPAddress& operator=(uint32_t address);
+
+ virtual size_t printTo(Print& p) const;
+
+ friend class EthernetClass;
+ friend class UDP;
+ friend class Client;
+ friend class Server;
+ friend class DhcpClass;
+ friend class DNSClient;
+};
+
+const IPAddress INADDR_NONE(0,0,0,0);
+
+
+#endif
diff --git a/cores/robot/Platform.h b/cores/robot/Platform.h
new file mode 100644
index 0000000..8b8f742
--- /dev/null
+++ b/cores/robot/Platform.h
@@ -0,0 +1,23 @@
+
+#ifndef __PLATFORM_H__
+#define __PLATFORM_H__
+
+#include <inttypes.h>
+#include <avr/pgmspace.h>
+#include <avr/eeprom.h>
+#include <avr/interrupt.h>
+#include <util/delay.h>
+
+typedef unsigned char u8;
+typedef unsigned short u16;
+typedef unsigned long u32;
+
+#include "Arduino.h"
+
+#if defined(USBCON)
+ #include "USBDesc.h"
+ #include "USBCore.h"
+ #include "USBAPI.h"
+#endif /* if defined(USBCON) */
+
+#endif
diff --git a/cores/robot/Print.cpp b/cores/robot/Print.cpp
new file mode 100755
index 0000000..53961ec
--- /dev/null
+++ b/cores/robot/Print.cpp
@@ -0,0 +1,268 @@
+/*
+ Print.cpp - Base class that provides print() and println()
+ Copyright (c) 2008 David A. Mellis. All right reserved.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+ Modified 23 November 2006 by David A. Mellis
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <math.h>
+#include "Arduino.h"
+
+#include "Print.h"
+
+// Public Methods //////////////////////////////////////////////////////////////
+
+/* default implementation: may be overridden */
+size_t Print::write(const uint8_t *buffer, size_t size)
+{
+ size_t n = 0;
+ while (size--) {
+ n += write(*buffer++);
+ }
+ return n;
+}
+
+size_t Print::print(const __FlashStringHelper *ifsh)
+{
+ const char PROGMEM *p = (const char PROGMEM *)ifsh;
+ size_t n = 0;
+ while (1) {
+ unsigned char c = pgm_read_byte(p++);
+ if (c == 0) break;
+ n += write(c);
+ }
+ return n;
+}
+
+size_t Print::print(const String &s)
+{
+ size_t n = 0;
+ for (uint16_t i = 0; i < s.length(); i++) {
+ n += write(s[i]);
+ }
+ return n;
+}
+
+size_t Print::print(const char str[])
+{
+ return write(str);
+}
+
+size_t Print::print(char c)
+{
+ return write(c);
+}
+
+size_t Print::print(unsigned char b, int base)
+{
+ return print((unsigned long) b, base);
+}
+
+size_t Print::print(int n, int base)
+{
+ return print((long) n, base);
+}
+
+size_t Print::print(unsigned int n, int base)
+{
+ return print((unsigned long) n, base);
+}
+
+size_t Print::print(long n, int base)
+{
+ if (base == 0) {
+ return write(n);
+ } else if (base == 10) {
+ if (n < 0) {
+ int t = print('-');
+ n = -n;
+ return printNumber(n, 10) + t;
+ }
+ return printNumber(n, 10);
+ } else {
+ return printNumber(n, base);
+ }
+}
+
+size_t Print::print(unsigned long n, int base)
+{
+ if (base == 0) return write(n);
+ else return printNumber(n, base);
+}
+
+size_t Print::print(double n, int digits)
+{
+ return printFloat(n, digits);
+}
+
+size_t Print::println(const __FlashStringHelper *ifsh)
+{
+ size_t n = print(ifsh);
+ n += println();
+ return n;
+}
+
+size_t Print::print(const Printable& x)
+{
+ return x.printTo(*this);
+}
+
+size_t Print::println(void)
+{
+ size_t n = print('\r');
+ n += print('\n');
+ return n;
+}
+
+size_t Print::println(const String &s)
+{
+ size_t n = print(s);
+ n += println();
+ return n;
+}
+
+size_t Print::println(const char c[])
+{
+ size_t n = print(c);
+ n += println();
+ return n;
+}
+
+size_t Print::println(char c)
+{
+ size_t n = print(c);
+ n += println();
+ return n;
+}
+
+size_t Print::println(unsigned char b, int base)
+{
+ size_t n = print(b, base);
+ n += println();
+ return n;
+}
+
+size_t Print::println(int num, int base)
+{
+ size_t n = print(num, base);
+ n += println();
+ return n;
+}
+
+size_t Print::println(unsigned int num, int base)
+{
+ size_t n = print(num, base);
+ n += println();
+ return n;
+}
+
+size_t Print::println(long num, int base)
+{
+ size_t n = print(num, base);
+ n += println();
+ return n;
+}
+
+size_t Print::println(unsigned long num, int base)
+{
+ size_t n = print(num, base);
+ n += println();
+ return n;
+}
+
+size_t Print::println(double num, int digits)
+{
+ size_t n = print(num, digits);
+ n += println();
+ return n;
+}
+
+size_t Print::println(const Printable& x)
+{
+ size_t n = print(x);
+ n += println();
+ return n;
+}
+
+// Private Methods /////////////////////////////////////////////////////////////
+
+size_t Print::printNumber(unsigned long n, uint8_t base) {
+ char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
+ char *str = &buf[sizeof(buf) - 1];
+
+ *str = '\0';
+
+ // prevent crash if called with base == 1
+ if (base < 2) base = 10;
+
+ do {
+ unsigned long m = n;
+ n /= base;
+ char c = m - base * n;
+ *--str = c < 10 ? c + '0' : c + 'A' - 10;
+ } while(n);
+
+ return write(str);
+}
+
+size_t Print::printFloat(double number, uint8_t digits)
+{
+ size_t n = 0;
+
+ if (isnan(number)) return print("nan");
+ if (isinf(number)) return print("inf");
+ if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
+ if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
+
+ // Handle negative numbers
+ if (number < 0.0)
+ {
+ n += print('-');
+ number = -number;
+ }
+
+ // Round correctly so that print(1.999, 2) prints as "2.00"
+ double rounding = 0.5;
+ for (uint8_t i=0; i<digits; ++i)
+ rounding /= 10.0;
+
+ number += rounding;
+
+ // Extract the integer part of the number and print it
+ unsigned long int_part = (unsigned long)number;
+ double remainder = number - (double)int_part;
+ n += print(int_part);
+
+ // Print the decimal point, but only if there are digits beyond
+ if (digits > 0) {
+ n += print(".");
+ }
+
+ // Extract digits from the remainder one at a time
+ while (digits-- > 0)
+ {
+ remainder *= 10.0;
+ int toPrint = int(remainder);
+ n += print(toPrint);
+ remainder -= toPrint;
+ }
+
+ return n;
+}
diff --git a/cores/robot/Print.h b/cores/robot/Print.h
new file mode 100755
index 0000000..dc76150
--- /dev/null
+++ b/cores/robot/Print.h
@@ -0,0 +1,81 @@
+/*
+ Print.h - Base class that provides print() and println()
+ Copyright (c) 2008 David A. Mellis. All right reserved.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#ifndef Print_h
+#define Print_h
+
+#include <inttypes.h>
+#include <stdio.h> // for size_t
+
+#include "WString.h"
+#include "Printable.h"
+
+#define DEC 10
+#define HEX 16
+#define OCT 8
+#define BIN 2
+
+class Print
+{
+ private:
+ int write_error;
+ size_t printNumber(unsigned long, uint8_t);
+ size_t printFloat(double, uint8_t);
+ protected:
+ void setWriteError(int err = 1) { write_error = err; }
+ public:
+ Print() : write_error(0) {}
+
+ int getWriteError() { return write_error; }
+ void clearWriteError() { setWriteError(0); }
+
+ virtual size_t write(uint8_t) = 0;
+ size_t write(const char *str) {
+ if (str == NULL) return 0;
+ return write((const uint8_t *)str, strlen(str));
+ }
+ virtual size_t write(const uint8_t *buffer, size_t size);
+
+ size_t print(const __FlashStringHelper *);
+ size_t print(const String &);
+ size_t print(const char[]);
+ size_t print(char);
+ size_t print(unsigned char, int = DEC);
+ size_t print(int, int = DEC);
+ size_t print(unsigned int, int = DEC);
+ size_t print(long, int = DEC);
+ size_t print(unsigned long, int = DEC);
+ size_t print(double, int = 2);
+ size_t print(const Printable&);
+
+ size_t println(const __FlashStringHelper *);
+ size_t println(const String &s);
+ size_t println(const char[]);
+ size_t println(char);
+ size_t println(unsigned char, int = DEC);
+ size_t println(int, int = DEC);
+ size_t println(unsigned int, int = DEC);
+ size_t println(long, int = DEC);
+ size_t println(unsigned long, int = DEC);
+ size_t println(double, int = 2);
+ size_t println(const Printable&);
+ size_t println(void);
+};
+
+#endif
diff --git a/cores/robot/Printable.h b/cores/robot/Printable.h
new file mode 100644
index 0000000..d03c9af
--- /dev/null
+++ b/cores/robot/Printable.h
@@ -0,0 +1,40 @@
+/*
+ Printable.h - Interface class that allows printing of complex types
+ Copyright (c) 2011 Adrian McEwen. All right reserved.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#ifndef Printable_h
+#define Printable_h
+
+#include <new.h>
+
+class Print;
+
+/** The Printable class provides a way for new classes to allow themselves to be printed.
+ By deriving from Printable and implementing the printTo method, it will then be possible
+ for users to print out instances of this class by passing them into the usual
+ Print::print and Print::println methods.
+*/
+
+class Printable
+{
+ public:
+ virtual size_t printTo(Print& p) const = 0;
+};
+
+#endif
+
diff --git a/cores/robot/Server.h b/cores/robot/Server.h
new file mode 100644
index 0000000..9674c76
--- /dev/null
+++ b/cores/robot/Server.h
@@ -0,0 +1,9 @@
+#ifndef server_h
+#define server_h
+
+class Server : public Print {
+public:
+ virtual void begin() =0;
+};
+
+#endif
diff --git a/cores/robot/Stream.cpp b/cores/robot/Stream.cpp
new file mode 100644
index 0000000..aafb7fc
--- /dev/null
+++ b/cores/robot/Stream.cpp
@@ -0,0 +1,270 @@
+/*
+ Stream.cpp - adds parsing methods to Stream class
+ Copyright (c) 2008 David A. Mellis. All right reserved.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+ Created July 2011
+ parsing functions based on TextFinder library by Michael Margolis
+ */
+
+#include "Arduino.h"
+#include "Stream.h"
+
+#define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
+#define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
+
+// private method to read stream with timeout
+int Stream::timedRead()
+{
+ int c;
+ _startMillis = millis();
+ do {
+ c = read();
+ if (c >= 0) return c;
+ } while(millis() - _startMillis < _timeout);
+ return -1; // -1 indicates timeout
+}
+
+// private method to peek stream with timeout
+int Stream::timedPeek()
+{
+ int c;
+ _startMillis = millis();
+ do {
+ c = peek();
+ if (c >= 0) return c;
+ } while(millis() - _startMillis < _timeout);
+ return -1; // -1 indicates timeout
+}
+
+// returns peek of the next digit in the stream or -1 if timeout
+// discards non-numeric characters
+int Stream::peekNextDigit()
+{
+ int c;
+ while (1) {
+ c = timedPeek();
+ if (c < 0) return c; // timeout
+ if (c == '-') return c;
+ if (c >= '0' && c <= '9') return c;
+ read(); // discard non-numeric
+ }
+}
+
+// Public Methods
+//////////////////////////////////////////////////////////////
+
+void Stream::setTimeout(unsigned long timeout) // sets the maximum number of milliseconds to wait
+{
+ _timeout = timeout;
+}
+
+ // find returns true if the target string is found
+bool Stream::find(char *target)
+{
+ return findUntil(target, NULL);
+}
+
+// reads data from the stream until the target string of given length is found
+// returns true if target string is found, false if timed out
+bool Stream::find(char *target, size_t length)
+{
+ return findUntil(target, length, NULL, 0);
+}
+
+// as find but search ends if the terminator string is found
+bool Stream::findUntil(char *target, char *terminator)
+{
+ return findUntil(target, strlen(target), terminator, strlen(terminator));
+}
+
+// reads data from the stream until the target string of the given length is found
+// search terminated if the terminator string is found
+// returns true if target string is found, false if terminated or timed out
+bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
+{
+ size_t index = 0; // maximum target string length is 64k bytes!
+ size_t termIndex = 0;
+ int c;
+
+ if( *target == 0)
+ return true; // return true if target is a null string
+ while( (c = timedRead()) > 0){
+
+ if(c != target[index])
+ index = 0; // reset index if any char does not match
+
+ if( c == target[index]){
+ //////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
+ if(++index >= targetLen){ // return true if all chars in the target match
+ return true;
+ }
+ }
+
+ if(termLen > 0 && c == terminator[termIndex]){
+ if(++termIndex >= termLen)
+ return false; // return false if terminate string found before target string
+ }
+ else
+ termIndex = 0;
+ }
+ return false;
+}
+
+
+// returns the first valid (long) integer value from the current position.
+// initial characters that are not digits (or the minus sign) are skipped
+// function is terminated by the first character that is not a digit.
+long Stream::parseInt()
+{
+ return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
+}
+
+// as above but a given skipChar is ignored
+// this allows format characters (typically commas) in values to be ignored
+long Stream::parseInt(char skipChar)
+{
+ boolean isNegative = false;
+ long value = 0;
+ int c;
+
+ c = peekNextDigit();
+ // ignore non numeric leading characters
+ if(c < 0)
+ return 0; // zero returned if timeout
+
+ do{
+ if(c == skipChar)
+ ; // ignore this charactor
+ else if(c == '-')
+ isNegative = true;
+ else if(c >= '0' && c <= '9') // is c a digit?
+ value = value * 10 + c - '0';
+ read(); // consume the character we got with peek
+ c = timedPeek();
+ }
+ while( (c >= '0' && c <= '9') || c == skipChar );
+
+ if(isNegative)
+ value = -value;
+ return value;
+}
+
+
+// as parseInt but returns a floating point value
+float Stream::parseFloat()
+{
+ return parseFloat(NO_SKIP_CHAR);
+}
+
+// as above but the given skipChar is ignored
+// this allows format characters (typically commas) in values to be ignored
+float Stream::parseFloat(char skipChar){
+ boolean isNegative = false;
+ boolean isFraction = false;
+ long value = 0;
+ char c;
+ float fraction = 1.0;
+
+ c = peekNextDigit();
+ // ignore non numeric leading characters
+ if(c < 0)
+ return 0; // zero returned if timeout
+
+ do{
+ if(c == skipChar)
+ ; // ignore
+ else if(c == '-')
+ isNegative = true;
+ else if (c == '.')
+ isFraction = true;
+ else if(c >= '0' && c <= '9') { // is c a digit?
+ value = value * 10 + c - '0';
+ if(isFraction)
+ fraction *= 0.1;
+ }
+ read(); // consume the character we got with peek
+ c = timedPeek();
+ }
+ while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );
+
+ if(isNegative)
+ value = -value;
+ if(isFraction)
+ return value * fraction;
+ else
+ return value;
+}
+
+// read characters from stream into buffer
+// terminates if length characters have been read, or timeout (see setTimeout)
+// returns the number of characters placed in the buffer
+// the buffer is NOT null terminated.
+//
+size_t Stream::readBytes(char *buffer, size_t length)
+{
+ size_t count = 0;
+ while (count < length) {
+ int c = timedRead();
+ if (c < 0) break;
+ *buffer++ = (char)c;
+ count++;
+ }
+ return count;
+}
+
+
+// as readBytes with terminator character
+// terminates if length characters have been read, timeout, or if the terminator character detected
+// returns the number of characters placed in the buffer (0 means no valid data found)
+
+size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
+{
+ if (length < 1) return 0;
+ size_t index = 0;
+ while (index < length) {
+ int c = timedRead();
+ if (c < 0 || c == terminator) break;
+ *buffer++ = (char)c;
+ index++;
+ }
+ return index; // return number of characters, not including null terminator
+}
+
+String Stream::readString()
+{
+ String ret;
+ int c = timedRead();
+ while (c >= 0)
+ {
+ ret += (char)c;
+ c = timedRead();
+ }
+ return ret;
+}
+
+String Stream::readStringUntil(char terminator)
+{
+ String ret;
+ int c = timedRead();
+ while (c >= 0 && c != terminator)
+ {
+ ret += (char)c;
+ c = timedRead();
+ }
+ return ret;
+}
+
diff --git a/cores/robot/Stream.h b/cores/robot/Stream.h
new file mode 100644
index 0000000..007b4bc
--- /dev/null
+++ b/cores/robot/Stream.h
@@ -0,0 +1,96 @@
+/*
+ Stream.h - base class for character-based streams.
+ Copyright (c) 2010 David A. Mellis. All right reserved.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+ parsing functions based on TextFinder library by Michael Margolis
+*/
+
+#ifndef Stream_h
+#define Stream_h
+
+#include <inttypes.h>
+#include "Print.h"
+
+// compatability macros for testing
+/*
+#define getInt() parseInt()
+#define getInt(skipChar) parseInt(skipchar)
+#define getFloat() parseFloat()
+#define getFloat(skipChar) parseFloat(skipChar)
+#define getString( pre_string, post_string, buffer, length)
+readBytesBetween( pre_string, terminator, buffer, length)
+*/
+
+class Stream : public Print
+{
+ protected:
+ unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
+ unsigned long _startMillis; // used for timeout measurement
+ int timedRead(); // private method to read stream with timeout
+ int timedPeek(); // private method to peek stream with timeout
+ int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
+
+ public:
+ virtual int available() = 0;
+ virtual int read() = 0;
+ virtual int peek() = 0;
+ virtual void flush() = 0;
+
+ Stream() {_timeout=1000;}
+
+// parsing methods
+
+ void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
+
+ bool find(char *target); // reads data from the stream until the target string is found
+ // returns true if target string is found, false if timed out (see setTimeout)
+
+ bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found
+ // returns true if target string is found, false if timed out
+
+ bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
+
+ bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
+
+
+ long parseInt(); // returns the first valid (long) integer value from the current position.
+ // initial characters that are not digits (or the minus sign) are skipped
+ // integer is terminated by the first character that is not a digit.
+
+ float parseFloat(); // float version of parseInt
+
+ size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
+ // terminates if length characters have been read or timeout (see setTimeout)
+ // returns the number of characters placed in the buffer (0 means no valid data found)
+
+ size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
+ // terminates if length characters have been read, timeout, or if the terminator character detected
+ // returns the number of characters placed in the buffer (0 means no valid data found)
+
+ // Arduino String functions to be added here
+ String readString();
+ String readStringUntil(char terminator);
+
+ protected:
+ long parseInt(char skipChar); // as above but the given skipChar is ignored
+ // as above but the given skipChar is ignored
+ // this allows format characters (typically commas) in values to be ignored
+
+ float parseFloat(char skipChar); // as above but the given skipChar is ignored
+};
+
+#endif
diff --git a/cores/robot/Tone.cpp b/cores/robot/Tone.cpp
new file mode 100755
index 0000000..9bb6fe7
--- /dev/null
+++ b/cores/robot/Tone.cpp
@@ -0,0 +1,616 @@
+/* Tone.cpp
+
+ A Tone Generator Library
+
+ Written by Brett Hagman
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+Version Modified By Date Comments
+------- ----------- -------- --------
+0001 B Hagman 09/08/02 Initial coding
+0002 B Hagman 09/08/18 Multiple pins
+0003 B Hagman 09/08/18 Moved initialization from constructor to begin()
+0004 B Hagman 09/09/26 Fixed problems with ATmega8
+0005 B Hagman 09/11/23 Scanned prescalars for best fit on 8 bit timers
+ 09/11/25 Changed pin toggle method to XOR
+ 09/11/25 Fixed timer0 from being excluded
+0006 D Mellis 09/12/29 Replaced objects with functions
+0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register
+0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY
+*************************************************/
+
+#include <avr/interrupt.h>
+#include <avr/pgmspace.h>
+#include "Arduino.h"
+#include "pins_arduino.h"
+
+#if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__)
+#define TCCR2A TCCR2
+#define TCCR2B TCCR2
+#define COM2A1 COM21
+#define COM2A0 COM20
+#define OCR2A OCR2
+#define TIMSK2 TIMSK
+#define OCIE2A OCIE2
+#define TIMER2_COMPA_vect TIMER2_COMP_vect
+#define TIMSK1 TIMSK
+#endif
+
+// timerx_toggle_count:
+// > 0 - duration specified
+// = 0 - stopped
+// < 0 - infinitely (until stop() method called, or new play() called)
+
+#if !defined(__AVR_ATmega8__)
+volatile long timer0_toggle_count;
+volatile uint8_t *timer0_pin_port;
+volatile uint8_t timer0_pin_mask;
+#endif
+
+volatile long timer1_toggle_count;
+volatile uint8_t *timer1_pin_port;
+volatile uint8_t timer1_pin_mask;
+volatile long timer2_toggle_count;
+volatile uint8_t *timer2_pin_port;
+volatile uint8_t timer2_pin_mask;
+
+#if defined(TIMSK3)
+volatile long timer3_toggle_count;
+volatile uint8_t *timer3_pin_port;
+volatile uint8_t timer3_pin_mask;
+#endif
+
+#if defined(TIMSK4)
+volatile long timer4_toggle_count;
+volatile uint8_t *timer4_pin_port;
+volatile uint8_t timer4_pin_mask;
+#endif
+
+#if defined(TIMSK5)
+volatile long timer5_toggle_count;
+volatile uint8_t *timer5_pin_port;
+volatile uint8_t timer5_pin_mask;
+#endif
+
+
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
+
+#define AVAILABLE_TONE_PINS 1
+#define USE_TIMER2
+
+const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
+static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ };
+
+#elif defined(__AVR_ATmega8__)
+
+#define AVAILABLE_TONE_PINS 1
+#define USE_TIMER2
+
+const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ };
+static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
+
+#elif defined(__AVR_ATmega32U4__)
+
+#define AVAILABLE_TONE_PINS 1
+#define USE_TIMER3
+
+const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ };
+static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
+
+#else
+
+#define AVAILABLE_TONE_PINS 1
+#define USE_TIMER2
+
+// Leave timer 0 to last.
+const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ };
+static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255 */ };
+
+#endif
+
+
+
+static int8_t toneBegin(uint8_t _pin)
+{
+ int8_t _timer = -1;
+
+ // if we're already using the pin, the timer should be configured.
+ for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
+ if (tone_pins[i] == _pin) {
+ return pgm_read_byte(tone_pin_to_timer_PGM + i);
+ }
+ }
+
+ // search for an unused timer.
+ for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
+ if (tone_pins[i] == 255) {
+ tone_pins[i] = _pin;
+ _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
+ break;
+ }
+ }
+
+ if (_timer != -1)
+ {
+ // Set timer specific stuff
+ // All timers in CTC mode
+ // 8 bit timers will require changing prescalar values,
+ // whereas 16 bit timers are set to either ck/1 or ck/64 prescalar
+ switch (_timer)
+ {
+ #if defined(TCCR0A) && defined(TCCR0B)
+ case 0:
+ // 8 bit timer
+ TCCR0A = 0;
+ TCCR0B = 0;
+ bitWrite(TCCR0A, WGM01, 1);
+ bitWrite(TCCR0B, CS00, 1);
+ timer0_pin_port = portOutputRegister(digitalPinToPort(_pin));
+ timer0_pin_mask = digitalPinToBitMask(_pin);
+ break;
+ #endif
+
+ #if defined(TCCR1A) && defined(TCCR1B) && defined(WGM12)
+ case 1:
+ // 16 bit timer
+ TCCR1A = 0;
+ TCCR1B = 0;
+ bitWrite(TCCR1B, WGM12, 1);
+ bitWrite(TCCR1B, CS10, 1);
+ timer1_pin_port = portOutputRegister(digitalPinToPort(_pin));
+ timer1_pin_mask = digitalPinToBitMask(_pin);
+ break;
+ #endif
+
+ #if defined(TCCR2A) && defined(TCCR2B)
+ case 2:
+ // 8 bit timer
+ TCCR2A = 0;
+ TCCR2B = 0;
+ bitWrite(TCCR2A, WGM21, 1);
+ bitWrite(TCCR2B, CS20, 1);
+ timer2_pin_port = portOutputRegister(digitalPinToPort(_pin));
+ timer2_pin_mask = digitalPinToBitMask(_pin);
+ break;
+ #endif
+
+ #if defined(TCCR3A) && defined(TCCR3B) && defined(TIMSK3)
+ case 3:
+ // 16 bit timer
+ TCCR3A = 0;
+ TCCR3B = 0;
+ bitWrite(TCCR3B, WGM32, 1);
+ bitWrite(TCCR3B, CS30, 1);
+ timer3_pin_port = portOutputRegister(digitalPinToPort(_pin));
+ timer3_pin_mask = digitalPinToBitMask(_pin);
+ break;
+ #endif
+
+ #if defined(TCCR4A) && defined(TCCR4B) && defined(TIMSK4)
+ case 4:
+ // 16 bit timer
+ TCCR4A = 0;
+ TCCR4B = 0;
+ #if defined(WGM42)
+ bitWrite(TCCR4B, WGM42, 1);
+ #elif defined(CS43)
+ #warning this may not be correct
+ // atmega32u4
+ bitWrite(TCCR4B, CS43, 1);
+ #endif
+ bitWrite(TCCR4B, CS40, 1);
+ timer4_pin_port = portOutputRegister(digitalPinToPort(_pin));
+ timer4_pin_mask = digitalPinToBitMask(_pin);
+ break;
+ #endif
+
+ #if defined(TCCR5A) && defined(TCCR5B) && defined(TIMSK5)
+ case 5:
+ // 16 bit timer
+ TCCR5A = 0;
+ TCCR5B = 0;
+ bitWrite(TCCR5B, WGM52, 1);
+ bitWrite(TCCR5B, CS50, 1);
+ timer5_pin_port = portOutputRegister(digitalPinToPort(_pin));
+ timer5_pin_mask = digitalPinToBitMask(_pin);
+ break;
+ #endif
+ }
+ }
+
+ return _timer;
+}
+
+
+
+// frequency (in hertz) and duration (in milliseconds).
+
+void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
+{
+ uint8_t prescalarbits = 0b001;
+ long toggle_count = 0;
+ uint32_t ocr = 0;
+ int8_t _timer;
+
+ _timer = toneBegin(_pin);
+
+ if (_timer >= 0)
+ {
+ // Set the pinMode as OUTPUT
+ pinMode(_pin, OUTPUT);
+
+ // if we are using an 8 bit timer, scan through prescalars to find the best fit
+ if (_timer == 0 || _timer == 2)
+ {
+ ocr = F_CPU / frequency / 2 - 1;
+ prescalarbits = 0b001; // ck/1: same for both timers
+ if (ocr > 255)
+ {
+ ocr = F_CPU / frequency / 2 / 8 - 1;
+ prescalarbits = 0b010; // ck/8: same for both timers
+
+ if (_timer == 2 && ocr > 255)
+ {
+ ocr = F_CPU / frequency / 2 / 32 - 1;
+ prescalarbits = 0b011;
+ }
+
+ if (ocr > 255)
+ {
+ ocr = F_CPU / frequency / 2 / 64 - 1;
+ prescalarbits = _timer == 0 ? 0b011 : 0b100;
+
+ if (_timer == 2 && ocr > 255)
+ {
+ ocr = F_CPU / frequency / 2 / 128 - 1;
+ prescalarbits = 0b101;
+ }
+
+ if (ocr > 255)
+ {
+ ocr = F_CPU / frequency / 2 / 256 - 1;
+ prescalarbits = _timer == 0 ? 0b100 : 0b110;
+ if (ocr > 255)
+ {
+ // can't do any better than /1024
+ ocr = F_CPU / frequency / 2 / 1024 - 1;
+ prescalarbits = _timer == 0 ? 0b101 : 0b111;
+ }
+ }
+ }
+ }
+
+#if defined(TCCR0B)
+ if (_timer == 0)
+ {
+ TCCR0B = prescalarbits;
+ }
+ else
+#endif
+#if defined(TCCR2B)
+ {
+ TCCR2B = prescalarbits;
+ }
+#else
+ {
+ // dummy place holder to make the above ifdefs work
+ }
+#endif
+ }
+ else
+ {
+ // two choices for the 16 bit timers: ck/1 or ck/64
+ ocr = F_CPU / frequency / 2 - 1;
+
+ prescalarbits = 0b001;
+ if (ocr > 0xffff)
+ {
+ ocr = F_CPU / frequency / 2 / 64 - 1;
+ prescalarbits = 0b011;
+ }
+
+ if (_timer == 1)
+ {
+#if defined(TCCR1B)
+ TCCR1B = (TCCR1B & 0b11111000) | prescalarbits;
+#endif
+ }
+#if defined(TCCR3B)
+ else if (_timer == 3)
+ TCCR3B = (TCCR3B & 0b11111000) | prescalarbits;
+#endif
+#if defined(TCCR4B)
+ else if (_timer == 4)
+ TCCR4B = (TCCR4B & 0b11111000) | prescalarbits;
+#endif
+#if defined(TCCR5B)
+ else if (_timer == 5)
+ TCCR5B = (TCCR5B & 0b11111000) | prescalarbits;
+#endif
+
+ }
+
+
+ // Calculate the toggle count
+ if (duration > 0)
+ {
+ toggle_count = 2 * frequency * duration / 1000;
+ }
+ else
+ {
+ toggle_count = -1;
+ }
+
+ // Set the OCR for the given timer,
+ // set the toggle count,
+ // then turn on the interrupts
+ switch (_timer)
+ {
+
+#if defined(OCR0A) && defined(TIMSK0) && defined(OCIE0A)
+ case 0:
+ OCR0A = ocr;
+ timer0_toggle_count = toggle_count;
+ bitWrite(TIMSK0, OCIE0A, 1);
+ break;
+#endif
+
+ case 1:
+#if defined(OCR1A) && defined(TIMSK1) && defined(OCIE1A)
+ OCR1A = ocr;
+ timer1_toggle_count = toggle_count;
+ bitWrite(TIMSK1, OCIE1A, 1);
+#elif defined(OCR1A) && defined(TIMSK) && defined(OCIE1A)
+ // this combination is for at least the ATmega32
+ OCR1A = ocr;
+ timer1_toggle_count = toggle_count;
+ bitWrite(TIMSK, OCIE1A, 1);
+#endif
+ break;
+
+#if defined(OCR2A) && defined(TIMSK2) && defined(OCIE2A)
+ case 2:
+ OCR2A = ocr;
+ timer2_toggle_count = toggle_count;
+ bitWrite(TIMSK2, OCIE2A, 1);
+ break;
+#endif
+
+#if defined(TIMSK3)
+ case 3:
+ OCR3A = ocr;
+ timer3_toggle_count = toggle_count;
+ bitWrite(TIMSK3, OCIE3A, 1);
+ break;
+#endif
+
+#if defined(TIMSK4)
+ case 4:
+ OCR4A = ocr;
+ timer4_toggle_count = toggle_count;
+ bitWrite(TIMSK4, OCIE4A, 1);
+ break;
+#endif
+
+#if defined(OCR5A) && defined(TIMSK5) && defined(OCIE5A)
+ case 5:
+ OCR5A = ocr;
+ timer5_toggle_count = toggle_count;
+ bitWrite(TIMSK5, OCIE5A, 1);
+ break;
+#endif
+
+ }
+ }
+}
+
+
+// XXX: this function only works properly for timer 2 (the only one we use
+// currently). for the others, it should end the tone, but won't restore
+// proper PWM functionality for the timer.
+void disableTimer(uint8_t _timer)
+{
+ switch (_timer)
+ {
+ case 0:
+ #if defined(TIMSK0)
+ TIMSK0 = 0;
+ #elif defined(TIMSK)
+ TIMSK = 0; // atmega32
+ #endif
+ break;
+
+#if defined(TIMSK1) && defined(OCIE1A)
+ case 1:
+ bitWrite(TIMSK1, OCIE1A, 0);
+ break;
+#endif
+
+ case 2:
+ #if defined(TIMSK2) && defined(OCIE2A)
+ bitWrite(TIMSK2, OCIE2A, 0); // disable interrupt
+ #endif
+ #if defined(TCCR2A) && defined(WGM20)
+ TCCR2A = (1 << WGM20);
+ #endif
+ #if defined(TCCR2B) && defined(CS22)
+ TCCR2B = (TCCR2B & 0b11111000) | (1 << CS22);
+ #endif
+ #if defined(OCR2A)
+ OCR2A = 0;
+ #endif
+ break;
+
+#if defined(TIMSK3)
+ case 3:
+ TIMSK3 = 0;
+ break;
+#endif
+
+#if defined(TIMSK4)
+ case 4:
+ TIMSK4 = 0;
+ break;
+#endif
+
+#if defined(TIMSK5)
+ case 5:
+ TIMSK5 = 0;
+ break;
+#endif
+ }
+}
+
+
+void noTone(uint8_t _pin)
+{
+ int8_t _timer = -1;
+
+ for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
+ if (tone_pins[i] == _pin) {
+ _timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
+ tone_pins[i] = 255;
+ }
+ }
+
+ disableTimer(_timer);
+
+ digitalWrite(_pin, 0);
+}
+
+#ifdef USE_TIMER0
+ISR(TIMER0_COMPA_vect)
+{
+ if (timer0_toggle_count != 0)
+ {
+ // toggle the pin
+ *timer0_pin_port ^= timer0_pin_mask;
+
+ if (timer0_toggle_count > 0)
+ timer0_toggle_count--;
+ }
+ else
+ {
+ disableTimer(0);
+ *timer0_pin_port &= ~(timer0_pin_mask); // keep pin low after stop
+ }
+}
+#endif
+
+
+#ifdef USE_TIMER1
+ISR(TIMER1_COMPA_vect)
+{
+ if (timer1_toggle_count != 0)
+ {
+ // toggle the pin
+ *timer1_pin_port ^= timer1_pin_mask;
+
+ if (timer1_toggle_count > 0)
+ timer1_toggle_count--;
+ }
+ else
+ {
+ disableTimer(1);
+ *timer1_pin_port &= ~(timer1_pin_mask); // keep pin low after stop
+ }
+}
+#endif
+
+
+#ifdef USE_TIMER2
+ISR(TIMER2_COMPA_vect)
+{
+
+ if (timer2_toggle_count != 0)
+ {
+ // toggle the pin
+ *timer2_pin_port ^= timer2_pin_mask;
+
+ if (timer2_toggle_count > 0)
+ timer2_toggle_count--;
+ }
+ else
+ {
+ // need to call noTone() so that the tone_pins[] entry is reset, so the
+ // timer gets initialized next time we call tone().
+ // XXX: this assumes timer 2 is always the first one used.
+ noTone(tone_pins[0]);
+// disableTimer(2);
+// *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
+ }
+}
+#endif
+
+
+#ifdef USE_TIMER3
+ISR(TIMER3_COMPA_vect)
+{
+ if (timer3_toggle_count != 0)
+ {
+ // toggle the pin
+ *timer3_pin_port ^= timer3_pin_mask;
+
+ if (timer3_toggle_count > 0)
+ timer3_toggle_count--;
+ }
+ else
+ {
+ disableTimer(3);
+ *timer3_pin_port &= ~(timer3_pin_mask); // keep pin low after stop
+ }
+}
+#endif
+
+
+#ifdef USE_TIMER4
+ISR(TIMER4_COMPA_vect)
+{
+ if (timer4_toggle_count != 0)
+ {
+ // toggle the pin
+ *timer4_pin_port ^= timer4_pin_mask;
+
+ if (timer4_toggle_count > 0)
+ timer4_toggle_count--;
+ }
+ else
+ {
+ disableTimer(4);
+ *timer4_pin_port &= ~(timer4_pin_mask); // keep pin low after stop
+ }
+}
+#endif
+
+
+#ifdef USE_TIMER5
+ISR(TIMER5_COMPA_vect)
+{
+ if (timer5_toggle_count != 0)
+ {
+ // toggle the pin
+ *timer5_pin_port ^= timer5_pin_mask;
+
+ if (timer5_toggle_count > 0)
+ timer5_toggle_count--;
+ }
+ else
+ {
+ disableTimer(5);
+ *timer5_pin_port &= ~(timer5_pin_mask); // keep pin low after stop
+ }
+}
+#endif
diff --git a/cores/robot/USBAPI.h b/cores/robot/USBAPI.h
new file mode 100644
index 0000000..eb2e593
--- /dev/null
+++ b/cores/robot/USBAPI.h
@@ -0,0 +1,196 @@
+
+
+#ifndef __USBAPI__
+#define __USBAPI__
+
+#if defined(USBCON)
+
+//================================================================================
+//================================================================================
+// USB
+
+class USBDevice_
+{
+public:
+ USBDevice_();
+ bool configured();
+
+ void attach();
+ void detach(); // Serial port goes down too...
+ void poll();
+};
+extern USBDevice_ USBDevice;
+
+//================================================================================
+//================================================================================
+// Serial over CDC (Serial1 is the physical port)
+
+class Serial_ : public Stream
+{
+private:
+ ring_buffer *_cdc_rx_buffer;
+public:
+ void begin(uint16_t baud_count);
+ void end(void);
+
+ virtual int available(void);
+ virtual void accept(void);
+ virtual int peek(void);
+ virtual int read(void);
+ virtual void flush(void);
+ virtual size_t write(uint8_t);
+ using Print::write; // pull in write(str) and write(buf, size) from Print
+ operator bool();
+};
+extern Serial_ Serial;
+
+//================================================================================
+//================================================================================
+// Mouse
+
+#define MOUSE_LEFT 1
+#define MOUSE_RIGHT 2
+#define MOUSE_MIDDLE 4
+#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
+
+class Mouse_
+{
+private:
+ uint8_t _buttons;
+ void buttons(uint8_t b);
+public:
+ Mouse_(void);
+ void begin(void);
+ void end(void);
+ void click(uint8_t b = MOUSE_LEFT);
+ void move(signed char x, signed char y, signed char wheel = 0);
+ void press(uint8_t b = MOUSE_LEFT); // press LEFT by default
+ void release(uint8_t b = MOUSE_LEFT); // release LEFT by default
+ bool isPressed(uint8_t b = MOUSE_LEFT); // check LEFT by default
+};
+extern Mouse_ Mouse;
+
+//================================================================================
+//================================================================================
+// Keyboard
+
+#define KEY_LEFT_CTRL 0x80
+#define KEY_LEFT_SHIFT 0x81
+#define KEY_LEFT_ALT 0x82
+#define KEY_LEFT_GUI 0x83
+#define KEY_RIGHT_CTRL 0x84
+#define KEY_RIGHT_SHIFT 0x85
+#define KEY_RIGHT_ALT 0x86
+#define KEY_RIGHT_GUI 0x87
+
+#define KEY_UP_ARROW 0xDA
+#define KEY_DOWN_ARROW 0xD9
+#define KEY_LEFT_ARROW 0xD8
+#define KEY_RIGHT_ARROW 0xD7
+#define KEY_BACKSPACE 0xB2
+#define KEY_TAB 0xB3
+#define KEY_RETURN 0xB0
+#define KEY_ESC 0xB1
+#define KEY_INSERT 0xD1
+#define KEY_DELETE 0xD4
+#define KEY_PAGE_UP 0xD3
+#define KEY_PAGE_DOWN 0xD6
+#define KEY_HOME 0xD2
+#define KEY_END 0xD5
+#define KEY_CAPS_LOCK 0xC1
+#define KEY_F1 0xC2
+#define KEY_F2 0xC3
+#define KEY_F3 0xC4
+#define KEY_F4 0xC5
+#define KEY_F5 0xC6
+#define KEY_F6 0xC7
+#define KEY_F7 0xC8
+#define KEY_F8 0xC9
+#define KEY_F9 0xCA
+#define KEY_F10 0xCB
+#define KEY_F11 0xCC
+#define KEY_F12 0xCD
+
+// Low level key report: up to 6 keys and shift, ctrl etc at once
+typedef struct
+{
+ uint8_t modifiers;
+ uint8_t reserved;
+ uint8_t keys[6];
+} KeyReport;
+
+class Keyboard_ : public Print
+{
+private:
+ KeyReport _keyReport;
+ void sendReport(KeyReport* keys);
+public:
+ Keyboard_(void);
+ void begin(void);
+ void end(void);
+ virtual size_t write(uint8_t k);
+ virtual size_t press(uint8_t k);
+ virtual size_t release(uint8_t k);
+ virtual void releaseAll(void);
+};
+extern Keyboard_ Keyboard;
+
+//================================================================================
+//================================================================================
+// Low level API
+
+typedef struct
+{
+ uint8_t bmRequestType;
+ uint8_t bRequest;
+ uint8_t wValueL;
+ uint8_t wValueH;
+ uint16_t wIndex;
+ uint16_t wLength;
+} Setup;
+
+//================================================================================
+//================================================================================
+// HID 'Driver'
+
+int HID_GetInterface(uint8_t* interfaceNum);
+int HID_GetDescriptor(int i);
+bool HID_Setup(Setup& setup);
+void HID_SendReport(uint8_t id, const void* data, int len);
+
+//================================================================================
+//================================================================================
+// MSC 'Driver'
+
+int MSC_GetInterface(uint8_t* interfaceNum);
+int MSC_GetDescriptor(int i);
+bool MSC_Setup(Setup& setup);
+bool MSC_Data(uint8_t rx,uint8_t tx);
+
+//================================================================================
+//================================================================================
+// CSC 'Driver'
+
+int CDC_GetInterface(uint8_t* interfaceNum);
+int CDC_GetDescriptor(int i);
+bool CDC_Setup(Setup& setup);
+
+//================================================================================
+//================================================================================
+
+#define TRANSFER_PGM 0x80
+#define TRANSFER_RELEASE 0x40
+#define TRANSFER_ZERO 0x20
+
+int USB_SendControl(uint8_t flags, const void* d, int len);
+int USB_RecvControl(void* d, int len);
+
+uint8_t USB_Available(uint8_t ep);
+int USB_Send(uint8_t ep, const void* data, int len); // blocking
+int USB_Recv(uint8_t ep, void* data, int len); // non-blocking
+int USB_Recv(uint8_t ep); // non-blocking
+void USB_Flush(uint8_t ep);
+
+#endif
+
+#endif /* if defined(USBCON) */ \ No newline at end of file
diff --git a/cores/robot/USBCore.cpp b/cores/robot/USBCore.cpp
new file mode 100644
index 0000000..d3e0170
--- /dev/null
+++ b/cores/robot/USBCore.cpp
@@ -0,0 +1,684 @@
+
+
+/* Copyright (c) 2010, Peter Barrett
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#include "Platform.h"
+#include "USBAPI.h"
+#include "USBDesc.h"
+
+#if defined(USBCON)
+
+#define EP_TYPE_CONTROL 0x00
+#define EP_TYPE_BULK_IN 0x81
+#define EP_TYPE_BULK_OUT 0x80
+#define EP_TYPE_INTERRUPT_IN 0xC1
+#define EP_TYPE_INTERRUPT_OUT 0xC0
+#define EP_TYPE_ISOCHRONOUS_IN 0x41
+#define EP_TYPE_ISOCHRONOUS_OUT 0x40
+
+/** Pulse generation counters to keep track of the number of milliseconds remaining for each pulse type */
+#define TX_RX_LED_PULSE_MS 100
+volatile u8 TxLEDPulse; /**< Milliseconds remaining for data Tx LED pulse */
+volatile u8 RxLEDPulse; /**< Milliseconds remaining for data Rx LED pulse */
+
+//==================================================================
+//==================================================================
+
+extern const u16 STRING_LANGUAGE[] PROGMEM;
+extern const u16 STRING_IPRODUCT[] PROGMEM;
+extern const u16 STRING_IMANUFACTURER[] PROGMEM;
+extern const DeviceDescriptor USB_DeviceDescriptor PROGMEM;
+extern const DeviceDescriptor USB_DeviceDescriptorA PROGMEM;
+
+const u16 STRING_LANGUAGE[2] = {
+ (3<<8) | (2+2),
+ 0x0409 // English
+};
+
+const u16 STRING_IPRODUCT[17] = {
+ (3<<8) | (2+2*16),
+#if USB_PID == 0x8036
+ 'A','r','d','u','i','n','o',' ','L','e','o','n','a','r','d','o'
+#elif USB_PID == 0x8037
+ 'A','r','d','u','i','n','o',' ','M','i','c','r','o',' ',' ',' '
+#elif USB_PID == 0x803C
+ 'A','r','d','u','i','n','o',' ','E','s','p','l','o','r','a',' '
+#elif USB_PID == 0x9208
+ 'L','i','l','y','P','a','d','U','S','B',' ',' ',' ',' ',' ',' '
+#else
+ 'U','S','B',' ','I','O',' ','B','o','a','r','d',' ',' ',' ',' '
+#endif
+};
+
+const u16 STRING_IMANUFACTURER[12] = {
+ (3<<8) | (2+2*11),
+#if USB_VID == 0x2341
+ 'A','r','d','u','i','n','o',' ','L','L','C'
+#elif USB_VID == 0x1b4f
+ 'S','p','a','r','k','F','u','n',' ',' ',' '
+#else
+ 'U','n','k','n','o','w','n',' ',' ',' ',' '
+#endif
+};
+
+#ifdef CDC_ENABLED
+#define DEVICE_CLASS 0x02
+#else
+#define DEVICE_CLASS 0x00
+#endif
+
+// DEVICE DESCRIPTOR
+const DeviceDescriptor USB_DeviceDescriptor =
+ D_DEVICE(0x00,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
+
+const DeviceDescriptor USB_DeviceDescriptorA =
+ D_DEVICE(DEVICE_CLASS,0x00,0x00,64,USB_VID,USB_PID,0x100,IMANUFACTURER,IPRODUCT,0,1);
+
+//==================================================================
+//==================================================================
+
+volatile u8 _usbConfiguration = 0;
+
+static inline void WaitIN(void)
+{
+ while (!(UEINTX & (1<<TXINI)));
+}
+
+static inline void ClearIN(void)
+{
+ UEINTX = ~(1<<TXINI);
+}
+
+static inline void WaitOUT(void)
+{
+ while (!(UEINTX & (1<<RXOUTI)))
+ ;
+}
+
+static inline u8 WaitForINOrOUT()
+{
+ while (!(UEINTX & ((1<<TXINI)|(1<<RXOUTI))))
+ ;
+ return (UEINTX & (1<<RXOUTI)) == 0;
+}
+
+static inline void ClearOUT(void)
+{
+ UEINTX = ~(1<<RXOUTI);
+}
+
+void Recv(volatile u8* data, u8 count)
+{
+ while (count--)
+ *data++ = UEDATX;
+
+ RXLED1; // light the RX LED
+ RxLEDPulse = TX_RX_LED_PULSE_MS;
+}
+
+static inline u8 Recv8()
+{
+ RXLED1; // light the RX LED
+ RxLEDPulse = TX_RX_LED_PULSE_MS;
+
+ return UEDATX;
+}
+
+static inline void Send8(u8 d)
+{
+ UEDATX = d;
+}
+
+static inline void SetEP(u8 ep)
+{
+ UENUM = ep;
+}
+
+static inline u8 FifoByteCount()
+{
+ return UEBCLX;
+}
+
+static inline u8 ReceivedSetupInt()
+{
+ return UEINTX & (1<<RXSTPI);
+}
+
+static inline void ClearSetupInt()
+{
+ UEINTX = ~((1<<RXSTPI) | (1<<RXOUTI) | (1<<TXINI));
+}
+
+static inline void Stall()
+{
+ UECONX = (1<<STALLRQ) | (1<<EPEN);
+}
+
+static inline u8 ReadWriteAllowed()
+{
+ return UEINTX & (1<<RWAL);
+}
+
+static inline u8 Stalled()
+{
+ return UEINTX & (1<<STALLEDI);
+}
+
+static inline u8 FifoFree()
+{
+ return UEINTX & (1<<FIFOCON);
+}
+
+static inline void ReleaseRX()
+{
+ UEINTX = 0x6B; // FIFOCON=0 NAKINI=1 RWAL=1 NAKOUTI=0 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=1
+}
+
+static inline void ReleaseTX()
+{
+ UEINTX = 0x3A; // FIFOCON=0 NAKINI=0 RWAL=1 NAKOUTI=1 RXSTPI=1 RXOUTI=0 STALLEDI=1 TXINI=0
+}
+
+static inline u8 FrameNumber()
+{
+ return UDFNUML;
+}
+
+//==================================================================
+//==================================================================
+
+u8 USBGetConfiguration(void)
+{
+ return _usbConfiguration;
+}
+
+#define USB_RECV_TIMEOUT
+class LockEP
+{
+ u8 _sreg;
+public:
+ LockEP(u8 ep) : _sreg(SREG)
+ {
+ cli();
+ SetEP(ep & 7);
+ }
+ ~LockEP()
+ {
+ SREG = _sreg;
+ }
+};
+
+// Number of bytes, assumes a rx endpoint
+u8 USB_Available(u8 ep)
+{
+ LockEP lock(ep);
+ return FifoByteCount();
+}
+
+// Non Blocking receive
+// Return number of bytes read
+int USB_Recv(u8 ep, void* d, int len)
+{
+ if (!_usbConfiguration || len < 0)
+ return -1;
+
+ LockEP lock(ep);
+ u8 n = FifoByteCount();
+ len = min(n,len);
+ n = len;
+ u8* dst = (u8*)d;
+ while (n--)
+ *dst++ = Recv8();
+ if (len && !FifoByteCount()) // release empty buffer
+ ReleaseRX();
+
+ return len;
+}
+
+// Recv 1 byte if ready
+int USB_Recv(u8 ep)
+{
+ u8 c;
+ if (USB_Recv(ep,&c,1) != 1)
+ return -1;
+ return c;
+}
+
+// Space in send EP
+u8 USB_SendSpace(u8 ep)
+{
+ LockEP lock(ep);
+ if (!ReadWriteAllowed())
+ return 0;
+ return 64 - FifoByteCount();
+}
+
+// Blocking Send of data to an endpoint
+int USB_Send(u8 ep, const void* d, int len)
+{
+ if (!_usbConfiguration)
+ return -1;
+
+ int r = len;
+ const u8* data = (const u8*)d;
+ u8 zero = ep & TRANSFER_ZERO;
+ u8 timeout = 250; // 250ms timeout on send? TODO
+ while (len)
+ {
+ u8 n = USB_SendSpace(ep);
+ if (n == 0)
+ {
+ if (!(--timeout))
+ return -1;
+ delay(1);
+ continue;
+ }
+
+ if (n > len)
+ n = len;
+ len -= n;
+ {
+ LockEP lock(ep);
+ if (ep & TRANSFER_ZERO)
+ {
+ while (n--)
+ Send8(0);
+ }
+ else if (ep & TRANSFER_PGM)
+ {
+ while (n--)
+ Send8(pgm_read_byte(data++));
+ }
+ else
+ {
+ while (n--)
+ Send8(*data++);
+ }
+ if (!ReadWriteAllowed() || ((len == 0) && (ep & TRANSFER_RELEASE))) // Release full buffer
+ ReleaseTX();
+ }
+ }
+ TXLED1; // light the TX LED
+ TxLEDPulse = TX_RX_LED_PULSE_MS;
+ return r;
+}
+
+extern const u8 _initEndpoints[] PROGMEM;
+const u8 _initEndpoints[] =
+{
+ 0,
+
+#ifdef CDC_ENABLED
+ EP_TYPE_INTERRUPT_IN, // CDC_ENDPOINT_ACM
+ EP_TYPE_BULK_OUT, // CDC_ENDPOINT_OUT
+ EP_TYPE_BULK_IN, // CDC_ENDPOINT_IN
+#endif
+
+#ifdef HID_ENABLED
+ EP_TYPE_INTERRUPT_IN // HID_ENDPOINT_INT
+#endif
+};
+
+#define EP_SINGLE_64 0x32 // EP0
+#define EP_DOUBLE_64 0x36 // Other endpoints
+
+static
+void InitEP(u8 index, u8 type, u8 size)
+{
+ UENUM = index;
+ UECONX = 1;
+ UECFG0X = type;
+ UECFG1X = size;
+}
+
+static
+void InitEndpoints()
+{
+ for (u8 i = 1; i < sizeof(_initEndpoints); i++)
+ {
+ UENUM = i;
+ UECONX = 1;
+ UECFG0X = pgm_read_byte(_initEndpoints+i);
+ UECFG1X = EP_DOUBLE_64;
+ }
+ UERST = 0x7E; // And reset them
+ UERST = 0;
+}
+
+// Handle CLASS_INTERFACE requests
+static
+bool ClassInterfaceRequest(Setup& setup)
+{
+ u8 i = setup.wIndex;
+
+#ifdef CDC_ENABLED
+ if (CDC_ACM_INTERFACE == i)
+ return CDC_Setup(setup);
+#endif
+
+#ifdef HID_ENABLED
+ if (HID_INTERFACE == i)
+ return HID_Setup(setup);
+#endif
+ return false;
+}
+
+int _cmark;
+int _cend;
+void InitControl(int end)
+{
+ SetEP(0);
+ _cmark = 0;
+ _cend = end;
+}
+
+static
+bool SendControl(u8 d)
+{
+ if (_cmark < _cend)
+ {
+ if (!WaitForINOrOUT())
+ return false;
+ Send8(d);
+ if (!((_cmark + 1) & 0x3F))
+ ClearIN(); // Fifo is full, release this packet
+ }
+ _cmark++;
+ return true;
+};
+
+// Clipped by _cmark/_cend
+int USB_SendControl(u8 flags, const void* d, int len)
+{
+ int sent = len;
+ const u8* data = (const u8*)d;
+ bool pgm = flags & TRANSFER_PGM;
+ while (len--)
+ {
+ u8 c = pgm ? pgm_read_byte(data++) : *data++;
+ if (!SendControl(c))
+ return -1;
+ }
+ return sent;
+}
+
+// Does not timeout or cross fifo boundaries
+// Will only work for transfers <= 64 bytes
+// TODO
+int USB_RecvControl(void* d, int len)
+{
+ WaitOUT();
+ Recv((u8*)d,len);
+ ClearOUT();
+ return len;
+}
+
+int SendInterfaces()
+{
+ int total = 0;
+ u8 interfaces = 0;
+
+#ifdef CDC_ENABLED
+ total = CDC_GetInterface(&interfaces);
+#endif
+
+#ifdef HID_ENABLED
+ total += HID_GetInterface(&interfaces);
+#endif
+
+ return interfaces;
+}
+
+// Construct a dynamic configuration descriptor
+// This really needs dynamic endpoint allocation etc
+// TODO
+static
+bool SendConfiguration(int maxlen)
+{
+ // Count and measure interfaces
+ InitControl(0);
+ int interfaces = SendInterfaces();
+ ConfigDescriptor config = D_CONFIG(_cmark + sizeof(ConfigDescriptor),interfaces);
+
+ // Now send them
+ InitControl(maxlen);
+ USB_SendControl(0,&config,sizeof(ConfigDescriptor));
+ SendInterfaces();
+ return true;
+}
+
+u8 _cdcComposite = 0;
+
+static
+bool SendDescriptor(Setup& setup)
+{
+ u8 t = setup.wValueH;
+ if (USB_CONFIGURATION_DESCRIPTOR_TYPE == t)
+ return SendConfiguration(setup.wLength);
+
+ InitControl(setup.wLength);
+#ifdef HID_ENABLED
+ if (HID_REPORT_DESCRIPTOR_TYPE == t)
+ return HID_GetDescriptor(t);
+#endif
+
+ u8 desc_length = 0;
+ const u8* desc_addr = 0;
+ if (USB_DEVICE_DESCRIPTOR_TYPE == t)
+ {
+ if (setup.wLength == 8)
+ _cdcComposite = 1;
+ desc_addr = _cdcComposite ? (const u8*)&USB_DeviceDescriptorA : (const u8*)&USB_DeviceDescriptor;
+ }
+ else if (USB_STRING_DESCRIPTOR_TYPE == t)
+ {
+ if (setup.wValueL == 0)
+ desc_addr = (const u8*)&STRING_LANGUAGE;
+ else if (setup.wValueL == IPRODUCT)
+ desc_addr = (const u8*)&STRING_IPRODUCT;
+ else if (setup.wValueL == IMANUFACTURER)
+ desc_addr = (const u8*)&STRING_IMANUFACTURER;
+ else
+ return false;
+ }
+
+ if (desc_addr == 0)
+ return false;
+ if (desc_length == 0)
+ desc_length = pgm_read_byte(desc_addr);
+
+ USB_SendControl(TRANSFER_PGM,desc_addr,desc_length);
+ return true;
+}
+
+// Endpoint 0 interrupt
+ISR(USB_COM_vect)
+{
+ SetEP(0);
+ if (!ReceivedSetupInt())
+ return;
+
+ Setup setup;
+ Recv((u8*)&setup,8);
+ ClearSetupInt();
+
+ u8 requestType = setup.bmRequestType;
+ if (requestType & REQUEST_DEVICETOHOST)
+ WaitIN();
+ else
+ ClearIN();
+
+ bool ok = true;
+ if (REQUEST_STANDARD == (requestType & REQUEST_TYPE))
+ {
+ // Standard Requests
+ u8 r = setup.bRequest;
+ if (GET_STATUS == r)
+ {
+ Send8(0); // TODO
+ Send8(0);
+ }
+ else if (CLEAR_FEATURE == r)
+ {
+ }
+ else if (SET_FEATURE == r)
+ {
+ }
+ else if (SET_ADDRESS == r)
+ {
+ WaitIN();
+ UDADDR = setup.wValueL | (1<<ADDEN);
+ }
+ else if (GET_DESCRIPTOR == r)
+ {
+ ok = SendDescriptor(setup);
+ }
+ else if (SET_DESCRIPTOR == r)
+ {
+ ok = false;
+ }
+ else if (GET_CONFIGURATION == r)
+ {
+ Send8(1);
+ }
+ else if (SET_CONFIGURATION == r)
+ {
+ if (REQUEST_DEVICE == (requestType & REQUEST_RECIPIENT))
+ {
+ InitEndpoints();
+ _usbConfiguration = setup.wValueL;
+ } else
+ ok = false;
+ }
+ else if (GET_INTERFACE == r)
+ {
+ }
+ else if (SET_INTERFACE == r)
+ {
+ }
+ }
+ else
+ {
+ InitControl(setup.wLength); // Max length of transfer
+ ok = ClassInterfaceRequest(setup);
+ }
+
+ if (ok)
+ ClearIN();
+ else
+ {
+ Stall();
+ }
+}
+
+void USB_Flush(u8 ep)
+{
+ SetEP(ep);
+ if (FifoByteCount())
+ ReleaseTX();
+}
+
+// General interrupt
+ISR(USB_GEN_vect)
+{
+ u8 udint = UDINT;
+ UDINT = 0;
+
+ // End of Reset
+ if (udint & (1<<EORSTI))
+ {
+ InitEP(0,EP_TYPE_CONTROL,EP_SINGLE_64); // init ep0
+ _usbConfiguration = 0; // not configured yet
+ UEIENX = 1 << RXSTPE; // Enable interrupts for ep0
+ }
+
+ // Start of Frame - happens every millisecond so we use it for TX and RX LED one-shot timing, too
+ if (udint & (1<<SOFI))
+ {
+#ifdef CDC_ENABLED
+ USB_Flush(CDC_TX); // Send a tx frame if found
+ if (USB_Available(CDC_RX)) // Handle received bytes (if any)
+ Serial.accept();
+#endif
+
+ // check whether the one-shot period has elapsed. if so, turn off the LED
+ if (TxLEDPulse && !(--TxLEDPulse))
+ TXLED0;
+ if (RxLEDPulse && !(--RxLEDPulse))
+ RXLED0;
+ }
+}
+
+// VBUS or counting frames
+// Any frame counting?
+u8 USBConnected()
+{
+ u8 f = UDFNUML;
+ delay(3);
+ return f != UDFNUML;
+}
+
+//=======================================================================
+//=======================================================================
+
+USBDevice_ USBDevice;
+
+USBDevice_::USBDevice_()
+{
+}
+
+void USBDevice_::attach()
+{
+ _usbConfiguration = 0;
+ UHWCON = 0x01; // power internal reg
+ USBCON = (1<<USBE)|(1<<FRZCLK); // clock frozen, usb enabled
+#if F_CPU == 16000000UL
+ PLLCSR = 0x12; // Need 16 MHz xtal
+#elif F_CPU == 8000000UL
+ PLLCSR = 0x02; // Need 8 MHz xtal
+#endif
+ while (!(PLLCSR & (1<<PLOCK))) // wait for lock pll
+ ;
+
+ // Some tests on specific versions of macosx (10.7.3), reported some
+ // strange behaviuors when the board is reset using the serial
+ // port touch at 1200 bps. This delay fixes this behaviour.
+ delay(1);
+
+ USBCON = ((1<<USBE)|(1<<OTGPADE)); // start USB clock
+ UDIEN = (1<<EORSTE)|(1<<SOFE); // Enable interrupts for EOR (End of Reset) and SOF (start of frame)
+ UDCON = 0; // enable attach resistor
+
+ TX_RX_LED_INIT;
+}
+
+void USBDevice_::detach()
+{
+}
+
+// Check for interrupts
+// TODO: VBUS detection
+bool USBDevice_::configured()
+{
+ return _usbConfiguration;
+}
+
+void USBDevice_::poll()
+{
+}
+
+#endif /* if defined(USBCON) */
diff --git a/cores/robot/USBCore.h b/cores/robot/USBCore.h
new file mode 100644
index 0000000..8d13806
--- /dev/null
+++ b/cores/robot/USBCore.h
@@ -0,0 +1,303 @@
+
+// Copyright (c) 2010, Peter Barrett
+/*
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#ifndef __USBCORE_H__
+#define __USBCORE_H__
+
+// Standard requests
+#define GET_STATUS 0
+#define CLEAR_FEATURE 1
+#define SET_FEATURE 3
+#define SET_ADDRESS 5
+#define GET_DESCRIPTOR 6
+#define SET_DESCRIPTOR 7
+#define GET_CONFIGURATION 8
+#define SET_CONFIGURATION 9
+#define GET_INTERFACE 10
+#define SET_INTERFACE 11
+
+
+// bmRequestType
+#define REQUEST_HOSTTODEVICE 0x00
+#define REQUEST_DEVICETOHOST 0x80
+#define REQUEST_DIRECTION 0x80
+
+#define REQUEST_STANDARD 0x00
+#define REQUEST_CLASS 0x20
+#define REQUEST_VENDOR 0x40
+#define REQUEST_TYPE 0x60
+
+#define REQUEST_DEVICE 0x00
+#define REQUEST_INTERFACE 0x01
+#define REQUEST_ENDPOINT 0x02
+#define REQUEST_OTHER 0x03
+#define REQUEST_RECIPIENT 0x03
+
+#define REQUEST_DEVICETOHOST_CLASS_INTERFACE (REQUEST_DEVICETOHOST + REQUEST_CLASS + REQUEST_INTERFACE)
+#define REQUEST_HOSTTODEVICE_CLASS_INTERFACE (REQUEST_HOSTTODEVICE + REQUEST_CLASS + REQUEST_INTERFACE)
+
+// Class requests
+
+#define CDC_SET_LINE_CODING 0x20
+#define CDC_GET_LINE_CODING 0x21
+#define CDC_SET_CONTROL_LINE_STATE 0x22
+
+#define MSC_RESET 0xFF
+#define MSC_GET_MAX_LUN 0xFE
+
+#define HID_GET_REPORT 0x01
+#define HID_GET_IDLE 0x02
+#define HID_GET_PROTOCOL 0x03
+#define HID_SET_REPORT 0x09
+#define HID_SET_IDLE 0x0A
+#define HID_SET_PROTOCOL 0x0B
+
+// Descriptors
+
+#define USB_DEVICE_DESC_SIZE 18
+#define USB_CONFIGUARTION_DESC_SIZE 9
+#define USB_INTERFACE_DESC_SIZE 9
+#define USB_ENDPOINT_DESC_SIZE 7
+
+#define USB_DEVICE_DESCRIPTOR_TYPE 1
+#define USB_CONFIGURATION_DESCRIPTOR_TYPE 2
+#define USB_STRING_DESCRIPTOR_TYPE 3
+#define USB_INTERFACE_DESCRIPTOR_TYPE 4
+#define USB_ENDPOINT_DESCRIPTOR_TYPE 5
+
+#define USB_DEVICE_CLASS_COMMUNICATIONS 0x02
+#define USB_DEVICE_CLASS_HUMAN_INTERFACE 0x03
+#define USB_DEVICE_CLASS_STORAGE 0x08
+#define USB_DEVICE_CLASS_VENDOR_SPECIFIC 0xFF
+
+#define USB_CONFIG_POWERED_MASK 0x40
+#define USB_CONFIG_BUS_POWERED 0x80
+#define USB_CONFIG_SELF_POWERED 0xC0
+#define USB_CONFIG_REMOTE_WAKEUP 0x20
+
+// bMaxPower in Configuration Descriptor
+#define USB_CONFIG_POWER_MA(mA) ((mA)/2)
+
+// bEndpointAddress in Endpoint Descriptor
+#define USB_ENDPOINT_DIRECTION_MASK 0x80
+#define USB_ENDPOINT_OUT(addr) ((addr) | 0x00)
+#define USB_ENDPOINT_IN(addr) ((addr) | 0x80)
+
+#define USB_ENDPOINT_TYPE_MASK 0x03
+#define USB_ENDPOINT_TYPE_CONTROL 0x00
+#define USB_ENDPOINT_TYPE_ISOCHRONOUS 0x01
+#define USB_ENDPOINT_TYPE_BULK 0x02
+#define USB_ENDPOINT_TYPE_INTERRUPT 0x03
+
+#define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF)
+
+#define CDC_V1_10 0x0110
+#define CDC_COMMUNICATION_INTERFACE_CLASS 0x02
+
+#define CDC_CALL_MANAGEMENT 0x01
+#define CDC_ABSTRACT_CONTROL_MODEL 0x02
+#define CDC_HEADER 0x00
+#define CDC_ABSTRACT_CONTROL_MANAGEMENT 0x02
+#define CDC_UNION 0x06
+#define CDC_CS_INTERFACE 0x24
+#define CDC_CS_ENDPOINT 0x25
+#define CDC_DATA_INTERFACE_CLASS 0x0A
+
+#define MSC_SUBCLASS_SCSI 0x06
+#define MSC_PROTOCOL_BULK_ONLY 0x50
+
+#define HID_HID_DESCRIPTOR_TYPE 0x21
+#define HID_REPORT_DESCRIPTOR_TYPE 0x22
+#define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
+
+
+// Device
+typedef struct {
+ u8 len; // 18
+ u8 dtype; // 1 USB_DEVICE_DESCRIPTOR_TYPE
+ u16 usbVersion; // 0x200
+ u8 deviceClass;
+ u8 deviceSubClass;
+ u8 deviceProtocol;
+ u8 packetSize0; // Packet 0
+ u16 idVendor;
+ u16 idProduct;
+ u16 deviceVersion; // 0x100
+ u8 iManufacturer;
+ u8 iProduct;
+ u8 iSerialNumber;
+ u8 bNumConfigurations;
+} DeviceDescriptor;
+
+// Config
+typedef struct {
+ u8 len; // 9
+ u8 dtype; // 2
+ u16 clen; // total length
+ u8 numInterfaces;
+ u8 config;
+ u8 iconfig;
+ u8 attributes;
+ u8 maxPower;
+} ConfigDescriptor;
+
+// String
+
+// Interface
+typedef struct
+{
+ u8 len; // 9
+ u8 dtype; // 4
+ u8 number;
+ u8 alternate;
+ u8 numEndpoints;
+ u8 interfaceClass;
+ u8 interfaceSubClass;
+ u8 protocol;
+ u8 iInterface;
+} InterfaceDescriptor;
+
+// Endpoint
+typedef struct
+{
+ u8 len; // 7
+ u8 dtype; // 5
+ u8 addr;
+ u8 attr;
+ u16 packetSize;
+ u8 interval;
+} EndpointDescriptor;
+
+// Interface Association Descriptor
+// Used to bind 2 interfaces together in CDC compostite device
+typedef struct
+{
+ u8 len; // 8
+ u8 dtype; // 11
+ u8 firstInterface;
+ u8 interfaceCount;
+ u8 functionClass;
+ u8 funtionSubClass;
+ u8 functionProtocol;
+ u8 iInterface;
+} IADDescriptor;
+
+// CDC CS interface descriptor
+typedef struct
+{
+ u8 len; // 5
+ u8 dtype; // 0x24
+ u8 subtype;
+ u8 d0;
+ u8 d1;
+} CDCCSInterfaceDescriptor;
+
+typedef struct
+{
+ u8 len; // 4
+ u8 dtype; // 0x24
+ u8 subtype;
+ u8 d0;
+} CDCCSInterfaceDescriptor4;
+
+typedef struct
+{
+ u8 len;
+ u8 dtype; // 0x24
+ u8 subtype; // 1
+ u8 bmCapabilities;
+ u8 bDataInterface;
+} CMFunctionalDescriptor;
+
+typedef struct
+{
+ u8 len;
+ u8 dtype; // 0x24
+ u8 subtype; // 1
+ u8 bmCapabilities;
+} ACMFunctionalDescriptor;
+
+typedef struct
+{
+ // IAD
+ IADDescriptor iad; // Only needed on compound device
+
+ // Control
+ InterfaceDescriptor cif; //
+ CDCCSInterfaceDescriptor header;
+ CMFunctionalDescriptor callManagement; // Call Management
+ ACMFunctionalDescriptor controlManagement; // ACM
+ CDCCSInterfaceDescriptor functionalDescriptor; // CDC_UNION
+ EndpointDescriptor cifin;
+
+ // Data
+ InterfaceDescriptor dif;
+ EndpointDescriptor in;
+ EndpointDescriptor out;
+} CDCDescriptor;
+
+typedef struct
+{
+ InterfaceDescriptor msc;
+ EndpointDescriptor in;
+ EndpointDescriptor out;
+} MSCDescriptor;
+
+typedef struct
+{
+ u8 len; // 9
+ u8 dtype; // 0x21
+ u8 addr;
+ u8 versionL; // 0x101
+ u8 versionH; // 0x101
+ u8 country;
+ u8 desctype; // 0x22 report
+ u8 descLenL;
+ u8 descLenH;
+} HIDDescDescriptor;
+
+typedef struct
+{
+ InterfaceDescriptor hid;
+ HIDDescDescriptor desc;
+ EndpointDescriptor in;
+} HIDDescriptor;
+
+
+#define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \
+ { 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
+
+#define D_CONFIG(_totalLength,_interfaces) \
+ { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(500) }
+
+#define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \
+ { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 }
+
+#define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \
+ { 7, 5, _addr,_attr,_packetSize, _interval }
+
+#define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \
+ { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 }
+
+#define D_HIDREPORT(_descriptorLength) \
+ { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
+
+#define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 }
+#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
+
+
+#endif \ No newline at end of file
diff --git a/cores/robot/USBDesc.h b/cores/robot/USBDesc.h
new file mode 100644
index 0000000..900713e
--- /dev/null
+++ b/cores/robot/USBDesc.h
@@ -0,0 +1,63 @@
+
+
+/* Copyright (c) 2011, Peter Barrett
+**
+** Permission to use, copy, modify, and/or distribute this software for
+** any purpose with or without fee is hereby granted, provided that the
+** above copyright notice and this permission notice appear in all copies.
+**
+** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
+** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
+** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
+** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
+** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+** SOFTWARE.
+*/
+
+#define CDC_ENABLED
+#define HID_ENABLED
+
+
+#ifdef CDC_ENABLED
+#define CDC_INTERFACE_COUNT 2
+#define CDC_ENPOINT_COUNT 3
+#else
+#define CDC_INTERFACE_COUNT 0
+#define CDC_ENPOINT_COUNT 0
+#endif
+
+#ifdef HID_ENABLED
+#define HID_INTERFACE_COUNT 1
+#define HID_ENPOINT_COUNT 1
+#else
+#define HID_INTERFACE_COUNT 0
+#define HID_ENPOINT_COUNT 0
+#endif
+
+#define CDC_ACM_INTERFACE 0 // CDC ACM
+#define CDC_DATA_INTERFACE 1 // CDC Data
+#define CDC_FIRST_ENDPOINT 1
+#define CDC_ENDPOINT_ACM (CDC_FIRST_ENDPOINT) // CDC First
+#define CDC_ENDPOINT_OUT (CDC_FIRST_ENDPOINT+1)
+#define CDC_ENDPOINT_IN (CDC_FIRST_ENDPOINT+2)
+
+#define HID_INTERFACE (CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT) // HID Interface
+#define HID_FIRST_ENDPOINT (CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT)
+#define HID_ENDPOINT_INT (HID_FIRST_ENDPOINT)
+
+#define INTERFACE_COUNT (MSC_INTERFACE + MSC_INTERFACE_COUNT)
+
+#ifdef CDC_ENABLED
+#define CDC_RX CDC_ENDPOINT_OUT
+#define CDC_TX CDC_ENDPOINT_IN
+#endif
+
+#ifdef HID_ENABLED
+#define HID_TX HID_ENDPOINT_INT
+#endif
+
+#define IMANUFACTURER 1
+#define IPRODUCT 2
+
diff --git a/cores/robot/Udp.h b/cores/robot/Udp.h
new file mode 100644
index 0000000..dc5644b
--- /dev/null
+++ b/cores/robot/Udp.h
@@ -0,0 +1,88 @@
+/*
+ * Udp.cpp: Library to send/receive UDP packets.
+ *
+ * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
+ * 1) UDP does not guarantee the order in which assembled UDP packets are received. This
+ * might not happen often in practice, but in larger network topologies, a UDP
+ * packet can be received out of sequence.
+ * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
+ * aware of it. Again, this may not be a concern in practice on small local networks.
+ * For more information, see http://www.cafeaulait.org/course/week12/35.html
+ *
+ * MIT License:
+ * Copyright (c) 2008 Bjoern Hartmann
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * bjoern@cs.stanford.edu 12/30/2008
+ */
+
+#ifndef udp_h
+#define udp_h
+
+#include <Stream.h>
+#include <IPAddress.h>
+
+class UDP : public Stream {
+
+public:
+ virtual uint8_t begin(uint16_t) =0; // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
+ virtual void stop() =0; // Finish with the UDP socket
+
+ // Sending UDP packets
+
+ // Start building up a packet to send to the remote host specific in ip and port
+ // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
+ virtual int beginPacket(IPAddress ip, uint16_t port) =0;
+ // Start building up a packet to send to the remote host specific in host and port
+ // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
+ virtual int beginPacket(const char *host, uint16_t port) =0;
+ // Finish off this packet and send it
+ // Returns 1 if the packet was sent successfully, 0 if there was an error
+ virtual int endPacket() =0;
+ // Write a single byte into the packet
+ virtual size_t write(uint8_t) =0;
+ // Write size bytes from buffer into the packet
+ virtual size_t write(const uint8_t *buffer, size_t size) =0;
+
+ // Start processing the next available incoming packet
+ // Returns the size of the packet in bytes, or 0 if no packets are available
+ virtual int parsePacket() =0;
+ // Number of bytes remaining in the current packet
+ virtual int available() =0;
+ // Read a single byte from the current packet
+ virtual int read() =0;
+ // Read up to len bytes from the current packet and place them into buffer
+ // Returns the number of bytes read, or 0 if none are available
+ virtual int read(unsigned char* buffer, size_t len) =0;
+ // Read up to len characters from the current packet and place them into buffer
+ // Returns the number of characters read, or 0 if none are available
+ virtual int read(char* buffer, size_t len) =0;
+ // Return the next byte from the current packet without moving on to the next byte
+ virtual int peek() =0;
+ virtual void flush() =0; // Finish reading the current packet
+
+ // Return the IP address of the host who sent the current incoming packet
+ virtual IPAddress remoteIP() =0;
+ // Return the port of the host who sent the current incoming packet
+ virtual uint16_t remotePort() =0;
+protected:
+ uint8_t* rawIPAddress(IPAddress& addr) { return addr.raw_address(); };
+};
+
+#endif
diff --git a/cores/robot/WCharacter.h b/cores/robot/WCharacter.h
new file mode 100644
index 0000000..79733b5
--- /dev/null
+++ b/cores/robot/WCharacter.h
@@ -0,0 +1,168 @@
+/*
+ WCharacter.h - Character utility functions for Wiring & Arduino
+ Copyright (c) 2010 Hernando Barragan. All right reserved.
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef Character_h
+#define Character_h
+
+#include <ctype.h>
+
+// WCharacter.h prototypes
+inline boolean isAlphaNumeric(int c) __attribute__((always_inline));
+inline boolean isAlpha(int c) __attribute__((always_inline));
+inline boolean isAscii(int c) __attribute__((always_inline));
+inline boolean isWhitespace(int c) __attribute__((always_inline));
+inline boolean isControl(int c) __attribute__((always_inline));
+inline boolean isDigit(int c) __attribute__((always_inline));
+inline boolean isGraph(int c) __attribute__((always_inline));
+inline boolean isLowerCase(int c) __attribute__((always_inline));
+inline boolean isPrintable(int c) __attribute__((always_inline));
+inline boolean isPunct(int c) __attribute__((always_inline));
+inline boolean isSpace(int c) __attribute__((always_inline));
+inline boolean isUpperCase(int c) __attribute__((always_inline));
+inline boolean isHexadecimalDigit(int c) __attribute__((always_inline));
+inline int toAscii(int c) __attribute__((always_inline));
+inline int toLowerCase(int c) __attribute__((always_inline));
+inline int toUpperCase(int c)__attribute__((always_inline));
+
+
+// Checks for an alphanumeric character.
+// It is equivalent to (isalpha(c) || isdigit(c)).
+inline boolean isAlphaNumeric(int c)
+{
+ return ( isalnum(c) == 0 ? false : true);
+}
+
+
+// Checks for an alphabetic character.
+// It is equivalent to (isupper(c) || islower(c)).
+inline boolean isAlpha(int c)
+{
+ return ( isalpha(c) == 0 ? false : true);
+}
+
+
+// Checks whether c is a 7-bit unsigned char value
+// that fits into the ASCII character set.
+inline boolean isAscii(int c)
+{
+ return ( isascii (c) == 0 ? false : true);
+}
+
+
+// Checks for a blank character, that is, a space or a tab.
+inline boolean isWhitespace(int c)
+{
+ return ( isblank (c) == 0 ? false : true);
+}
+
+
+// Checks for a control character.
+inline boolean isControl(int c)
+{
+ return ( iscntrl (c) == 0 ? false : true);
+}
+
+
+// Checks for a digit (0 through 9).
+inline boolean isDigit(int c)
+{
+ return ( isdigit (c) == 0 ? false : true);
+}
+
+
+// Checks for any printable character except space.
+inline boolean isGraph(int c)
+{
+ return ( isgraph (c) == 0 ? false : true);
+}
+
+
+// Checks for a lower-case character.
+inline boolean isLowerCase(int c)
+{
+ return (islower (c) == 0 ? false : true);
+}
+
+
+// Checks for any printable character including space.
+inline boolean isPrintable(int c)
+{
+ return ( isprint (c) == 0 ? false : true);
+}
+
+
+// Checks for any printable character which is not a space
+// or an alphanumeric character.
+inline boolean isPunct(int c)
+{
+ return ( ispunct (c) == 0 ? false : true);
+}
+
+
+// Checks for white-space characters. For the avr-libc library,
+// these are: space, formfeed ('\f'), newline ('\n'), carriage
+// return ('\r'), horizontal tab ('\t'), and vertical tab ('\v').
+inline boolean isSpace(int c)
+{
+ return ( isspace (c) == 0 ? false : true);
+}
+
+
+// Checks for an uppercase letter.
+inline boolean isUpperCase(int c)
+{
+ return ( isupper (c) == 0 ? false : true);
+}
+
+
+// Checks for a hexadecimal digits, i.e. one of 0 1 2 3 4 5 6 7
+// 8 9 a b c d e f A B C D E F.
+inline boolean isHexadecimalDigit(int c)
+{
+ return ( isxdigit (c) == 0 ? false : true);
+}
+
+
+// Converts c to a 7-bit unsigned char value that fits into the
+// ASCII character set, by clearing the high-order bits.
+inline int toAscii(int c)
+{
+ return toascii (c);
+}
+
+
+// Warning:
+// Many people will be unhappy if you use this function.
+// This function will convert accented letters into random
+// characters.
+
+// Converts the letter c to lower case, if possible.
+inline int toLowerCase(int c)
+{
+ return tolower (c);
+}
+
+
+// Converts the letter c to upper case, if possible.
+inline int toUpperCase(int c)
+{
+ return toupper (c);
+}
+
+#endif \ No newline at end of file
diff --git a/cores/robot/WInterrupts.c b/cores/robot/WInterrupts.c
new file mode 100644
index 0000000..d3fbf10
--- /dev/null
+++ b/cores/robot/WInterrupts.c
@@ -0,0 +1,334 @@
+/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
+
+/*
+ Part of the Wiring project - http://wiring.uniandes.edu.co
+
+ Copyright (c) 2004-05 Hernando Barragan
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+
+ Modified 24 November 2006 by David A. Mellis
+ Modified 1 August 2010 by Mark Sproul
+*/
+
+#include <inttypes.h>
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <avr/pgmspace.h>
+#include <stdio.h>
+
+#include "wiring_private.h"
+
+static volatile voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS];
+// volatile static voidFuncPtr twiIntFunc;
+
+void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
+ if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
+ intFunc[interruptNum] = userFunc;
+
+ // Configure the interrupt mode (trigger on low input, any change, rising
+ // edge, or falling edge). The mode constants were chosen to correspond
+ // to the configuration bits in the hardware register, so we simply shift
+ // the mode into place.
+
+ // Enable the interrupt.
+
+ switch (interruptNum) {
+#if defined(__AVR_ATmega32U4__)
+ // I hate doing this, but the register assignment differs between the 1280/2560
+ // and the 32U4. Since avrlib defines registers PCMSK1 and PCMSK2 that aren't
+ // even present on the 32U4 this is the only way to distinguish between them.
+ case 0:
+ EICRA = (EICRA & ~((1<<ISC00) | (1<<ISC01))) | (mode << ISC00);
+ EIMSK |= (1<<INT0);
+ break;
+ case 1:
+ EICRA = (EICRA & ~((1<<ISC10) | (1<<ISC11))) | (mode << ISC10);
+ EIMSK |= (1<<INT1);
+ break;
+ case 2:
+ EICRA = (EICRA & ~((1<<ISC20) | (1<<ISC21))) | (mode << ISC20);
+ EIMSK |= (1<<INT2);
+ break;
+ case 3:
+ EICRA = (EICRA & ~((1<<ISC30) | (1<<ISC31))) | (mode << ISC30);
+ EIMSK |= (1<<INT3);
+ break;
+ case 4:
+ EICRB = (EICRB & ~((1<<ISC60) | (1<<ISC61))) | (mode << ISC60);
+ EIMSK |= (1<<INT6);
+ break;
+#elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
+ case 2:
+ EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
+ EIMSK |= (1 << INT0);
+ break;
+ case 3:
+ EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
+ EIMSK |= (1 << INT1);
+ break;
+ case 4:
+ EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
+ EIMSK |= (1 << INT2);
+ break;
+ case 5:
+ EICRA = (EICRA & ~((1 << ISC30) | (1 << ISC31))) | (mode << ISC30);
+ EIMSK |= (1 << INT3);
+ break;
+ case 0:
+ EICRB = (EICRB & ~((1 << ISC40) | (1 << ISC41))) | (mode << ISC40);
+ EIMSK |= (1 << INT4);
+ break;
+ case 1:
+ EICRB = (EICRB & ~((1 << ISC50) | (1 << ISC51))) | (mode << ISC50);
+ EIMSK |= (1 << INT5);
+ break;
+ case 6:
+ EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (mode << ISC60);
+ EIMSK |= (1 << INT6);
+ break;
+ case 7:
+ EICRB = (EICRB & ~((1 << ISC70) | (1 << ISC71))) | (mode << ISC70);
+ EIMSK |= (1 << INT7);
+ break;
+#else
+ case 0:
+ #if defined(EICRA) && defined(ISC00) && defined(EIMSK)
+ EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
+ EIMSK |= (1 << INT0);
+ #elif defined(MCUCR) && defined(ISC00) && defined(GICR)
+ MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
+ GICR |= (1 << INT0);
+ #elif defined(MCUCR) && defined(ISC00) && defined(GIMSK)
+ MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
+ GIMSK |= (1 << INT0);
+ #else
+ #error attachInterrupt not finished for this CPU (case 0)
+ #endif
+ break;
+
+ case 1:
+ #if defined(EICRA) && defined(ISC10) && defined(ISC11) && defined(EIMSK)
+ EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
+ EIMSK |= (1 << INT1);
+ #elif defined(MCUCR) && defined(ISC10) && defined(ISC11) && defined(GICR)
+ MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
+ GICR |= (1 << INT1);
+ #elif defined(MCUCR) && defined(ISC10) && defined(GIMSK) && defined(GIMSK)
+ MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
+ GIMSK |= (1 << INT1);
+ #else
+ #warning attachInterrupt may need some more work for this cpu (case 1)
+ #endif
+ break;
+
+ case 2:
+ #if defined(EICRA) && defined(ISC20) && defined(ISC21) && defined(EIMSK)
+ EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
+ EIMSK |= (1 << INT2);
+ #elif defined(MCUCR) && defined(ISC20) && defined(ISC21) && defined(GICR)
+ MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
+ GICR |= (1 << INT2);
+ #elif defined(MCUCR) && defined(ISC20) && defined(GIMSK) && defined(GIMSK)
+ MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
+ GIMSK |= (1 << INT2);
+ #endif
+ break;
+#endif
+ }
+ }
+}
+
+void detachInterrupt(uint8_t interruptNum) {
+ if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
+ // Disable the interrupt. (We can't assume that interruptNum is equal
+ // to the number of the EIMSK bit to clear, as this isn't true on the
+ // ATmega8. There, INT0 is 6 and INT1 is 7.)
+ switch (interruptNum) {
+#if defined(__AVR_ATmega32U4__)
+ case 0:
+ EIMSK &= ~(1<<INT0);
+ break;
+ case 1:
+ EIMSK &= ~(1<<INT1);
+ break;
+ case 2:
+ EIMSK &= ~(1<<INT2);
+ break;
+ case 3:
+ EIMSK &= ~(1<<INT3);
+ break;
+ case 4:
+ EIMSK &= ~(1<<INT6);
+ break;
+#elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
+ case 2:
+ EIMSK &= ~(1 << INT0);
+ break;
+ case 3:
+ EIMSK &= ~(1 << INT1);
+ break;
+ case 4:
+ EIMSK &= ~(1 << INT2);
+ break;
+ case 5:
+ EIMSK &= ~(1 << INT3);
+ break;
+ case 0:
+ EIMSK &= ~(1 << INT4);
+ break;
+ case 1:
+ EIMSK &= ~(1 << INT5);
+ break;
+ case 6:
+ EIMSK &= ~(1 << INT6);
+ break;
+ case 7:
+ EIMSK &= ~(1 << INT7);
+ break;
+#else
+ case 0:
+ #if defined(EIMSK) && defined(INT0)
+ EIMSK &= ~(1 << INT0);
+ #elif defined(GICR) && defined(ISC00)
+ GICR &= ~(1 << INT0); // atmega32
+ #elif defined(GIMSK) && defined(INT0)
+ GIMSK &= ~(1 << INT0);
+ #else
+ #error detachInterrupt not finished for this cpu
+ #endif
+ break;
+
+ case 1:
+ #if defined(EIMSK) && defined(INT1)
+ EIMSK &= ~(1 << INT1);
+ #elif defined(GICR) && defined(INT1)
+ GICR &= ~(1 << INT1); // atmega32
+ #elif defined(GIMSK) && defined(INT1)
+ GIMSK &= ~(1 << INT1);
+ #else
+ #warning detachInterrupt may need some more work for this cpu (case 1)
+ #endif
+ break;
+#endif
+ }
+
+ intFunc[interruptNum] = 0;
+ }
+}
+
+/*
+void attachInterruptTwi(void (*userFunc)(void) ) {
+ twiIntFunc = userFunc;
+}
+*/
+
+#if defined(__AVR_ATmega32U4__)
+ISR(INT0_vect) {
+ if(intFunc[EXTERNAL_INT_0])
+ intFunc[EXTERNAL_INT_0]();
+}
+
+ISR(INT1_vect) {
+ if(intFunc[EXTERNAL_INT_1])
+ intFunc[EXTERNAL_INT_1]();
+}
+
+ISR(INT2_vect) {
+ if(intFunc[EXTERNAL_INT_2])
+ intFunc[EXTERNAL_INT_2]();
+}
+
+ISR(INT3_vect) {
+ if(intFunc[EXTERNAL_INT_3])
+ intFunc[EXTERNAL_INT_3]();
+}
+
+ISR(INT6_vect) {
+ if(intFunc[EXTERNAL_INT_4])
+ intFunc[EXTERNAL_INT_4]();
+}
+
+#elif defined(EICRA) && defined(EICRB)
+
+ISR(INT0_vect) {
+ if(intFunc[EXTERNAL_INT_2])
+ intFunc[EXTERNAL_INT_2]();
+}
+
+ISR(INT1_vect) {
+ if(intFunc[EXTERNAL_INT_3])
+ intFunc[EXTERNAL_INT_3]();
+}
+
+ISR(INT2_vect) {
+ if(intFunc[EXTERNAL_INT_4])
+ intFunc[EXTERNAL_INT_4]();
+}
+
+ISR(INT3_vect) {
+ if(intFunc[EXTERNAL_INT_5])
+ intFunc[EXTERNAL_INT_5]();
+}
+
+ISR(INT4_vect) {
+ if(intFunc[EXTERNAL_INT_0])
+ intFunc[EXTERNAL_INT_0]();
+}
+
+ISR(INT5_vect) {
+ if(intFunc[EXTERNAL_INT_1])
+ intFunc[EXTERNAL_INT_1]();
+}
+
+ISR(INT6_vect) {
+ if(intFunc[EXTERNAL_INT_6])
+ intFunc[EXTERNAL_INT_6]();
+}
+
+ISR(INT7_vect) {
+ if(intFunc[EXTERNAL_INT_7])
+ intFunc[EXTERNAL_INT_7]();
+}
+
+#else
+
+ISR(INT0_vect) {
+ if(intFunc[EXTERNAL_INT_0])
+ intFunc[EXTERNAL_INT_0]();
+}
+
+ISR(INT1_vect) {
+ if(intFunc[EXTERNAL_INT_1])
+ intFunc[EXTERNAL_INT_1]();
+}
+
+#if defined(EICRA) && defined(ISC20)
+ISR(INT2_vect) {
+ if(intFunc[EXTERNAL_INT_2])
+ intFunc[EXTERNAL_INT_2]();
+}
+#endif
+
+#endif
+
+/*
+ISR(TWI_vect) {
+ if(twiIntFunc)
+ twiIntFunc();
+}
+*/
+
diff --git a/cores/robot/WMath.cpp b/cores/robot/WMath.cpp
new file mode 100644
index 0000000..2120c4c
--- /dev/null
+++ b/cores/robot/WMath.cpp
@@ -0,0 +1,60 @@
+/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
+
+/*
+ Part of the Wiring project - http://wiring.org.co
+ Copyright (c) 2004-06 Hernando Barragan
+ Modified 13 August 2006, David A. Mellis for Arduino - http://www.arduino.cc/
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+
+ $Id$
+*/
+
+extern "C" {
+ #include "stdlib.h"
+}
+
+void randomSeed(unsigned int seed)
+{
+ if (seed != 0) {
+ srandom(seed);
+ }
+}
+
+long random(long howbig)
+{
+ if (howbig == 0) {
+ return 0;
+ }
+ return random() % howbig;
+}
+
+long random(long howsmall, long howbig)
+{
+ if (howsmall >= howbig) {
+ return howsmall;
+ }
+ long diff = howbig - howsmall;
+ return random(diff) + howsmall;
+}
+
+long map(long x, long in_min, long in_max, long out_min, long out_max)
+{
+ return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
+}
+
+unsigned int makeWord(unsigned int w) { return w; }
+unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; } \ No newline at end of file
diff --git a/cores/robot/WString.cpp b/cores/robot/WString.cpp
new file mode 100644
index 0000000..c6839fc
--- /dev/null
+++ b/cores/robot/WString.cpp
@@ -0,0 +1,645 @@
+/*
+ WString.cpp - String library for Wiring & Arduino
+ ...mostly rewritten by Paul Stoffregen...
+ Copyright (c) 2009-10 Hernando Barragan. All rights reserved.
+ Copyright 2011, Paul Stoffregen, paul@pjrc.com
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include "WString.h"
+
+
+/*********************************************/
+/* Constructors */
+/*********************************************/
+
+String::String(const char *cstr)
+{
+ init();
+ if (cstr) copy(cstr, strlen(cstr));
+}
+
+String::String(const String &value)
+{
+ init();
+ *this = value;
+}
+
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+String::String(String &&rval)
+{
+ init();
+ move(rval);
+}
+String::String(StringSumHelper &&rval)
+{
+ init();
+ move(rval);
+}
+#endif
+
+String::String(char c)
+{
+ init();
+ char buf[2];
+ buf[0] = c;
+ buf[1] = 0;
+ *this = buf;
+}
+
+String::String(unsigned char value, unsigned char base)
+{
+ init();
+ char buf[9];
+ utoa(value, buf, base);
+ *this = buf;
+}
+
+String::String(int value, unsigned char base)
+{
+ init();
+ char buf[18];
+ itoa(value, buf, base);
+ *this = buf;
+}
+
+String::String(unsigned int value, unsigned char base)
+{
+ init();
+ char buf[17];
+ utoa(value, buf, base);
+ *this = buf;
+}
+
+String::String(long value, unsigned char base)
+{
+ init();
+ char buf[34];
+ ltoa(value, buf, base);
+ *this = buf;
+}
+
+String::String(unsigned long value, unsigned char base)
+{
+ init();
+ char buf[33];
+ ultoa(value, buf, base);
+ *this = buf;
+}
+
+String::~String()
+{
+ free(buffer);
+}
+
+/*********************************************/
+/* Memory Management */
+/*********************************************/
+
+inline void String::init(void)
+{
+ buffer = NULL;
+ capacity = 0;
+ len = 0;
+ flags = 0;
+}
+
+void String::invalidate(void)
+{
+ if (buffer) free(buffer);
+ buffer = NULL;
+ capacity = len = 0;
+}
+
+unsigned char String::reserve(unsigned int size)
+{
+ if (buffer && capacity >= size) return 1;
+ if (changeBuffer(size)) {
+ if (len == 0) buffer[0] = 0;
+ return 1;
+ }
+ return 0;
+}
+
+unsigned char String::changeBuffer(unsigned int maxStrLen)
+{
+ char *newbuffer = (char *)realloc(buffer, maxStrLen + 1);
+ if (newbuffer) {
+ buffer = newbuffer;
+ capacity = maxStrLen;
+ return 1;
+ }
+ return 0;
+}
+
+/*********************************************/
+/* Copy and Move */
+/*********************************************/
+
+String & String::copy(const char *cstr, unsigned int length)
+{
+ if (!reserve(length)) {
+ invalidate();
+ return *this;
+ }
+ len = length;
+ strcpy(buffer, cstr);
+ return *this;
+}
+
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+void String::move(String &rhs)
+{
+ if (buffer) {
+ if (capacity >= rhs.len) {
+ strcpy(buffer, rhs.buffer);
+ len = rhs.len;
+ rhs.len = 0;
+ return;
+ } else {
+ free(buffer);
+ }
+ }
+ buffer = rhs.buffer;
+ capacity = rhs.capacity;
+ len = rhs.len;
+ rhs.buffer = NULL;
+ rhs.capacity = 0;
+ rhs.len = 0;
+}
+#endif
+
+String & String::operator = (const String &rhs)
+{
+ if (this == &rhs) return *this;
+
+ if (rhs.buffer) copy(rhs.buffer, rhs.len);
+ else invalidate();
+
+ return *this;
+}
+
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+String & String::operator = (String &&rval)
+{
+ if (this != &rval) move(rval);
+ return *this;
+}
+
+String & String::operator = (StringSumHelper &&rval)
+{
+ if (this != &rval) move(rval);
+ return *this;
+}
+#endif
+
+String & String::operator = (const char *cstr)
+{
+ if (cstr) copy(cstr, strlen(cstr));
+ else invalidate();
+
+ return *this;
+}
+
+/*********************************************/
+/* concat */
+/*********************************************/
+
+unsigned char String::concat(const String &s)
+{
+ return concat(s.buffer, s.len);
+}
+
+unsigned char String::concat(const char *cstr, unsigned int length)
+{
+ unsigned int newlen = len + length;
+ if (!cstr) return 0;
+ if (length == 0) return 1;
+ if (!reserve(newlen)) return 0;
+ strcpy(buffer + len, cstr);
+ len = newlen;
+ return 1;
+}
+
+unsigned char String::concat(const char *cstr)
+{
+ if (!cstr) return 0;
+ return concat(cstr, strlen(cstr));
+}
+
+unsigned char String::concat(char c)
+{
+ char buf[2];
+ buf[0] = c;
+ buf[1] = 0;
+ return concat(buf, 1);
+}
+
+unsigned char String::concat(unsigned char num)
+{
+ char buf[4];
+ itoa(num, buf, 10);
+ return concat(buf, strlen(buf));
+}
+
+unsigned char String::concat(int num)
+{
+ char buf[7];
+ itoa(num, buf, 10);
+ return concat(buf, strlen(buf));
+}
+
+unsigned char String::concat(unsigned int num)
+{
+ char buf[6];
+ utoa(num, buf, 10);
+ return concat(buf, strlen(buf));
+}
+
+unsigned char String::concat(long num)
+{
+ char buf[12];
+ ltoa(num, buf, 10);
+ return concat(buf, strlen(buf));
+}
+
+unsigned char String::concat(unsigned long num)
+{
+ char buf[11];
+ ultoa(num, buf, 10);
+ return concat(buf, strlen(buf));
+}
+
+/*********************************************/
+/* Concatenate */
+/*********************************************/
+
+StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(rhs.buffer, rhs.len)) a.invalidate();
+ return a;
+}
+
+StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!cstr || !a.concat(cstr, strlen(cstr))) a.invalidate();
+ return a;
+}
+
+StringSumHelper & operator + (const StringSumHelper &lhs, char c)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(c)) a.invalidate();
+ return a;
+}
+
+StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(num)) a.invalidate();
+ return a;
+}
+
+StringSumHelper & operator + (const StringSumHelper &lhs, int num)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(num)) a.invalidate();
+ return a;
+}
+
+StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(num)) a.invalidate();
+ return a;
+}
+
+StringSumHelper & operator + (const StringSumHelper &lhs, long num)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(num)) a.invalidate();
+ return a;
+}
+
+StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num)
+{
+ StringSumHelper &a = const_cast<StringSumHelper&>(lhs);
+ if (!a.concat(num)) a.invalidate();
+ return a;
+}
+
+/*********************************************/
+/* Comparison */
+/*********************************************/
+
+int String::compareTo(const String &s) const
+{
+ if (!buffer || !s.buffer) {
+ if (s.buffer && s.len > 0) return 0 - *(unsigned char *)s.buffer;
+ if (buffer && len > 0) return *(unsigned char *)buffer;
+ return 0;
+ }
+ return strcmp(buffer, s.buffer);
+}
+
+unsigned char String::equals(const String &s2) const
+{
+ return (len == s2.len && compareTo(s2) == 0);
+}
+
+unsigned char String::equals(const char *cstr) const
+{
+ if (len == 0) return (cstr == NULL || *cstr == 0);
+ if (cstr == NULL) return buffer[0] == 0;
+ return strcmp(buffer, cstr) == 0;
+}
+
+unsigned char String::operator<(const String &rhs) const
+{
+ return compareTo(rhs) < 0;
+}
+
+unsigned char String::operator>(const String &rhs) const
+{
+ return compareTo(rhs) > 0;
+}
+
+unsigned char String::operator<=(const String &rhs) const
+{
+ return compareTo(rhs) <= 0;
+}
+
+unsigned char String::operator>=(const String &rhs) const
+{
+ return compareTo(rhs) >= 0;
+}
+
+unsigned char String::equalsIgnoreCase( const String &s2 ) const
+{
+ if (this == &s2) return 1;
+ if (len != s2.len) return 0;
+ if (len == 0) return 1;
+ const char *p1 = buffer;
+ const char *p2 = s2.buffer;
+ while (*p1) {
+ if (tolower(*p1++) != tolower(*p2++)) return 0;
+ }
+ return 1;
+}
+
+unsigned char String::startsWith( const String &s2 ) const
+{
+ if (len < s2.len) return 0;
+ return startsWith(s2, 0);
+}
+
+unsigned char String::startsWith( const String &s2, unsigned int offset ) const
+{
+ if (offset > len - s2.len || !buffer || !s2.buffer) return 0;
+ return strncmp( &buffer[offset], s2.buffer, s2.len ) == 0;
+}
+
+unsigned char String::endsWith( const String &s2 ) const
+{
+ if ( len < s2.len || !buffer || !s2.buffer) return 0;
+ return strcmp(&buffer[len - s2.len], s2.buffer) == 0;
+}
+
+/*********************************************/
+/* Character Access */
+/*********************************************/
+
+char String::charAt(unsigned int loc) const
+{
+ return operator[](loc);
+}
+
+void String::setCharAt(unsigned int loc, char c)
+{
+ if (loc < len) buffer[loc] = c;
+}
+
+char & String::operator[](unsigned int index)
+{
+ static char dummy_writable_char;
+ if (index >= len || !buffer) {
+ dummy_writable_char = 0;
+ return dummy_writable_char;
+ }
+ return buffer[index];
+}
+
+char String::operator[]( unsigned int index ) const
+{
+ if (index >= len || !buffer) return 0;
+ return buffer[index];
+}
+
+void String::getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index) const
+{
+ if (!bufsize || !buf) return;
+ if (index >= len) {
+ buf[0] = 0;
+ return;
+ }
+ unsigned int n = bufsize - 1;
+ if (n > len - index) n = len - index;
+ strncpy((char *)buf, buffer + index, n);
+ buf[n] = 0;
+}
+
+/*********************************************/
+/* Search */
+/*********************************************/
+
+int String::indexOf(char c) const
+{
+ return indexOf(c, 0);
+}
+
+int String::indexOf( char ch, unsigned int fromIndex ) const
+{
+ if (fromIndex >= len) return -1;
+ const char* temp = strchr(buffer + fromIndex, ch);
+ if (temp == NULL) return -1;
+ return temp - buffer;
+}
+
+int String::indexOf(const String &s2) const
+{
+ return indexOf(s2, 0);
+}
+
+int String::indexOf(const String &s2, unsigned int fromIndex) const
+{
+ if (fromIndex >= len) return -1;
+ const char *found = strstr(buffer + fromIndex, s2.buffer);
+ if (found == NULL) return -1;
+ return found - buffer;
+}
+
+int String::lastIndexOf( char theChar ) const
+{
+ return lastIndexOf(theChar, len - 1);
+}
+
+int String::lastIndexOf(char ch, unsigned int fromIndex) const
+{
+ if (fromIndex >= len) return -1;
+ char tempchar = buffer[fromIndex + 1];
+ buffer[fromIndex + 1] = '\0';
+ char* temp = strrchr( buffer, ch );
+ buffer[fromIndex + 1] = tempchar;
+ if (temp == NULL) return -1;
+ return temp - buffer;
+}
+
+int String::lastIndexOf(const String &s2) const
+{
+ return lastIndexOf(s2, len - s2.len);
+}
+
+int String::lastIndexOf(const String &s2, unsigned int fromIndex) const
+{
+ if (s2.len == 0 || len == 0 || s2.len > len) return -1;
+ if (fromIndex >= len) fromIndex = len - 1;
+ int found = -1;
+ for (char *p = buffer; p <= buffer + fromIndex; p++) {
+ p = strstr(p, s2.buffer);
+ if (!p) break;
+ if ((unsigned int)(p - buffer) <= fromIndex) found = p - buffer;
+ }
+ return found;
+}
+
+String String::substring( unsigned int left ) const
+{
+ return substring(left, len);
+}
+
+String String::substring(unsigned int left, unsigned int right) const
+{
+ if (left > right) {
+ unsigned int temp = right;
+ right = left;
+ left = temp;
+ }
+ String out;
+ if (left > len) return out;
+ if (right > len) right = len;
+ char temp = buffer[right]; // save the replaced character
+ buffer[right] = '\0';
+ out = buffer + left; // pointer arithmetic
+ buffer[right] = temp; //restore character
+ return out;
+}
+
+/*********************************************/
+/* Modification */
+/*********************************************/
+
+void String::replace(char find, char replace)
+{
+ if (!buffer) return;
+ for (char *p = buffer; *p; p++) {
+ if (*p == find) *p = replace;
+ }
+}
+
+void String::replace(const String& find, const String& replace)
+{
+ if (len == 0 || find.len == 0) return;
+ int diff = replace.len - find.len;
+ char *readFrom = buffer;
+ char *foundAt;
+ if (diff == 0) {
+ while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
+ memcpy(foundAt, replace.buffer, replace.len);
+ readFrom = foundAt + replace.len;
+ }
+ } else if (diff < 0) {
+ char *writeTo = buffer;
+ while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
+ unsigned int n = foundAt - readFrom;
+ memcpy(writeTo, readFrom, n);
+ writeTo += n;
+ memcpy(writeTo, replace.buffer, replace.len);
+ writeTo += replace.len;
+ readFrom = foundAt + find.len;
+ len += diff;
+ }
+ strcpy(writeTo, readFrom);
+ } else {
+ unsigned int size = len; // compute size needed for result
+ while ((foundAt = strstr(readFrom, find.buffer)) != NULL) {
+ readFrom = foundAt + find.len;
+ size += diff;
+ }
+ if (size == len) return;
+ if (size > capacity && !changeBuffer(size)) return; // XXX: tell user!
+ int index = len - 1;
+ while (index >= 0 && (index = lastIndexOf(find, index)) >= 0) {
+ readFrom = buffer + index + find.len;
+ memmove(readFrom + diff, readFrom, len - (readFrom - buffer));
+ len += diff;
+ buffer[len] = 0;
+ memcpy(buffer + index, replace.buffer, replace.len);
+ index--;
+ }
+ }
+}
+
+void String::toLowerCase(void)
+{
+ if (!buffer) return;
+ for (char *p = buffer; *p; p++) {
+ *p = tolower(*p);
+ }
+}
+
+void String::toUpperCase(void)
+{
+ if (!buffer) return;
+ for (char *p = buffer; *p; p++) {
+ *p = toupper(*p);
+ }
+}
+
+void String::trim(void)
+{
+ if (!buffer || len == 0) return;
+ char *begin = buffer;
+ while (isspace(*begin)) begin++;
+ char *end = buffer + len - 1;
+ while (isspace(*end) && end >= begin) end--;
+ len = end + 1 - begin;
+ if (begin > buffer) memcpy(buffer, begin, len);
+ buffer[len] = 0;
+}
+
+/*********************************************/
+/* Parsing / Conversion */
+/*********************************************/
+
+long String::toInt(void) const
+{
+ if (buffer) return atol(buffer);
+ return 0;
+}
+
+
diff --git a/cores/robot/WString.h b/cores/robot/WString.h
new file mode 100644
index 0000000..642b016
--- /dev/null
+++ b/cores/robot/WString.h
@@ -0,0 +1,206 @@
+/*
+ WString.h - String library for Wiring & Arduino
+ ...mostly rewritten by Paul Stoffregen...
+ Copyright (c) 2009-10 Hernando Barragan. All right reserved.
+ Copyright 2011, Paul Stoffregen, paul@pjrc.com
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#ifndef String_class_h
+#define String_class_h
+#ifdef __cplusplus
+
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <avr/pgmspace.h>
+
+// When compiling programs with this class, the following gcc parameters
+// dramatically increase performance and memory (RAM) efficiency, typically
+// with little or no increase in code size.
+// -felide-constructors
+// -std=c++0x
+
+class __FlashStringHelper;
+#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
+
+// An inherited class for holding the result of a concatenation. These
+// result objects are assumed to be writable by subsequent concatenations.
+class StringSumHelper;
+
+// The string class
+class String
+{
+ // use a function pointer to allow for "if (s)" without the
+ // complications of an operator bool(). for more information, see:
+ // http://www.artima.com/cppsource/safebool.html
+ typedef void (String::*StringIfHelperType)() const;
+ void StringIfHelper() const {}
+
+public:
+ // constructors
+ // creates a copy of the initial value.
+ // if the initial value is null or invalid, or if memory allocation
+ // fails, the string will be marked as invalid (i.e. "if (s)" will
+ // be false).
+ String(const char *cstr = "");
+ String(const String &str);
+ #ifdef __GXX_EXPERIMENTAL_CXX0X__
+ String(String &&rval);
+ String(StringSumHelper &&rval);
+ #endif
+ explicit String(char c);
+ explicit String(unsigned char, unsigned char base=10);
+ explicit String(int, unsigned char base=10);
+ explicit String(unsigned int, unsigned char base=10);
+ explicit String(long, unsigned char base=10);
+ explicit String(unsigned long, unsigned char base=10);
+ ~String(void);
+
+ // memory management
+ // return true on success, false on failure (in which case, the string
+ // is left unchanged). reserve(0), if successful, will validate an
+ // invalid string (i.e., "if (s)" will be true afterwards)
+ unsigned char reserve(unsigned int size);
+ inline unsigned int length(void) const {return len;}
+
+ // creates a copy of the assigned value. if the value is null or
+ // invalid, or if the memory allocation fails, the string will be
+ // marked as invalid ("if (s)" will be false).
+ String & operator = (const String &rhs);
+ String & operator = (const char *cstr);
+ #ifdef __GXX_EXPERIMENTAL_CXX0X__
+ String & operator = (String &&rval);
+ String & operator = (StringSumHelper &&rval);
+ #endif
+
+ // concatenate (works w/ built-in types)
+
+ // returns true on success, false on failure (in which case, the string
+ // is left unchanged). if the argument is null or invalid, the
+ // concatenation is considered unsucessful.
+ unsigned char concat(const String &str);
+ unsigned char concat(const char *cstr);
+ unsigned char concat(char c);
+ unsigned char concat(unsigned char c);
+ unsigned char concat(int num);
+ unsigned char concat(unsigned int num);
+ unsigned char concat(long num);
+ unsigned char concat(unsigned long num);
+
+ // if there's not enough memory for the concatenated value, the string
+ // will be left unchanged (but this isn't signalled in any way)
+ String & operator += (const String &rhs) {concat(rhs); return (*this);}
+ String & operator += (const char *cstr) {concat(cstr); return (*this);}
+ String & operator += (char c) {concat(c); return (*this);}
+ String & operator += (unsigned char num) {concat(num); return (*this);}
+ String & operator += (int num) {concat(num); return (*this);}
+ String & operator += (unsigned int num) {concat(num); return (*this);}
+ String & operator += (long num) {concat(num); return (*this);}
+ String & operator += (unsigned long num) {concat(num); return (*this);}
+
+ friend StringSumHelper & operator + (const StringSumHelper &lhs, const String &rhs);
+ friend StringSumHelper & operator + (const StringSumHelper &lhs, const char *cstr);
+ friend StringSumHelper & operator + (const StringSumHelper &lhs, char c);
+ friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned char num);
+ friend StringSumHelper & operator + (const StringSumHelper &lhs, int num);
+ friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned int num);
+ friend StringSumHelper & operator + (const StringSumHelper &lhs, long num);
+ friend StringSumHelper & operator + (const StringSumHelper &lhs, unsigned long num);
+
+ // comparison (only works w/ Strings and "strings")
+ operator StringIfHelperType() const { return buffer ? &String::StringIfHelper : 0; }
+ int compareTo(const String &s) const;
+ unsigned char equals(const String &s) const;
+ unsigned char equals(const char *cstr) const;
+ unsigned char operator == (const String &rhs) const {return equals(rhs);}
+ unsigned char operator == (const char *cstr) const {return equals(cstr);}
+ unsigned char operator != (const String &rhs) const {return !equals(rhs);}
+ unsigned char operator != (const char *cstr) const {return !equals(cstr);}
+ unsigned char operator < (const String &rhs) const;
+ unsigned char operator > (const String &rhs) const;
+ unsigned char operator <= (const String &rhs) const;
+ unsigned char operator >= (const String &rhs) const;
+ unsigned char equalsIgnoreCase(const String &s) const;
+ unsigned char startsWith( const String &prefix) const;
+ unsigned char startsWith(const String &prefix, unsigned int offset) const;
+ unsigned char endsWith(const String &suffix) const;
+
+ // character acccess
+ char charAt(unsigned int index) const;
+ void setCharAt(unsigned int index, char c);
+ char operator [] (unsigned int index) const;
+ char& operator [] (unsigned int index);
+ void getBytes(unsigned char *buf, unsigned int bufsize, unsigned int index=0) const;
+ void toCharArray(char *buf, unsigned int bufsize, unsigned int index=0) const
+ {getBytes((unsigned char *)buf, bufsize, index);}
+ const char * c_str() const { return buffer; }
+
+ // search
+ int indexOf( char ch ) const;
+ int indexOf( char ch, unsigned int fromIndex ) const;
+ int indexOf( const String &str ) const;
+ int indexOf( const String &str, unsigned int fromIndex ) const;
+ int lastIndexOf( char ch ) const;
+ int lastIndexOf( char ch, unsigned int fromIndex ) const;
+ int lastIndexOf( const String &str ) const;
+ int lastIndexOf( const String &str, unsigned int fromIndex ) const;
+ String substring( unsigned int beginIndex ) const;
+ String substring( unsigned int beginIndex, unsigned int endIndex ) const;
+
+ // modification
+ void replace(char find, char replace);
+ void replace(const String& find, const String& replace);
+ void toLowerCase(void);
+ void toUpperCase(void);
+ void trim(void);
+
+ // parsing/conversion
+ long toInt(void) const;
+
+protected:
+ char *buffer; // the actual char array
+ unsigned int capacity; // the array length minus one (for the '\0')
+ unsigned int len; // the String length (not counting the '\0')
+ unsigned char flags; // unused, for future features
+protected:
+ void init(void);
+ void invalidate(void);
+ unsigned char changeBuffer(unsigned int maxStrLen);
+ unsigned char concat(const char *cstr, unsigned int length);
+
+ // copy and move
+ String & copy(const char *cstr, unsigned int length);
+ #ifdef __GXX_EXPERIMENTAL_CXX0X__
+ void move(String &rhs);
+ #endif
+};
+
+class StringSumHelper : public String
+{
+public:
+ StringSumHelper(const String &s) : String(s) {}
+ StringSumHelper(const char *p) : String(p) {}
+ StringSumHelper(char c) : String(c) {}
+ StringSumHelper(unsigned char num) : String(num) {}
+ StringSumHelper(int num) : String(num) {}
+ StringSumHelper(unsigned int num) : String(num) {}
+ StringSumHelper(long num) : String(num) {}
+ StringSumHelper(unsigned long num) : String(num) {}
+};
+
+#endif // __cplusplus
+#endif // String_class_h
diff --git a/cores/robot/binary.h b/cores/robot/binary.h
new file mode 100644
index 0000000..af14980
--- /dev/null
+++ b/cores/robot/binary.h
@@ -0,0 +1,515 @@
+#ifndef Binary_h
+#define Binary_h
+
+#define B0 0
+#define B00 0
+#define B000 0
+#define B0000 0
+#define B00000 0
+#define B000000 0
+#define B0000000 0
+#define B00000000 0
+#define B1 1
+#define B01 1
+#define B001 1
+#define B0001 1
+#define B00001 1
+#define B000001 1
+#define B0000001 1
+#define B00000001 1
+#define B10 2
+#define B010 2
+#define B0010 2
+#define B00010 2
+#define B000010 2
+#define B0000010 2
+#define B00000010 2
+#define B11 3
+#define B011 3
+#define B0011 3
+#define B00011 3
+#define B000011 3
+#define B0000011 3
+#define B00000011 3
+#define B100 4
+#define B0100 4
+#define B00100 4
+#define B000100 4
+#define B0000100 4
+#define B00000100 4
+#define B101 5
+#define B0101 5
+#define B00101 5
+#define B000101 5
+#define B0000101 5
+#define B00000101 5
+#define B110 6
+#define B0110 6
+#define B00110 6
+#define B000110 6
+#define B0000110 6
+#define B00000110 6
+#define B111 7
+#define B0111 7
+#define B00111 7
+#define B000111 7
+#define B0000111 7
+#define B00000111 7
+#define B1000 8
+#define B01000 8
+#define B001000 8
+#define B0001000 8
+#define B00001000 8
+#define B1001 9
+#define B01001 9
+#define B001001 9
+#define B0001001 9
+#define B00001001 9
+#define B1010 10
+#define B01010 10
+#define B001010 10
+#define B0001010 10
+#define B00001010 10
+#define B1011 11
+#define B01011 11
+#define B001011 11
+#define B0001011 11
+#define B00001011 11
+#define B1100 12
+#define B01100 12
+#define B001100 12
+#define B0001100 12
+#define B00001100 12
+#define B1101 13
+#define B01101 13
+#define B001101 13
+#define B0001101 13
+#define B00001101 13
+#define B1110 14
+#define B01110 14
+#define B001110 14
+#define B0001110 14
+#define B00001110 14
+#define B1111 15
+#define B01111 15
+#define B001111 15
+#define B0001111 15
+#define B00001111 15
+#define B10000 16
+#define B010000 16
+#define B0010000 16
+#define B00010000 16
+#define B10001 17
+#define B010001 17
+#define B0010001 17
+#define B00010001 17
+#define B10010 18
+#define B010010 18
+#define B0010010 18
+#define B00010010 18
+#define B10011 19
+#define B010011 19
+#define B0010011 19
+#define B00010011 19
+#define B10100 20
+#define B010100 20
+#define B0010100 20
+#define B00010100 20
+#define B10101 21
+#define B010101 21
+#define B0010101 21
+#define B00010101 21
+#define B10110 22
+#define B010110 22
+#define B0010110 22
+#define B00010110 22
+#define B10111 23
+#define B010111 23
+#define B0010111 23
+#define B00010111 23
+#define B11000 24
+#define B011000 24
+#define B0011000 24
+#define B00011000 24
+#define B11001 25
+#define B011001 25
+#define B0011001 25
+#define B00011001 25
+#define B11010 26
+#define B011010 26
+#define B0011010 26
+#define B00011010 26
+#define B11011 27
+#define B011011 27
+#define B0011011 27
+#define B00011011 27
+#define B11100 28
+#define B011100 28
+#define B0011100 28
+#define B00011100 28
+#define B11101 29
+#define B011101 29
+#define B0011101 29
+#define B00011101 29
+#define B11110 30
+#define B011110 30
+#define B0011110 30
+#define B00011110 30
+#define B11111 31
+#define B011111 31
+#define B0011111 31
+#define B00011111 31
+#define B100000 32
+#define B0100000 32
+#define B00100000 32
+#define B100001 33
+#define B0100001 33
+#define B00100001 33
+#define B100010 34
+#define B0100010 34
+#define B00100010 34
+#define B100011 35
+#define B0100011 35
+#define B00100011 35
+#define B100100 36
+#define B0100100 36
+#define B00100100 36
+#define B100101 37
+#define B0100101 37
+#define B00100101 37
+#define B100110 38
+#define B0100110 38
+#define B00100110 38
+#define B100111 39
+#define B0100111 39
+#define B00100111 39
+#define B101000 40
+#define B0101000 40
+#define B00101000 40
+#define B101001 41
+#define B0101001 41
+#define B00101001 41
+#define B101010 42
+#define B0101010 42
+#define B00101010 42
+#define B101011 43
+#define B0101011 43
+#define B00101011 43
+#define B101100 44
+#define B0101100 44
+#define B00101100 44
+#define B101101 45
+#define B0101101 45
+#define B00101101 45
+#define B101110 46
+#define B0101110 46
+#define B00101110 46
+#define B101111 47
+#define B0101111 47
+#define B00101111 47
+#define B110000 48
+#define B0110000 48
+#define B00110000 48
+#define B110001 49
+#define B0110001 49
+#define B00110001 49
+#define B110010 50
+#define B0110010 50
+#define B00110010 50
+#define B110011 51
+#define B0110011 51
+#define B00110011 51
+#define B110100 52
+#define B0110100 52
+#define B00110100 52
+#define B110101 53
+#define B0110101 53
+#define B00110101 53
+#define B110110 54
+#define B0110110 54
+#define B00110110 54
+#define B110111 55
+#define B0110111 55
+#define B00110111 55
+#define B111000 56
+#define B0111000 56
+#define B00111000 56
+#define B111001 57
+#define B0111001 57
+#define B00111001 57
+#define B111010 58
+#define B0111010 58
+#define B00111010 58
+#define B111011 59
+#define B0111011 59
+#define B00111011 59
+#define B111100 60
+#define B0111100 60
+#define B00111100 60
+#define B111101 61
+#define B0111101 61
+#define B00111101 61
+#define B111110 62
+#define B0111110 62
+#define B00111110 62
+#define B111111 63
+#define B0111111 63
+#define B00111111 63
+#define B1000000 64
+#define B01000000 64
+#define B1000001 65
+#define B01000001 65
+#define B1000010 66
+#define B01000010 66
+#define B1000011 67
+#define B01000011 67
+#define B1000100 68
+#define B01000100 68
+#define B1000101 69
+#define B01000101 69
+#define B1000110 70
+#define B01000110 70
+#define B1000111 71
+#define B01000111 71
+#define B1001000 72
+#define B01001000 72
+#define B1001001 73
+#define B01001001 73
+#define B1001010 74
+#define B01001010 74
+#define B1001011 75
+#define B01001011 75
+#define B1001100 76
+#define B01001100 76
+#define B1001101 77
+#define B01001101 77
+#define B1001110 78
+#define B01001110 78
+#define B1001111 79
+#define B01001111 79
+#define B1010000 80
+#define B01010000 80
+#define B1010001 81
+#define B01010001 81
+#define B1010010 82
+#define B01010010 82
+#define B1010011 83
+#define B01010011 83
+#define B1010100 84
+#define B01010100 84
+#define B1010101 85
+#define B01010101 85
+#define B1010110 86
+#define B01010110 86
+#define B1010111 87
+#define B01010111 87
+#define B1011000 88
+#define B01011000 88
+#define B1011001 89
+#define B01011001 89
+#define B1011010 90
+#define B01011010 90
+#define B1011011 91
+#define B01011011 91
+#define B1011100 92
+#define B01011100 92
+#define B1011101 93
+#define B01011101 93
+#define B1011110 94
+#define B01011110 94
+#define B1011111 95
+#define B01011111 95
+#define B1100000 96
+#define B01100000 96
+#define B1100001 97
+#define B01100001 97
+#define B1100010 98
+#define B01100010 98
+#define B1100011 99
+#define B01100011 99
+#define B1100100 100
+#define B01100100 100
+#define B1100101 101
+#define B01100101 101
+#define B1100110 102
+#define B01100110 102
+#define B1100111 103
+#define B01100111 103
+#define B1101000 104
+#define B01101000 104
+#define B1101001 105
+#define B01101001 105
+#define B1101010 106
+#define B01101010 106
+#define B1101011 107
+#define B01101011 107
+#define B1101100 108
+#define B01101100 108
+#define B1101101 109
+#define B01101101 109
+#define B1101110 110
+#define B01101110 110
+#define B1101111 111
+#define B01101111 111
+#define B1110000 112
+#define B01110000 112
+#define B1110001 113
+#define B01110001 113
+#define B1110010 114
+#define B01110010 114
+#define B1110011 115
+#define B01110011 115
+#define B1110100 116
+#define B01110100 116
+#define B1110101 117
+#define B01110101 117
+#define B1110110 118
+#define B01110110 118
+#define B1110111 119
+#define B01110111 119
+#define B1111000 120
+#define B01111000 120
+#define B1111001 121
+#define B01111001 121
+#define B1111010 122
+#define B01111010 122
+#define B1111011 123
+#define B01111011 123
+#define B1111100 124
+#define B01111100 124
+#define B1111101 125
+#define B01111101 125
+#define B1111110 126
+#define B01111110 126
+#define B1111111 127
+#define B01111111 127
+#define B10000000 128
+#define B10000001 129
+#define B10000010 130
+#define B10000011 131
+#define B10000100 132
+#define B10000101 133
+#define B10000110 134
+#define B10000111 135
+#define B10001000 136
+#define B10001001 137
+#define B10001010 138
+#define B10001011 139
+#define B10001100 140
+#define B10001101 141
+#define B10001110 142
+#define B10001111 143
+#define B10010000 144
+#define B10010001 145
+#define B10010010 146
+#define B10010011 147
+#define B10010100 148
+#define B10010101 149
+#define B10010110 150
+#define B10010111 151
+#define B10011000 152
+#define B10011001 153
+#define B10011010 154
+#define B10011011 155
+#define B10011100 156
+#define B10011101 157
+#define B10011110 158
+#define B10011111 159
+#define B10100000 160
+#define B10100001 161
+#define B10100010 162
+#define B10100011 163
+#define B10100100 164
+#define B10100101 165
+#define B10100110 166
+#define B10100111 167
+#define B10101000 168
+#define B10101001 169
+#define B10101010 170
+#define B10101011 171
+#define B10101100 172
+#define B10101101 173
+#define B10101110 174
+#define B10101111 175
+#define B10110000 176
+#define B10110001 177
+#define B10110010 178
+#define B10110011 179
+#define B10110100 180
+#define B10110101 181
+#define B10110110 182
+#define B10110111 183
+#define B10111000 184
+#define B10111001 185
+#define B10111010 186
+#define B10111011 187
+#define B10111100 188
+#define B10111101 189
+#define B10111110 190
+#define B10111111 191
+#define B11000000 192
+#define B11000001 193
+#define B11000010 194
+#define B11000011 195
+#define B11000100 196
+#define B11000101 197
+#define B11000110 198
+#define B11000111 199
+#define B11001000 200
+#define B11001001 201
+#define B11001010 202
+#define B11001011 203
+#define B11001100 204
+#define B11001101 205
+#define B11001110 206
+#define B11001111 207
+#define B11010000 208
+#define B11010001 209
+#define B11010010 210
+#define B11010011 211
+#define B11010100 212
+#define B11010101 213
+#define B11010110 214
+#define B11010111 215
+#define B11011000 216
+#define B11011001 217
+#define B11011010 218
+#define B11011011 219
+#define B11011100 220
+#define B11011101 221
+#define B11011110 222
+#define B11011111 223
+#define B11100000 224
+#define B11100001 225
+#define B11100010 226
+#define B11100011 227
+#define B11100100 228
+#define B11100101 229
+#define B11100110 230
+#define B11100111 231
+#define B11101000 232
+#define B11101001 233
+#define B11101010 234
+#define B11101011 235
+#define B11101100 236
+#define B11101101 237
+#define B11101110 238
+#define B11101111 239
+#define B11110000 240
+#define B11110001 241
+#define B11110010 242
+#define B11110011 243
+#define B11110100 244
+#define B11110101 245
+#define B11110110 246
+#define B11110111 247
+#define B11111000 248
+#define B11111001 249
+#define B11111010 250
+#define B11111011 251
+#define B11111100 252
+#define B11111101 253
+#define B11111110 254
+#define B11111111 255
+
+#endif
diff --git a/cores/robot/main.cpp b/cores/robot/main.cpp
new file mode 100644
index 0000000..3d4e079
--- /dev/null
+++ b/cores/robot/main.cpp
@@ -0,0 +1,20 @@
+#include <Arduino.h>
+
+int main(void)
+{
+ init();
+
+#if defined(USBCON)
+ USBDevice.attach();
+#endif
+
+ setup();
+
+ for (;;) {
+ loop();
+ if (serialEventRun) serialEventRun();
+ }
+
+ return 0;
+}
+
diff --git a/cores/robot/new.cpp b/cores/robot/new.cpp
new file mode 100644
index 0000000..b81031e
--- /dev/null
+++ b/cores/robot/new.cpp
@@ -0,0 +1,28 @@
+#include <new.h>
+
+void * operator new(size_t size)
+{
+ return malloc(size);
+}
+
+void * operator new[](size_t size)
+{
+ return malloc(size);
+}
+
+void operator delete(void * ptr)
+{
+ free(ptr);
+}
+
+void operator delete[](void * ptr)
+{
+ free(ptr);
+}
+
+int __cxa_guard_acquire(__guard *g) {return !*(char *)(g);};
+void __cxa_guard_release (__guard *g) {*(char *)g = 1;};
+void __cxa_guard_abort (__guard *) {};
+
+void __cxa_pure_virtual(void) {};
+
diff --git a/cores/robot/new.h b/cores/robot/new.h
new file mode 100644
index 0000000..991c86c
--- /dev/null
+++ b/cores/robot/new.h
@@ -0,0 +1,24 @@
+/* Header to define new/delete operators as they aren't provided by avr-gcc by default
+ Taken from http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59453
+ */
+
+#ifndef NEW_H
+#define NEW_H
+
+#include <stdlib.h>
+
+void * operator new(size_t size);
+void * operator new[](size_t size);
+void operator delete(void * ptr);
+void operator delete[](void * ptr);
+
+__extension__ typedef int __guard __attribute__((mode (__DI__)));
+
+extern "C" int __cxa_guard_acquire(__guard *);
+extern "C" void __cxa_guard_release (__guard *);
+extern "C" void __cxa_guard_abort (__guard *);
+
+extern "C" void __cxa_pure_virtual(void);
+
+#endif
+
diff --git a/cores/robot/wiring.c b/cores/robot/wiring.c
new file mode 100644
index 0000000..a3c4390
--- /dev/null
+++ b/cores/robot/wiring.c
@@ -0,0 +1,324 @@
+/*
+ wiring.c - Partial implementation of the Wiring API for the ATmega8.
+ Part of Arduino - http://www.arduino.cc/
+
+ Copyright (c) 2005-2006 David A. Mellis
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+
+ $Id$
+*/
+
+#include "wiring_private.h"
+
+// the prescaler is set so that timer0 ticks every 64 clock cycles, and the
+// the overflow handler is called every 256 ticks.
+#define MICROSECONDS_PER_TIMER0_OVERFLOW (clockCyclesToMicroseconds(64 * 256))
+
+// the whole number of milliseconds per timer0 overflow
+#define MILLIS_INC (MICROSECONDS_PER_TIMER0_OVERFLOW / 1000)
+
+// the fractional number of milliseconds per timer0 overflow. we shift right
+// by three to fit these numbers into a byte. (for the clock speeds we care
+// about - 8 and 16 MHz - this doesn't lose precision.)
+#define FRACT_INC ((MICROSECONDS_PER_TIMER0_OVERFLOW % 1000) >> 3)
+#define FRACT_MAX (1000 >> 3)
+
+volatile unsigned long timer0_overflow_count = 0;
+volatile unsigned long timer0_millis = 0;
+static unsigned char timer0_fract = 0;
+
+#if defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
+ISR(TIM0_OVF_vect)
+#else
+ISR(TIMER0_OVF_vect)
+#endif
+{
+ // copy these to local variables so they can be stored in registers
+ // (volatile variables must be read from memory on every access)
+ unsigned long m = timer0_millis;
+ unsigned char f = timer0_fract;
+
+ m += MILLIS_INC;
+ f += FRACT_INC;
+ if (f >= FRACT_MAX) {
+ f -= FRACT_MAX;
+ m += 1;
+ }
+
+ timer0_fract = f;
+ timer0_millis = m;
+ timer0_overflow_count++;
+}
+
+unsigned long millis()
+{
+ unsigned long m;
+ uint8_t oldSREG = SREG;
+
+ // disable interrupts while we read timer0_millis or we might get an
+ // inconsistent value (e.g. in the middle of a write to timer0_millis)
+ cli();
+ m = timer0_millis;
+ SREG = oldSREG;
+
+ return m;
+}
+
+unsigned long micros() {
+ unsigned long m;
+ uint8_t oldSREG = SREG, t;
+
+ cli();
+ m = timer0_overflow_count;
+#if defined(TCNT0)
+ t = TCNT0;
+#elif defined(TCNT0L)
+ t = TCNT0L;
+#else
+ #error TIMER 0 not defined
+#endif
+
+
+#ifdef TIFR0
+ if ((TIFR0 & _BV(TOV0)) && (t < 255))
+ m++;
+#else
+ if ((TIFR & _BV(TOV0)) && (t < 255))
+ m++;
+#endif
+
+ SREG = oldSREG;
+
+ return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
+}
+
+void delay(unsigned long ms)
+{
+ uint16_t start = (uint16_t)micros();
+
+ while (ms > 0) {
+ if (((uint16_t)micros() - start) >= 1000) {
+ ms--;
+ start += 1000;
+ }
+ }
+}
+
+/* Delay for the given number of microseconds. Assumes a 8 or 16 MHz clock. */
+void delayMicroseconds(unsigned int us)
+{
+ // calling avrlib's delay_us() function with low values (e.g. 1 or
+ // 2 microseconds) gives delays longer than desired.
+ //delay_us(us);
+#if F_CPU >= 20000000L
+ // for the 20 MHz clock on rare Arduino boards
+
+ // for a one-microsecond delay, simply wait 2 cycle and return. The overhead
+ // of the function call yields a delay of exactly a one microsecond.
+ __asm__ __volatile__ (
+ "nop" "\n\t"
+ "nop"); //just waiting 2 cycle
+ if (--us == 0)
+ return;
+
+ // the following loop takes a 1/5 of a microsecond (4 cycles)
+ // per iteration, so execute it five times for each microsecond of
+ // delay requested.
+ us = (us<<2) + us; // x5 us
+
+ // account for the time taken in the preceeding commands.
+ us -= 2;
+
+#elif F_CPU >= 16000000L
+ // for the 16 MHz clock on most Arduino boards
+
+ // for a one-microsecond delay, simply return. the overhead
+ // of the function call yields a delay of approximately 1 1/8 us.
+ if (--us == 0)
+ return;
+
+ // the following loop takes a quarter of a microsecond (4 cycles)
+ // per iteration, so execute it four times for each microsecond of
+ // delay requested.
+ us <<= 2;
+
+ // account for the time taken in the preceeding commands.
+ us -= 2;
+#else
+ // for the 8 MHz internal clock on the ATmega168
+
+ // for a one- or two-microsecond delay, simply return. the overhead of
+ // the function calls takes more than two microseconds. can't just
+ // subtract two, since us is unsigned; we'd overflow.
+ if (--us == 0)
+ return;
+ if (--us == 0)
+ return;
+
+ // the following loop takes half of a microsecond (4 cycles)
+ // per iteration, so execute it twice for each microsecond of
+ // delay requested.
+ us <<= 1;
+
+ // partially compensate for the time taken by the preceeding commands.
+ // we can't subtract any more than this or we'd overflow w/ small delays.
+ us--;
+#endif
+
+ // busy wait
+ __asm__ __volatile__ (
+ "1: sbiw %0,1" "\n\t" // 2 cycles
+ "brne 1b" : "=w" (us) : "0" (us) // 2 cycles
+ );
+}
+
+void init()
+{
+ // this needs to be called before setup() or some functions won't
+ // work there
+ sei();
+
+ // on the ATmega168, timer 0 is also used for fast hardware pwm
+ // (using phase-correct PWM would mean that timer 0 overflowed half as often
+ // resulting in different millis() behavior on the ATmega8 and ATmega168)
+#if defined(TCCR0A) && defined(WGM01)
+ sbi(TCCR0A, WGM01);
+ sbi(TCCR0A, WGM00);
+#endif
+
+ // set timer 0 prescale factor to 64
+#if defined(__AVR_ATmega128__)
+ // CPU specific: different values for the ATmega128
+ sbi(TCCR0, CS02);
+#elif defined(TCCR0) && defined(CS01) && defined(CS00)
+ // this combination is for the standard atmega8
+ sbi(TCCR0, CS01);
+ sbi(TCCR0, CS00);
+#elif defined(TCCR0B) && defined(CS01) && defined(CS00)
+ // this combination is for the standard 168/328/1280/2560
+ sbi(TCCR0B, CS01);
+ sbi(TCCR0B, CS00);
+#elif defined(TCCR0A) && defined(CS01) && defined(CS00)
+ // this combination is for the __AVR_ATmega645__ series
+ sbi(TCCR0A, CS01);
+ sbi(TCCR0A, CS00);
+#else
+ #error Timer 0 prescale factor 64 not set correctly
+#endif
+
+ // enable timer 0 overflow interrupt
+#if defined(TIMSK) && defined(TOIE0)
+ sbi(TIMSK, TOIE0);
+#elif defined(TIMSK0) && defined(TOIE0)
+ sbi(TIMSK0, TOIE0);
+#else
+ #error Timer 0 overflow interrupt not set correctly
+#endif
+
+ // timers 1 and 2 are used for phase-correct hardware pwm
+ // this is better for motors as it ensures an even waveform
+ // note, however, that fast pwm mode can achieve a frequency of up
+ // 8 MHz (with a 16 MHz clock) at 50% duty cycle
+
+#if defined(TCCR1B) && defined(CS11) && defined(CS10)
+ TCCR1B = 0;
+
+ // set timer 1 prescale factor to 64
+ sbi(TCCR1B, CS11);
+#if F_CPU >= 8000000L
+ sbi(TCCR1B, CS10);
+#endif
+#elif defined(TCCR1) && defined(CS11) && defined(CS10)
+ sbi(TCCR1, CS11);
+#if F_CPU >= 8000000L
+ sbi(TCCR1, CS10);
+#endif
+#endif
+ // put timer 1 in 8-bit phase correct pwm mode
+#if defined(TCCR1A) && defined(WGM10)
+ sbi(TCCR1A, WGM10);
+#elif defined(TCCR1)
+ #warning this needs to be finished
+#endif
+
+ // set timer 2 prescale factor to 64
+#if defined(TCCR2) && defined(CS22)
+ sbi(TCCR2, CS22);
+#elif defined(TCCR2B) && defined(CS22)
+ sbi(TCCR2B, CS22);
+#else
+ #warning Timer 2 not finished (may not be present on this CPU)
+#endif
+
+ // configure timer 2 for phase correct pwm (8-bit)
+#if defined(TCCR2) && defined(WGM20)
+ sbi(TCCR2, WGM20);
+#elif defined(TCCR2A) && defined(WGM20)
+ sbi(TCCR2A, WGM20);
+#else
+ #warning Timer 2 not finished (may not be present on this CPU)
+#endif
+
+#if defined(TCCR3B) && defined(CS31) && defined(WGM30)
+ sbi(TCCR3B, CS31); // set timer 3 prescale factor to 64
+ sbi(TCCR3B, CS30);
+ sbi(TCCR3A, WGM30); // put timer 3 in 8-bit phase correct pwm mode
+#endif
+
+#if defined(TCCR4A) && defined(TCCR4B) && defined(TCCR4D) /* beginning of timer4 block for 32U4 and similar */
+ sbi(TCCR4B, CS42); // set timer4 prescale factor to 64
+ sbi(TCCR4B, CS41);
+ sbi(TCCR4B, CS40);
+ sbi(TCCR4D, WGM40); // put timer 4 in phase- and frequency-correct PWM mode
+ sbi(TCCR4A, PWM4A); // enable PWM mode for comparator OCR4A
+ sbi(TCCR4C, PWM4D); // enable PWM mode for comparator OCR4D
+#else /* beginning of timer4 block for ATMEGA1280 and ATMEGA2560 */
+#if defined(TCCR4B) && defined(CS41) && defined(WGM40)
+ sbi(TCCR4B, CS41); // set timer 4 prescale factor to 64
+ sbi(TCCR4B, CS40);
+ sbi(TCCR4A, WGM40); // put timer 4 in 8-bit phase correct pwm mode
+#endif
+#endif /* end timer4 block for ATMEGA1280/2560 and similar */
+
+#if defined(TCCR5B) && defined(CS51) && defined(WGM50)
+ sbi(TCCR5B, CS51); // set timer 5 prescale factor to 64
+ sbi(TCCR5B, CS50);
+ sbi(TCCR5A, WGM50); // put timer 5 in 8-bit phase correct pwm mode
+#endif
+
+#if defined(ADCSRA)
+ // set a2d prescale factor to 128
+ // 16 MHz / 128 = 125 KHz, inside the desired 50-200 KHz range.
+ // XXX: this will not work properly for other clock speeds, and
+ // this code should use F_CPU to determine the prescale factor.
+ sbi(ADCSRA, ADPS2);
+ sbi(ADCSRA, ADPS1);
+ sbi(ADCSRA, ADPS0);
+
+ // enable a2d conversions
+ sbi(ADCSRA, ADEN);
+#endif
+
+ // the bootloader connects pins 0 and 1 to the USART; disconnect them
+ // here so they can be used as normal digital i/o; they will be
+ // reconnected in Serial.begin()
+#if defined(UCSRB)
+ UCSRB = 0;
+#elif defined(UCSR0B)
+ UCSR0B = 0;
+#endif
+}
diff --git a/cores/robot/wiring_analog.c b/cores/robot/wiring_analog.c
new file mode 100644
index 0000000..7ed0e4e
--- /dev/null
+++ b/cores/robot/wiring_analog.c
@@ -0,0 +1,284 @@
+/*
+ wiring_analog.c - analog input and output
+ Part of Arduino - http://www.arduino.cc/
+
+ Copyright (c) 2005-2006 David A. Mellis
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+
+ Modified 28 September 2010 by Mark Sproul
+
+ $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
+*/
+
+#include "wiring_private.h"
+#include "pins_arduino.h"
+
+uint8_t analog_reference = DEFAULT;
+
+void analogReference(uint8_t mode)
+{
+ // can't actually set the register here because the default setting
+ // will connect AVCC and the AREF pin, which would cause a short if
+ // there's something connected to AREF.
+ analog_reference = mode;
+}
+
+int analogRead(uint8_t pin)
+{
+ uint8_t low, high;
+
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
+ if (pin >= 54) pin -= 54; // allow for channel or pin numbers
+#elif defined(__AVR_ATmega32U4__)
+ if (pin >= 18) pin -= 18; // allow for channel or pin numbers
+#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
+ if (pin >= 24) pin -= 24; // allow for channel or pin numbers
+#elif defined(analogPinToChannel) && (defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__))
+ pin = analogPinToChannel(pin);
+#else
+ if (pin >= 14) pin -= 14; // allow for channel or pin numbers
+#endif
+
+#if defined(__AVR_ATmega32U4__)
+ pin = analogPinToChannel(pin);
+ ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
+#elif defined(ADCSRB) && defined(MUX5)
+ // the MUX5 bit of ADCSRB selects whether we're reading from channels
+ // 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
+ ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
+#endif
+
+ // set the analog reference (high two bits of ADMUX) and select the
+ // channel (low 4 bits). this also sets ADLAR (left-adjust result)
+ // to 0 (the default).
+#if defined(ADMUX)
+ ADMUX = (analog_reference << 6) | (pin & 0x07);
+#endif
+
+ // without a delay, we seem to read from the wrong channel
+ //delay(1);
+
+#if defined(ADCSRA) && defined(ADCL)
+ // start the conversion
+ sbi(ADCSRA, ADSC);
+
+ // ADSC is cleared when the conversion finishes
+ while (bit_is_set(ADCSRA, ADSC));
+
+ // we have to read ADCL first; doing so locks both ADCL
+ // and ADCH until ADCH is read. reading ADCL second would
+ // cause the results of each conversion to be discarded,
+ // as ADCL and ADCH would be locked when it completed.
+ low = ADCL;
+ high = ADCH;
+#else
+ // we dont have an ADC, return 0
+ low = 0;
+ high = 0;
+#endif
+
+ // combine the two bytes
+ return (high << 8) | low;
+}
+
+// Right now, PWM output only works on the pins with
+// hardware support. These are defined in the appropriate
+// pins_*.c file. For the rest of the pins, we default
+// to digital output.
+void analogWrite(uint8_t pin, int val)
+{
+ // We need to make sure the PWM output is enabled for those pins
+ // that support it, as we turn it off when digitally reading or
+ // writing with them. Also, make sure the pin is in output mode
+ // for consistenty with Wiring, which doesn't require a pinMode
+ // call for the analog output pins.
+ pinMode(pin, OUTPUT);
+ if (val == 0)
+ {
+ digitalWrite(pin, LOW);
+ }
+ else if (val == 255)
+ {
+ digitalWrite(pin, HIGH);
+ }
+ else
+ {
+ switch(digitalPinToTimer(pin))
+ {
+ // XXX fix needed for atmega8
+ #if defined(TCCR0) && defined(COM00) && !defined(__AVR_ATmega8__)
+ case TIMER0A:
+ // connect pwm to pin on timer 0
+ sbi(TCCR0, COM00);
+ OCR0 = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR0A) && defined(COM0A1)
+ case TIMER0A:
+ // connect pwm to pin on timer 0, channel A
+ sbi(TCCR0A, COM0A1);
+ OCR0A = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR0A) && defined(COM0B1)
+ case TIMER0B:
+ // connect pwm to pin on timer 0, channel B
+ sbi(TCCR0A, COM0B1);
+ OCR0B = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR1A) && defined(COM1A1)
+ case TIMER1A:
+ // connect pwm to pin on timer 1, channel A
+ sbi(TCCR1A, COM1A1);
+ OCR1A = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR1A) && defined(COM1B1)
+ case TIMER1B:
+ // connect pwm to pin on timer 1, channel B
+ sbi(TCCR1A, COM1B1);
+ OCR1B = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR2) && defined(COM21)
+ case TIMER2:
+ // connect pwm to pin on timer 2
+ sbi(TCCR2, COM21);
+ OCR2 = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR2A) && defined(COM2A1)
+ case TIMER2A:
+ // connect pwm to pin on timer 2, channel A
+ sbi(TCCR2A, COM2A1);
+ OCR2A = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR2A) && defined(COM2B1)
+ case TIMER2B:
+ // connect pwm to pin on timer 2, channel B
+ sbi(TCCR2A, COM2B1);
+ OCR2B = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR3A) && defined(COM3A1)
+ case TIMER3A:
+ // connect pwm to pin on timer 3, channel A
+ sbi(TCCR3A, COM3A1);
+ OCR3A = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR3A) && defined(COM3B1)
+ case TIMER3B:
+ // connect pwm to pin on timer 3, channel B
+ sbi(TCCR3A, COM3B1);
+ OCR3B = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR3A) && defined(COM3C1)
+ case TIMER3C:
+ // connect pwm to pin on timer 3, channel C
+ sbi(TCCR3A, COM3C1);
+ OCR3C = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR4A)
+ case TIMER4A:
+ //connect pwm to pin on timer 4, channel A
+ sbi(TCCR4A, COM4A1);
+ #if defined(COM4A0) // only used on 32U4
+ cbi(TCCR4A, COM4A0);
+ #endif
+ OCR4A = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR4A) && defined(COM4B1)
+ case TIMER4B:
+ // connect pwm to pin on timer 4, channel B
+ sbi(TCCR4A, COM4B1);
+ OCR4B = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR4A) && defined(COM4C1)
+ case TIMER4C:
+ // connect pwm to pin on timer 4, channel C
+ sbi(TCCR4A, COM4C1);
+ OCR4C = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR4C) && defined(COM4D1)
+ case TIMER4D:
+ // connect pwm to pin on timer 4, channel D
+ sbi(TCCR4C, COM4D1);
+ #if defined(COM4D0) // only used on 32U4
+ cbi(TCCR4C, COM4D0);
+ #endif
+ OCR4D = val; // set pwm duty
+ break;
+ #endif
+
+
+ #if defined(TCCR5A) && defined(COM5A1)
+ case TIMER5A:
+ // connect pwm to pin on timer 5, channel A
+ sbi(TCCR5A, COM5A1);
+ OCR5A = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR5A) && defined(COM5B1)
+ case TIMER5B:
+ // connect pwm to pin on timer 5, channel B
+ sbi(TCCR5A, COM5B1);
+ OCR5B = val; // set pwm duty
+ break;
+ #endif
+
+ #if defined(TCCR5A) && defined(COM5C1)
+ case TIMER5C:
+ // connect pwm to pin on timer 5, channel C
+ sbi(TCCR5A, COM5C1);
+ OCR5C = val; // set pwm duty
+ break;
+ #endif
+
+ case NOT_ON_TIMER:
+ default:
+ if (val < 128) {
+ digitalWrite(pin, LOW);
+ } else {
+ digitalWrite(pin, HIGH);
+ }
+ }
+ }
+}
+
diff --git a/cores/robot/wiring_digital.c b/cores/robot/wiring_digital.c
new file mode 100644
index 0000000..be323b1
--- /dev/null
+++ b/cores/robot/wiring_digital.c
@@ -0,0 +1,178 @@
+/*
+ wiring_digital.c - digital input and output functions
+ Part of Arduino - http://www.arduino.cc/
+
+ Copyright (c) 2005-2006 David A. Mellis
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+
+ Modified 28 September 2010 by Mark Sproul
+
+ $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
+*/
+
+#define ARDUINO_MAIN
+#include "wiring_private.h"
+#include "pins_arduino.h"
+
+void pinMode(uint8_t pin, uint8_t mode)
+{
+ uint8_t bit = digitalPinToBitMask(pin);
+ uint8_t port = digitalPinToPort(pin);
+ volatile uint8_t *reg, *out;
+
+ if (port == NOT_A_PIN) return;
+
+ // JWS: can I let the optimizer do this?
+ reg = portModeRegister(port);
+ out = portOutputRegister(port);
+
+ if (mode == INPUT) {
+ uint8_t oldSREG = SREG;
+ cli();
+ *reg &= ~bit;
+ *out &= ~bit;
+ SREG = oldSREG;
+ } else if (mode == INPUT_PULLUP) {
+ uint8_t oldSREG = SREG;
+ cli();
+ *reg &= ~bit;
+ *out |= bit;
+ SREG = oldSREG;
+ } else {
+ uint8_t oldSREG = SREG;
+ cli();
+ *reg |= bit;
+ SREG = oldSREG;
+ }
+}
+
+// Forcing this inline keeps the callers from having to push their own stuff
+// on the stack. It is a good performance win and only takes 1 more byte per
+// user than calling. (It will take more bytes on the 168.)
+//
+// But shouldn't this be moved into pinMode? Seems silly to check and do on
+// each digitalread or write.
+//
+// Mark Sproul:
+// - Removed inline. Save 170 bytes on atmega1280
+// - changed to a switch statment; added 32 bytes but much easier to read and maintain.
+// - Added more #ifdefs, now compiles for atmega645
+//
+//static inline void turnOffPWM(uint8_t timer) __attribute__ ((always_inline));
+//static inline void turnOffPWM(uint8_t timer)
+static void turnOffPWM(uint8_t timer)
+{
+ switch (timer)
+ {
+ #if defined(TCCR1A) && defined(COM1A1)
+ case TIMER1A: cbi(TCCR1A, COM1A1); break;
+ #endif
+ #if defined(TCCR1A) && defined(COM1B1)
+ case TIMER1B: cbi(TCCR1A, COM1B1); break;
+ #endif
+
+ #if defined(TCCR2) && defined(COM21)
+ case TIMER2: cbi(TCCR2, COM21); break;
+ #endif
+
+ #if defined(TCCR0A) && defined(COM0A1)
+ case TIMER0A: cbi(TCCR0A, COM0A1); break;
+ #endif
+
+ #if defined(TIMER0B) && defined(COM0B1)
+ case TIMER0B: cbi(TCCR0A, COM0B1); break;
+ #endif
+ #if defined(TCCR2A) && defined(COM2A1)
+ case TIMER2A: cbi(TCCR2A, COM2A1); break;
+ #endif
+ #if defined(TCCR2A) && defined(COM2B1)
+ case TIMER2B: cbi(TCCR2A, COM2B1); break;
+ #endif
+
+ #if defined(TCCR3A) && defined(COM3A1)
+ case TIMER3A: cbi(TCCR3A, COM3A1); break;
+ #endif
+ #if defined(TCCR3A) && defined(COM3B1)
+ case TIMER3B: cbi(TCCR3A, COM3B1); break;
+ #endif
+ #if defined(TCCR3A) && defined(COM3C1)
+ case TIMER3C: cbi(TCCR3A, COM3C1); break;
+ #endif
+
+ #if defined(TCCR4A) && defined(COM4A1)
+ case TIMER4A: cbi(TCCR4A, COM4A1); break;
+ #endif
+ #if defined(TCCR4A) && defined(COM4B1)
+ case TIMER4B: cbi(TCCR4A, COM4B1); break;
+ #endif
+ #if defined(TCCR4A) && defined(COM4C1)
+ case TIMER4C: cbi(TCCR4A, COM4C1); break;
+ #endif
+ #if defined(TCCR4C) && defined(COM4D1)
+ case TIMER4D: cbi(TCCR4C, COM4D1); break;
+ #endif
+
+ #if defined(TCCR5A)
+ case TIMER5A: cbi(TCCR5A, COM5A1); break;
+ case TIMER5B: cbi(TCCR5A, COM5B1); break;
+ case TIMER5C: cbi(TCCR5A, COM5C1); break;
+ #endif
+ }
+}
+
+void digitalWrite(uint8_t pin, uint8_t val)
+{
+ uint8_t timer = digitalPinToTimer(pin);
+ uint8_t bit = digitalPinToBitMask(pin);
+ uint8_t port = digitalPinToPort(pin);
+ volatile uint8_t *out;
+
+ if (port == NOT_A_PIN) return;
+
+ // If the pin that support PWM output, we need to turn it off
+ // before doing a digital write.
+ if (timer != NOT_ON_TIMER) turnOffPWM(timer);
+
+ out = portOutputRegister(port);
+
+ uint8_t oldSREG = SREG;
+ cli();
+
+ if (val == LOW) {
+ *out &= ~bit;
+ } else {
+ *out |= bit;
+ }
+
+ SREG = oldSREG;
+}
+
+int digitalRead(uint8_t pin)
+{
+ uint8_t timer = digitalPinToTimer(pin);
+ uint8_t bit = digitalPinToBitMask(pin);
+ uint8_t port = digitalPinToPort(pin);
+
+ if (port == NOT_A_PIN) return LOW;
+
+ // If the pin that support PWM output, we need to turn it off
+ // before getting a digital reading.
+ if (timer != NOT_ON_TIMER) turnOffPWM(timer);
+
+ if (*portInputRegister(port) & bit) return HIGH;
+ return LOW;
+}
diff --git a/cores/robot/wiring_private.h b/cores/robot/wiring_private.h
new file mode 100755
index 0000000..c366005
--- /dev/null
+++ b/cores/robot/wiring_private.h
@@ -0,0 +1,71 @@
+/*
+ wiring_private.h - Internal header file.
+ Part of Arduino - http://www.arduino.cc/
+
+ Copyright (c) 2005-2006 David A. Mellis
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+
+ $Id: wiring.h 239 2007-01-12 17:58:39Z mellis $
+*/
+
+#ifndef WiringPrivate_h
+#define WiringPrivate_h
+
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <stdio.h>
+#include <stdarg.h>
+
+#include "Arduino.h"
+
+#ifdef __cplusplus
+extern "C"{
+#endif
+
+#ifndef cbi
+#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
+#endif
+#ifndef sbi
+#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
+#endif
+
+#define EXTERNAL_INT_0 0
+#define EXTERNAL_INT_1 1
+#define EXTERNAL_INT_2 2
+#define EXTERNAL_INT_3 3
+#define EXTERNAL_INT_4 4
+#define EXTERNAL_INT_5 5
+#define EXTERNAL_INT_6 6
+#define EXTERNAL_INT_7 7
+
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
+#define EXTERNAL_NUM_INTERRUPTS 8
+#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
+#define EXTERNAL_NUM_INTERRUPTS 3
+#elif defined(__AVR_ATmega32U4__)
+#define EXTERNAL_NUM_INTERRUPTS 5
+#else
+#define EXTERNAL_NUM_INTERRUPTS 2
+#endif
+
+typedef void (*voidFuncPtr)(void);
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+#endif
diff --git a/cores/robot/wiring_pulse.c b/cores/robot/wiring_pulse.c
new file mode 100755
index 0000000..0d96886
--- /dev/null
+++ b/cores/robot/wiring_pulse.c
@@ -0,0 +1,69 @@
+/*
+ wiring_pulse.c - pulseIn() function
+ Part of Arduino - http://www.arduino.cc/
+
+ Copyright (c) 2005-2006 David A. Mellis
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+
+ $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
+*/
+
+#include "wiring_private.h"
+#include "pins_arduino.h"
+
+/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
+ * or LOW, the type of pulse to measure. Works on pulses from 2-3 microseconds
+ * to 3 minutes in length, but must be called at least a few dozen microseconds
+ * before the start of the pulse. */
+unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
+{
+ // cache the port and bit of the pin in order to speed up the
+ // pulse width measuring loop and achieve finer resolution. calling
+ // digitalRead() instead yields much coarser resolution.
+ uint8_t bit = digitalPinToBitMask(pin);
+ uint8_t port = digitalPinToPort(pin);
+ uint8_t stateMask = (state ? bit : 0);
+ unsigned long width = 0; // keep initialization out of time critical area
+
+ // convert the timeout from microseconds to a number of times through
+ // the initial loop; it takes 16 clock cycles per iteration.
+ unsigned long numloops = 0;
+ unsigned long maxloops = microsecondsToClockCycles(timeout) / 16;
+
+ // wait for any previous pulse to end
+ while ((*portInputRegister(port) & bit) == stateMask)
+ if (numloops++ == maxloops)
+ return 0;
+
+ // wait for the pulse to start
+ while ((*portInputRegister(port) & bit) != stateMask)
+ if (numloops++ == maxloops)
+ return 0;
+
+ // wait for the pulse to stop
+ while ((*portInputRegister(port) & bit) == stateMask) {
+ if (numloops++ == maxloops)
+ return 0;
+ width++;
+ }
+
+ // convert the reading to microseconds. The loop has been determined
+ // to be 20 clock cycles long and have about 16 clocks between the edge
+ // and the start of the loop. There will be some error introduced by
+ // the interrupt handlers.
+ return clockCyclesToMicroseconds(width * 21 + 16);
+}
diff --git a/cores/robot/wiring_shift.c b/cores/robot/wiring_shift.c
new file mode 100755
index 0000000..cfe7867
--- /dev/null
+++ b/cores/robot/wiring_shift.c
@@ -0,0 +1,55 @@
+/*
+ wiring_shift.c - shiftOut() function
+ Part of Arduino - http://www.arduino.cc/
+
+ Copyright (c) 2005-2006 David A. Mellis
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+
+ $Id: wiring.c 248 2007-02-03 15:36:30Z mellis $
+*/
+
+#include "wiring_private.h"
+
+uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder) {
+ uint8_t value = 0;
+ uint8_t i;
+
+ for (i = 0; i < 8; ++i) {
+ digitalWrite(clockPin, HIGH);
+ if (bitOrder == LSBFIRST)
+ value |= digitalRead(dataPin) << i;
+ else
+ value |= digitalRead(dataPin) << (7 - i);
+ digitalWrite(clockPin, LOW);
+ }
+ return value;
+}
+
+void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
+{
+ uint8_t i;
+
+ for (i = 0; i < 8; i++) {
+ if (bitOrder == LSBFIRST)
+ digitalWrite(dataPin, !!(val & (1 << i)));
+ else
+ digitalWrite(dataPin, !!(val & (1 << (7 - i))));
+
+ digitalWrite(clockPin, HIGH);
+ digitalWrite(clockPin, LOW);
+ }
+}
diff --git a/variants/robot_control/pins_arduino.h b/variants/robot_control/pins_arduino.h
new file mode 100644
index 0000000..d7ecae5
--- /dev/null
+++ b/variants/robot_control/pins_arduino.h
@@ -0,0 +1,274 @@
+/*
+ pins_arduino.h - Pin definition functions for Arduino Robot Control Board
+ Part of Arduino - http://www.arduino.cc/
+
+ Copyright (c) 2913 D. Cuartielles, X. Yang (Arduino Verkstad)
+ Copyright (c) 2012 D. Cuartielles, N. de la Riva, I. Gallego, E. Gallego
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+
+ $Id: pins_arduino.h 1 2013-03-16 20:47:51Z cuartielles $
+*/
+
+#ifndef Pins_Arduino_h
+#define Pins_Arduino_h
+
+#include <avr/pgmspace.h>
+
+#define ARDUINO_MODEL_USB_PID 0x0038
+
+#define TX_RX_LED_INIT DDRD |= (1<<5), DDRB |= (1<<0)
+#define TXLED0 PORTD |= (1<<5)
+#define TXLED1 PORTD &= ~(1<<5)
+#define RXLED0 PORTB |= (1<<0)
+#define RXLED1 PORTB &= ~(1<<0)
+
+static const uint8_t RX = 0;
+static const uint8_t TX = 1;
+static const uint8_t SDA = 2;
+static const uint8_t SCL = 3;
+
+// Map SPI port to 'new' pins D14..D17
+static const uint8_t SS = 17;
+static const uint8_t MOSI = 16;
+static const uint8_t MISO = 14;
+static const uint8_t SCK = 15;
+
+// Mapping of analog pins as digital I/O
+// A6-A11 share with digital pins
+static const uint8_t A0 = 18;
+static const uint8_t A1 = 19;
+static const uint8_t A2 = 20;
+static const uint8_t A3 = 21;
+static const uint8_t A4 = 22;
+static const uint8_t A5 = 23;
+static const uint8_t A6 = 24; // D4
+static const uint8_t A7 = 25; // D6
+static const uint8_t A8 = 26; // D8
+static const uint8_t A9 = 27; // D9
+static const uint8_t A10 = 28; // D10
+static const uint8_t A11 = 29; // D12
+
+// Specific Mapping for the Control Board
+static const uint8_t KEY = 18; // AD0
+static const uint8_t MUX_IN = 24; // ADC8 - A6
+static const uint8_t MUXA = 6; // D5 - TKD4
+static const uint8_t MUXB = 11; // D11
+static const uint8_t MUXC = 12; // D12 - TKD5
+static const uint8_t MUXD = 13; // D13
+static const uint8_t BUZZ = 5; // D5
+static const uint8_t POT = 23; // AD5
+static const uint8_t DC_LCD = 10; // D10
+static const uint8_t LCD_CS = 9; // D9
+static const uint8_t RST_LCD = 7; // D6
+static const uint8_t CARD_CS = 8; // D8
+static const uint8_t TKD0 = 19; // ADC6 - A1
+static const uint8_t TKD1 = 20; // ADC5 - A2
+static const uint8_t TKD2 = 21; // ADC4 - A3
+static const uint8_t TKD3 = 22; // ADC1 - A4
+static const uint8_t TKD4 = 6; // D5 - MUXA
+static const uint8_t TKD5 = 12; // D12 - MUXC
+static const uint8_t LED1 = 17; // D17 - RX_Led
+
+// __AVR_ATmega32U4__ has an unusual mapping of pins to channels
+extern const uint8_t PROGMEM analog_pin_to_channel_PGM[];
+#define analogPinToChannel(P) ( pgm_read_byte( analog_pin_to_channel_PGM + (P) ) )
+
+#ifdef ARDUINO_MAIN
+
+// On the Arduino board, digital pins are also used
+// for the analog output (software PWM). Analog input
+// pins are a separate set.
+
+// ARDUINO LEONARDO / ARDUINO ROBOT CONTROL / ATMEGA 32U4 / FUNCTION / REGISTER
+//
+// D0 RX PD2 RX RXD1/INT2
+// D1 TX PD3 TX TXD1/INT3
+// D2 SDA PD1 SDA SDA/INT1
+// D3# SCL PD0 PWM8/SCL OC0B/SCL/INT0
+// D4 MUX_IN A6 PD4 ADC8
+// D5# BUZZ PC6 ??? OC3A/#OC4A
+// D6# MUXA/TKD4 A7 PD7 FastPWM #OC4D/ADC10
+// D7 RST_LCD PE6 INT6/AIN0
+//
+// D8 CARD_CS A8 PB4 ADC11/PCINT4
+// D9# LCD_CS A9 PB5 PWM16 OC1A/#OC4B/ADC12/PCINT5
+// D10# DC_LCD A10 PB6 PWM16 OC1B/0c4B/ADC13/PCINT6
+// D11# MUXB PB7 PWM8/16 0C0A/OC1C/#RTS/PCINT7
+// D12 MUXC/TKD5 A11 PD6 T1/#OC4D/ADC9
+// D13# MUXD PC7 PWM10 CLK0/OC4A
+//
+// A0 KEY D18 PF7 ADC7
+// A1 TKD0 D19 PF6 ADC6
+// A2 TKD1 D20 PF5 ADC5
+// A3 TKD2 D21 PF4 ADC4
+// A4 TKD3 D22 PF1 ADC1
+// A5 POT D23 PF0 ADC0
+//
+// MISO MISO D14 PB3 MISO,PCINT3
+// SCK SCK D15 PB1 SCK,PCINT1
+// MOSI MOSI D16 PB2 MOSI,PCINT2
+// SS RX_LED D17 PB0 RXLED,SS/PCINT0
+//
+// TXLED TX_LED PD5
+// HWB PE2 HWB
+
+// these arrays map port names (e.g. port B) to the
+// appropriate addresses for various functions (e.g. reading
+// and writing)
+const uint16_t PROGMEM port_to_mode_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &DDRB,
+ (uint16_t) &DDRC,
+ (uint16_t) &DDRD,
+ (uint16_t) &DDRE,
+ (uint16_t) &DDRF,
+};
+
+const uint16_t PROGMEM port_to_output_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &PORTB,
+ (uint16_t) &PORTC,
+ (uint16_t) &PORTD,
+ (uint16_t) &PORTE,
+ (uint16_t) &PORTF,
+};
+
+const uint16_t PROGMEM port_to_input_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &PINB,
+ (uint16_t) &PINC,
+ (uint16_t) &PIND,
+ (uint16_t) &PINE,
+ (uint16_t) &PINF,
+};
+
+const uint8_t PROGMEM digital_pin_to_port_PGM[30] = {
+ PD, // D0 - PD2
+ PD, // D1 - PD3
+ PD, // D2 - PD1
+ PD, // D3 - PD0
+ PD, // D4 - PD4
+ PC, // D5 - PC6
+ PD, // D6 - PD7
+ PE, // D7 - PE6
+
+ PB, // D8 - PB4
+ PB, // D9 - PB5
+ PB, // D10 - PB6
+ PB, // D11 - PB7
+ PD, // D12 - PD6
+ PC, // D13 - PC7
+
+ PB, // D14 - MISO - PB3
+ PB, // D15 - SCK - PB1
+ PB, // D16 - MOSI - PB2
+ PB, // D17 - SS - PB0
+
+ PF, // D18 - A0 - PF7
+ PF, // D19 - A1 - PF6
+ PF, // D20 - A2 - PF5
+ PF, // D21 - A3 - PF4
+ PF, // D22 - A4 - PF1
+ PF, // D23 - A5 - PF0
+
+ PD, // D24 / D4 - A6 - PD4
+ PD, // D25 / D6 - A7 - PD7
+ PB, // D26 / D8 - A8 - PB4
+ PB, // D27 / D9 - A9 - PB5
+ PB, // D28 / D10 - A10 - PB6
+ PD, // D29 / D12 - A11 - PD6
+};
+
+const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[30] = {
+ _BV(2), // D0 - PD2
+ _BV(3), // D1 - PD3
+ _BV(1), // D2 - PD1
+ _BV(0), // D3 - PD0
+ _BV(4), // D4 - PD4
+ _BV(6), // D5 - PC6
+ _BV(7), // D6 - PD7
+ _BV(6), // D7 - PE6
+
+ _BV(4), // D8 - PB4
+ _BV(5), // D9 - PB5
+ _BV(6), // D10 - PB6
+ _BV(7), // D11 - PB7
+ _BV(6), // D12 - PD6
+ _BV(7), // D13 - PC7
+
+ _BV(3), // D14 - MISO - PB3
+ _BV(1), // D15 - SCK - PB1
+ _BV(2), // D16 - MOSI - PB2
+ _BV(0), // D17 - SS - PB0
+
+ _BV(7), // D18 - A0 - PF7
+ _BV(6), // D19 - A1 - PF6
+ _BV(5), // D20 - A2 - PF5
+ _BV(4), // D21 - A3 - PF4
+ _BV(1), // D22 - A4 - PF1
+ _BV(0), // D23 - A5 - PF0
+
+ _BV(4), // D24 / D4 - A6 - PD4
+ _BV(7), // D25 / D6 - A7 - PD7
+ _BV(4), // D26 / D8 - A8 - PB4
+ _BV(5), // D27 / D9 - A9 - PB5
+ _BV(6), // D28 / D10 - A10 - PB6
+ _BV(6), // D29 / D12 - A11 - PD6
+};
+
+const uint8_t PROGMEM digital_pin_to_timer_PGM[18] = {
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+ TIMER0B, /* 3 */
+ NOT_ON_TIMER,
+ TIMER3A, /* 5 */
+ TIMER4D, /* 6 */
+ NOT_ON_TIMER,
+
+ NOT_ON_TIMER,
+ TIMER1A, /* 9 */
+ TIMER1B, /* 10 */
+ TIMER0A, /* 11 */
+
+ NOT_ON_TIMER,
+ TIMER4A, /* 13 */
+
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+};
+
+const uint8_t PROGMEM analog_pin_to_channel_PGM[12] = {
+ 7, // A0 PF7 ADC7
+ 6, // A1 PF6 ADC6
+ 5, // A2 PF5 ADC5
+ 4, // A3 PF4 ADC4
+ 1, // A4 PF1 ADC1
+ 0, // A5 PF0 ADC0
+ 8, // A6 D4 PD4 ADC8
+ 10, // A7 D6 PD7 ADC10
+ 11, // A8 D8 PB4 ADC11
+ 12, // A9 D9 PB5 ADC12
+ 13, // A10 D10 PB6 ADC13
+ 9 // A11 D12 PD6 ADC9
+};
+
+#endif /* ARDUINO_MAIN */
+#endif /* Pins_Arduino_h */
diff --git a/variants/robot_motor/pins_arduino.h b/variants/robot_motor/pins_arduino.h
new file mode 100644
index 0000000..902d424
--- /dev/null
+++ b/variants/robot_motor/pins_arduino.h
@@ -0,0 +1,271 @@
+/*
+ pins_arduino.h - Pin definition functions for Arduino Robot Control Board
+ Part of Arduino - http://www.arduino.cc/
+
+ Copyright (c) 2913 D. Cuartielles, X. Yang (Arduino Verkstad)
+ Copyright (c) 2012 D. Cuartielles, N. de la Riva, I. Gallego, E. Gallego
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General
+ Public License along with this library; if not, write to the
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ Boston, MA 02111-1307 USA
+
+ $Id: pins_arduino.h 1 2013-03-16 20:47:51Z cuartielles $
+*/
+
+#ifndef Pins_Arduino_h
+#define Pins_Arduino_h
+
+#include <avr/pgmspace.h>
+
+#define ARDUINO_MODEL_USB_PID 0x0039
+
+#define TX_RX_LED_INIT DDRD |= (1<<5), DDRB |= (1<<0)
+#define TXLED0 PORTD |= (1<<5)
+#define TXLED1 PORTD &= ~(1<<5)
+#define RXLED0 PORTB |= (1<<0)
+#define RXLED1 PORTB &= ~(1<<0)
+
+static const uint8_t RX = 0;
+static const uint8_t TX = 1;
+static const uint8_t SDA = 2;
+static const uint8_t SCL = 3;
+
+// Map SPI port to 'new' pins D14..D17
+static const uint8_t SS = 17;
+static const uint8_t MOSI = 16;
+static const uint8_t MISO = 14;
+static const uint8_t SCK = 15;
+
+// Mapping of analog pins as digital I/O
+// A6-A11 share with digital pins
+static const uint8_t A0 = 18;
+static const uint8_t A1 = 19;
+static const uint8_t A2 = 20;
+static const uint8_t A3 = 21;
+static const uint8_t A4 = 22;
+static const uint8_t A5 = 23;
+static const uint8_t A6 = 24; // D4
+static const uint8_t A7 = 25; // D6
+static const uint8_t A8 = 26; // D8
+static const uint8_t A9 = 27; // D9
+static const uint8_t A10 = 28; // D10
+static const uint8_t A11 = 29; // D12
+
+// Specific Mapping for the Motor Board
+static const uint8_t MUX_IN = 20; // A2
+static const uint8_t MUXA = 7; // D7
+static const uint8_t MUXB = 8; // D8
+static const uint8_t MUXC = 11; // D11
+static const uint8_t MUXI = 13; // D13
+static const uint8_t TRIM = 21; // A3
+static const uint8_t SENSE_A = 22; // A4
+static const uint8_t SENSE_B = 23; // A5
+static const uint8_t IN_A1 = 6; // D6 - A7
+static const uint8_t IN_A2 = 5; // D5
+static const uint8_t IN_B1 = 10; // D10
+static const uint8_t IN_B2 = 9; // D9
+static const uint8_t TK1 = 18; // A0
+static const uint8_t TK2 = 19; // A1
+static const uint8_t TK3 = 4; // A6
+static const uint8_t TK4 = 12; // A11
+
+// __AVR_ATmega32U4__ has an unusual mapping of pins to channels
+extern const uint8_t PROGMEM analog_pin_to_channel_PGM[];
+#define analogPinToChannel(P) ( pgm_read_byte( analog_pin_to_channel_PGM + (P) ) )
+
+#ifdef ARDUINO_MAIN
+
+// On the Arduino board, digital pins are also used
+// for the analog output (software PWM). Analog input
+// pins are a separate set.
+
+// ARDUINO LEONARDO / ARDUINO ROBOT CONTROL / ATMEGA 32U4 / FUNCTION / REGISTER
+//
+// D0 RX PD2 RX RXD1/INT2
+// D1 TX PD3 TX TXD1/INT3
+// D2 SDA PD1 SDA SDA/INT1
+// D3# SCL PD0 PWM8/SCL OC0B/SCL/INT0
+// D4 TK3 A6 PD4 ADC8
+// D5# INA2 PC6 ??? OC3A/#OC4A
+// D6# INA1 A7 PD7 FastPWM #OC4D/ADC10
+// D7 MUXA PE6 INT6/AIN0
+//
+// D8 MUXB A8 PB4 ADC11/PCINT4
+// D9# INB2 A9 PB5 PWM16 OC1A/#OC4B/ADC12/PCINT5
+// D10# INB1 A10 PB6 PWM16 OC1B/0c4B/ADC13/PCINT6
+// D11# MUXC PB7 PWM8/16 0C0A/OC1C/#RTS/PCINT7
+// D12 TK4 A11 PD6 T1/#OC4D/ADC9
+// D13# MUXI PC7 PWM10 CLK0/OC4A
+//
+// A0 TK1 D18 PF7 ADC7
+// A1 TK2 D19 PF6 ADC6
+// A2 MUX_IN D20 PF5 ADC5
+// A3 TRIM D21 PF4 ADC4
+// A4 SENSE_A D22 PF1 ADC1
+// A5 SENSE_B D23 PF0 ADC0
+//
+// MISO MISO D14 PB3 MISO,PCINT3
+// SCK SCK D15 PB1 SCK,PCINT1
+// MOSI MOSI D16 PB2 MOSI,PCINT2
+// SS RX_LED D17 PB0 RXLED,SS/PCINT0
+//
+// TXLED TX_LED PD5
+// HWB PE2 HWB
+
+// these arrays map port names (e.g. port B) to the
+// appropriate addresses for various functions (e.g. reading
+// and writing)
+const uint16_t PROGMEM port_to_mode_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &DDRB,
+ (uint16_t) &DDRC,
+ (uint16_t) &DDRD,
+ (uint16_t) &DDRE,
+ (uint16_t) &DDRF,
+};
+
+const uint16_t PROGMEM port_to_output_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &PORTB,
+ (uint16_t) &PORTC,
+ (uint16_t) &PORTD,
+ (uint16_t) &PORTE,
+ (uint16_t) &PORTF,
+};
+
+const uint16_t PROGMEM port_to_input_PGM[] = {
+ NOT_A_PORT,
+ NOT_A_PORT,
+ (uint16_t) &PINB,
+ (uint16_t) &PINC,
+ (uint16_t) &PIND,
+ (uint16_t) &PINE,
+ (uint16_t) &PINF,
+};
+
+const uint8_t PROGMEM digital_pin_to_port_PGM[30] = {
+ PD, // D0 - PD2
+ PD, // D1 - PD3
+ PD, // D2 - PD1
+ PD, // D3 - PD0
+ PD, // D4 - PD4
+ PC, // D5 - PC6
+ PD, // D6 - PD7
+ PE, // D7 - PE6
+
+ PB, // D8 - PB4
+ PB, // D9 - PB5
+ PB, // D10 - PB6
+ PB, // D11 - PB7
+ PD, // D12 - PD6
+ PC, // D13 - PC7
+
+ PB, // D14 - MISO - PB3
+ PB, // D15 - SCK - PB1
+ PB, // D16 - MOSI - PB2
+ PB, // D17 - SS - PB0
+
+ PF, // D18 - A0 - PF7
+ PF, // D19 - A1 - PF6
+ PF, // D20 - A2 - PF5
+ PF, // D21 - A3 - PF4
+ PF, // D22 - A4 - PF1
+ PF, // D23 - A5 - PF0
+
+ PD, // D24 / D4 - A6 - PD4
+ PD, // D25 / D6 - A7 - PD7
+ PB, // D26 / D8 - A8 - PB4
+ PB, // D27 / D9 - A9 - PB5
+ PB, // D28 / D10 - A10 - PB6
+ PD, // D29 / D12 - A11 - PD6
+};
+
+const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[30] = {
+ _BV(2), // D0 - PD2
+ _BV(3), // D1 - PD3
+ _BV(1), // D2 - PD1
+ _BV(0), // D3 - PD0
+ _BV(4), // D4 - PD4
+ _BV(6), // D5 - PC6
+ _BV(7), // D6 - PD7
+ _BV(6), // D7 - PE6
+
+ _BV(4), // D8 - PB4
+ _BV(5), // D9 - PB5
+ _BV(6), // D10 - PB6
+ _BV(7), // D11 - PB7
+ _BV(6), // D12 - PD6
+ _BV(7), // D13 - PC7
+
+ _BV(3), // D14 - MISO - PB3
+ _BV(1), // D15 - SCK - PB1
+ _BV(2), // D16 - MOSI - PB2
+ _BV(0), // D17 - SS - PB0
+
+ _BV(7), // D18 - A0 - PF7
+ _BV(6), // D19 - A1 - PF6
+ _BV(5), // D20 - A2 - PF5
+ _BV(4), // D21 - A3 - PF4
+ _BV(1), // D22 - A4 - PF1
+ _BV(0), // D23 - A5 - PF0
+
+ _BV(4), // D24 / D4 - A6 - PD4
+ _BV(7), // D25 / D6 - A7 - PD7
+ _BV(4), // D26 / D8 - A8 - PB4
+ _BV(5), // D27 / D9 - A9 - PB5
+ _BV(6), // D28 / D10 - A10 - PB6
+ _BV(6), // D29 / D12 - A11 - PD6
+};
+
+const uint8_t PROGMEM digital_pin_to_timer_PGM[18] = {
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+ TIMER0B, /* 3 */
+ NOT_ON_TIMER,
+ TIMER3A, /* 5 */
+ TIMER4D, /* 6 */
+ NOT_ON_TIMER,
+
+ NOT_ON_TIMER,
+ TIMER1A, /* 9 */
+ TIMER1B, /* 10 */
+ TIMER0A, /* 11 */
+
+ NOT_ON_TIMER,
+ TIMER4A, /* 13 */
+
+ NOT_ON_TIMER,
+ NOT_ON_TIMER,
+};
+
+const uint8_t PROGMEM analog_pin_to_channel_PGM[12] = {
+ 7, // A0 PF7 ADC7
+ 6, // A1 PF6 ADC6
+ 5, // A2 PF5 ADC5
+ 4, // A3 PF4 ADC4
+ 1, // A4 PF1 ADC1
+ 0, // A5 PF0 ADC0
+ 8, // A6 D4 PD4 ADC8
+ 10, // A7 D6 PD7 ADC10
+ 11, // A8 D8 PB4 ADC11
+ 12, // A9 D9 PB5 ADC12
+ 13, // A10 D10 PB6 ADC13
+ 9 // A11 D12 PD6 ADC9
+};
+
+#endif /* ARDUINO_MAIN */
+#endif /* Pins_Arduino_h */