ControlChain library on Arduino Leonardo - got it working!

Hi there!

Got my little ControlChain test lab set up based on Arduino Pro Micro. It’s a little messy, with all the wires, but it works.

I like Pro Micro for its excellent size/capabilities ratio, I’m using it the most in my Arduino-based projects, and that’s why it was the only board (except for a Mega :wink:) that I had at hand. Pro Micro is based on ATmega32u4 and is, essentially, a miniature version of Leonardo, which is not listed as supported by ControlChain library, and, in fact, if you try and compile the library for Leonardo, you’ll get an error. I’m an amateur C++ coder, not a pro, but I have found a workaround for it, messing up a little with the library code, so I thought I’d share it so someone may find it useful too (and maybe this tweak will find its way into the git source of the library, who knows).
The bottomline is that Leonardo uses a serial port 0 for USB communication, and a serial 1 for hardware i/o over TX/RX pins, whilst the library tries to communicate through the registers of the serial 0 port. A couple of extra lines of code, and there you have it!

Now while I’m waiting for the spare parts from AliExpress to arrive for my massive footswitch/expression controller board I plan to build, I’m gonna play around with the lab :wink:


18 Likes

Excellent! That’s some great work. I see that you are directly connecting the UTP wire to the various pins on the micro. What is your eventual solution for the RJ45 port? I’ve been pondering that for my own projects.

2 Likes

@malfunction54, no, not a direct connection, I went in line with the Arduino shield schematics, I’m using MAX489EPD+ transciever, I’ve replicated the schematics directly on the breadboard.

3 Likes

Ah, gotcha!

1 Like

Thank you so much for sharing this @TheRedOne!
It actually may turn out to be really helpful even for us internally.

By the way, where have you shared the code?

3 Likes

I didn’t 'cos I’m not sure if the fix is universal (e.g. Arduino Mega also has more than one serial port), so I want to dig a little more into not only check for serial 1 availability, but to check if the board is ATmega32u4 based, or, alternatively, if serial 0 is indeed reserved for USB comms somehow…
Although I will share a snippet of a code later today in here.

4 Likes

Ok. Perfect :slight_smile: Thank you a lot

1 Like

Ok, here it is, the code I’ve added is between // ----------- BEGIN ------------------ and
// ----------- END ------------------.
A simple check if the registers for the serial 1 are defined, then addition of the code with those registers. Without it CCSerial function does not get defined for Leonardo, and you get a comple error.
I’m not even sure what the ISR is for, but I saw the same registers used there, and defied that as well, just in case.

This is ReUART.cpp, starting line 19.

#if defined(UBRRH) && defined(UBRRL)
HwSerial CCSerial(&UBRRH, &UBRRL, &UCSRA, &UCSRB, &UCSRC, &UDR);
// ----------- BEGIN ------------------
#elif defined(UBRR1H) && defined(UBRR1L)
HwSerial CCSerial(&UBRR1H, &UBRR1L, &UCSR1A, &UCSR1B, &UCSR1C, &UDR1);
// ----------- END ------------------
#else
HwSerial CCSerial(&UBRR0H, &UBRR0L, &UCSR0A, &UCSR0B, &UCSR0C, &UDR0);
#endif

// ISR
#if defined(USART_RX_vect)
ISR(USART_RX_vect)
// ----------- BEGIN ------------------
#elif defined(USART1_RX_vect)
ISR(USART1_RX_vect)
// ----------- END ------------------
#elif defined(USART0_RX_vect)
ISR(USART0_RX_vect)
#elif defined(USART_RXC_vect)
ISR(USART_RXC_vect) // ATmega8
#else
#error "Don't know what the Data Received vector is called for Serial"
#endif
6 Likes

Thank you so much for sharing this! I will make sure that our devs check it as well :slight_smile:

4 Likes