{"id":16140867,"url":"https://github.com/ellerbach/legoinfrared","last_synced_at":"2025-03-18T17:30:34.137Z","repository":{"id":214176725,"uuid":"735645759","full_name":"Ellerbach/LegoInfrared","owner":"Ellerbach","description":"Implementation of the Lego Infrared protocol using SPI. Implementation for both .NET IoT and .NET nanoFramework","archived":false,"fork":false,"pushed_at":"2025-03-13T00:22:13.000Z","size":4183,"stargazers_count":2,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-14T10:21:27.750Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ellerbach.png","metadata":{"files":{"readme":"README.md","changelog":"changelog-config.json","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-25T17:03:13.000Z","updated_at":"2025-03-07T08:54:46.000Z","dependencies_parsed_at":"2024-04-18T01:29:11.144Z","dependency_job_id":"6a4da4ad-1efc-451e-9491-2969012926d3","html_url":"https://github.com/Ellerbach/LegoInfrared","commit_stats":null,"previous_names":["ellerbach/legoinfrared"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ellerbach%2FLegoInfrared","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ellerbach%2FLegoInfrared/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ellerbach%2FLegoInfrared/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ellerbach%2FLegoInfrared/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ellerbach","download_url":"https://codeload.github.com/Ellerbach/LegoInfrared/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244269342,"owners_count":20426202,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-09T23:53:58.768Z","updated_at":"2025-03-18T17:30:33.366Z","avatar_url":"https://github.com/Ellerbach.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# LegoInfrared\n\nImplementation of the Lego Infrared protocol using SPI. Implementation for both .NET IoT and .NET nanoFramework.\n\nThis project allow to control any Lego Power Function and support all Lego modes including exclusive modes which can't be done thru any of the Lego remote control.\n\n## Documentation\n\nFor reference only, all copyrights to [Lego(c)](https://www.lego.com/), the infrared protocol specifications are included in this repository here for the [initial v1.0 version](./Assets/LEGO_Power_Functions_RC.pdf) and a [further v1.10 version](./Assets/LEGO_Power_Functions_RC_v110.pdf).\n\nSo far, this implementation is compatible with the v1.0. It is intended to add v1.10 support in a short future.\n\n## Required electronic\n\nThe infrared module require a bit of electronic. You'll need a to drive enough power in the infrared led to be able to control trains or other Lego Power Functions in large rooms or with a lot of lights.\n\nFor this, you'll need to build the equivalent electronic:\n\n![Electronic for Infrared](./Assets/infrared.jpg)\n\nNote that the chipselect with Enable is not mandatory. The schema can also work with 3.3V, the distance covered will be less than with 5V.\n\n* The MOSI pin is 19 and Enabled is pin number 24 on a Raspberry PI. Those are the 2 pins you need for the infrared part.\n* On an ESP32, this is configurable, install the nanoFramework.Hardware.Esp32 nuget and use the `Configuration.SetPinFunction` function.\n\n## Usage\n\nYou need first to create an instance of the `LegoInfrared` class:\n\n```csharp\nLegoInfrared _infrared;\n// On an ESP32, setup first the pins for the SPI\nConfiguration.SetPinFunction(14, DeviceFunction.SPI1_CLOCK);\nConfiguration.SetPinFunction(12, DeviceFunction.SPI1_MISO);\nConfiguration.SetPinFunction(13, DeviceFunction.SPI1_MOSI);\n\n// We will use chip select 17\n_infrared = new LegoInfrared(1, 17);\n\n_infrared.ComboMode(Speed.BlueForward, Speed.RedForward, Channel.Channel1);\n\n_infrared.SingleOutputContinuousAll(\n    new Function[] { Function.Set, Function.Clear, Function.Toggle, Function.NoChange },\n    new Output[] { Output.BluePinC2, Output.RedPinC2, Output.RedPinC1, Output.BluePinC1 });\n```\n\nAnd as shown in the example, you can then call any of the public function. You still need to understand what each function is doing.\n\nNote that if you don't want to use an enable pin, you can set the chip select pin to -1. This will not use any chip select.\n\n## WebServer API usage for .NET nanoFramework\n\nYou have 2 available applications, one which will require an ESP32 with PSRAM, the [nanoFramework.WebServerAndSerial](./Samples/nanoFramework/nanoFramework.WebServerAndSerial/) and one [nanoFramework.WebServerAndSerial.NoBluetooth](./Samples/nanoFramework/nanoFramework.WebServerAndSerial.NoBluetooth/) which can run without PSRAM but won't support Bluetooth wifi setup rather it will use Access Point wireless support.\n\n### Bluetooth application setup\n\nYou have to use [Improv Wifi](https://www.improv-wifi.com) to setup the application. Just connect the application and enter your SSID and Password, the device will automatically connect. The new IP address is broadcasted in the Improv Wifi settings.\n\n### Non Bluetooth, only Wifi setup\n\nAn SSID `LegoInfrared` will appear, connect to it and go to [\u003chttp://192.168.4.1\u003e](http://192.168.4.1), enter your wifi credentials and save. The new IP address should be displayed. Connect back the device you've been using to setup the board to your network and you'll be good to go!\n\n### Infrared configuration\n\nGo to the configuration page (`/config`) and enter valid GPIO pin numbers. Note that for ChipSelect, this value can stay as -1 but all the others must be valid. Save the configuration and you'll be good to go!\n\n### Security\n\nBy default basic authentication is used for security. Even if the default login and password are empty, you must specify basic authentication in all the calls you are doing. If you do not, you will get an unauthorized error.\n\n## .NET IoT WebServer API usage\n\nYou have one full example implementing quite a lot of features on the [WebServerAndSerial](./Samples/dotnet/WebServerAndSerial/WebServerAndSerial/). You have the ability to also control signals (red/green) and switches through servo motors.\n\n### Infrared configuration on Raspberry PI on System on Chip\n\nLike for the .NET nanoFramework implementation, the Infrared uses SPI to output the commands. So you have to activate SPI and use one of the channel. Please see the [documentation here](https://github.com/dotnet/iot/blob/main/Documentation/raspi-spi.md) to see how to best do it on a Raspberry Pi. You can activate it differently depending on the platform you are using.\n\nIn all cases, you can go to the configuration page \u003chttp://ipaddress/Configuration\u003e of the board to adjust the settings. You can select a specific SPI channel (default on a Raspberry is 0) and a chip select. The chip select is optional. If you don't need chip select, just set it to -1.\n\nIf your configuration, is valid, you will get a confirmation that the Infrared Lego class has been able to be created. This doesn't mean that all will perfectly work. Go to the test page and try one of the command. Replace the infrared led by a normal led, you'll see it blinking!\n\n### Switches with servo motors\n\nPiloting switches require as well specific electronic. In this case multiplexing as we have only 1 PWM output to pilot up to 16 switches. We are using a 74HC4515:\n\n[schema of the 74HC4515](/http://www.nxp.com/documents/data_sheet/74HC_HCT4515_CNV.pdf)\n\nHere is a proposed configuration on a Raspberry Pi:\n\n* A0 -\u003e GPIO16 (pin 36)\n* A1 -\u003e GPIO20 (pin 38)\n* A2 -\u003e GPIO21 (pin 40)\n* A3 -\u003e GPIO19 (pin 35)\n* E -\u003e Ground\n* LE -\u003e GPIO12 (pin 33), this is the default PWM0 pin\n\nAll output to be plugged to the driving pin of every servo motor. Servo motor to be at +5V voltage. The level of high for the 4515 is lower than the 3.3V delivered by the RPI, so no need of level converter to 5V.\n\nFor the hardware part, you'll need simple servo motor, here is a basic view on how to integrate them with the Lego switch:\n\n![Servo motor integration](/Assets/switches.jpg)\n\nYou must have the same servo everywhere as the piloting is done with the same configuration. Go to the \u003chttp://ipaddress/Configuration\u003e and adjust the Switch part.\n\nIn the configuration parameters, you'll need to select all the pins used for the multiplexing. If you only have 2 switches, you only need 1 GPIO setup, if you are using the 16 ones, you'll need the 4 of them!\n\nYou also need to give the characteristics of the servo motors you are using. IT does require the minimum pulse and the maximum one. By default, the command will close the switch at the minimum pulse and open it at the maximum one. It is recommended to test the setup to find the optimal values.\n\nYou also need to make sure PWM is properly activated on your device. For this, please follow the [documentation here](https://github.com/dotnet/iot/blob/main/Documentation/raspi-pwm.md).\n\nYou can only pilot 1 switch at a time, this is a limitation.\n\n### Signals with led\n\nPiloting signals is quite straight forward using the a 74HC595. You can virtually chain as many as you want to have as many signals.\nPlease note that so far, it is limited in the code to 16.\n\n![Electronic for signals](/Assets/signal.jpg)\n\nEvery 74HC595 can handle 4 signals, chain them as for any 74HC595. Here is an example of configuration for a Raspberry Pi:\n\n* Pin 8 is ground\n* Pin 16 is VCC (+5V recommended)\n* Pin 14 to MOSI GPIO10 (physical pin 19) for the first 74HC595 of the chain. For the next one it has to be linked to the Pin 9 of the previous 74HC595\n* Pin 13 to ground (used to activate or not the 8 output)\n* Pin 12 to GPIO7 (physical pin 26), used to select the SPI\n* Pin 11 to SCLK GPIO11 (physical pin 23)\n* Pin 10 to +VSS (this one is used to reset if needed)\n* Pin 15, 1, 2, 3, 4, 5, 6, 7 are the output pin\n* Pin 9 has to be linked to the next 74HC595 of the chain if there are more than 1.\n\nThe configuration can be adjusted in the \u003chttp://ipaddress/Configuration\u003e page. You will have to set the SPI bus number and you need to use a chip select if you are sharing the SPI with the Infrared part. See how to activate SPI and chip select in the [documentation here](https://github.com/dotnet/iot/blob/main/Documentation/raspi-spi.md).\n\n### Running in docker\n\nYou can run this solution on docker by building the images or pulling them.\nTBD\n\n* params for the ports `-p 80:80`\n* volume mount for the config `-v ~/config:/app/config`\n* privileged mode `--privileged`\n\n## Using the API\n\nThe detailed API protocol can be [found here](./Lego_api_doc.md). It is strongly recommended to read the documentation to understand how to best use all this.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fellerbach%2Flegoinfrared","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fellerbach%2Flegoinfrared","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fellerbach%2Flegoinfrared/lists"}