Does anyone know of any libraries available for the STM32 chip to use another language for a USB HID adapter / plug and play for a game controller? I would also need the ability to edit the driver display page so allow combining axis, and reversing them.
I know C++ would increase the overhead, [...]
and the chip I use has 32KB memory, if I recall correctly, but the current implementation of C would require a rewrite anyway just because it's such a mess from the original author so if it does, I may as well use a different language... I don't really care if I use C++, Python, Java, or Lua for the project - I know all of them fluently. I would be open to C# as it seems cleaner than C++ but I'd need to learn it.. I don't really have a preference but I'd prefer object oriented over basic C ( and Lua would work for this purpose too )...
I know C++ would increase the overhead, and the chip I use has 32KB memory
I'd be interested in seeing a real-world, non-trivial C++ codebase (not one explicitely written just to prove a point) for a small MCU (let's say a 32 KB or even 64 KB binary) that properly uses C++ constructs (and not just uses it as C with namespaces) and that really leads to similar or smaller code size and similar performance than an equivalent C codebase. We often hear or read about such claims, but it's often difficult to find real examples.
I'd be interested in seeing a real-world, non-trivial C++ codebase (not one explicitely written just to prove a point) for a small MCU (let's say a 32 KB or even 64 KB binary) that properly uses C++ constructs (and not just uses it as C with namespaces) and that really leads to similar or smaller code size and similar performance than an equivalent C codebase.
From what I have read about using C++ or another language with microcontrollers, they stated C++ increases overhead quite a bit so the onboard 32KB may not be enough - if that's wrong then no problem..
OR, it could be the library size is larger - I don't recall..
My main reason for switching is human readability and ease of maintenance because right now everything looks thrown together with minimal comments and there is so much code which has been repeated which doesn't need to be...
There is one obvious area in which the usual C++ library gives smaller programs than the standard C library: printing or formatting to a string or buffer using printf and friends. Because the printf format string is parsed at runtime, there is no way to know whether you're going to be printing strings, or characters or integers or floating point or what. The result is that the code for ALL of them has to be present in your final program.
With either standard C++'s streams library and << operator, or with something like the Arduino library's overloaded print() and println() functions your program binary should end up with formatting code for ONLY the actual datatypes you use.
In C you could avoid printf() and write and use print_char(), print_int(), print_string(), print_float() functions, but the code becomes more tedious to write and most people just stick with printf() and friends.
The key to reducing code is not avoiding libraries but using smaller versions of them. The printf example is a classic one. The best solution is to use a smaller printf and not re-invent the wheel.
One thing to consider is that printf() is very flexible since it allows to modify the format itself at execution time (since the format is just a string),
One thing to consider is that printf() is very flexible since it allows to modify the format itself at execution time (since the format is just a string),Try to compile something with GCC and you'll find out it won't allow to feed it a non-constant formatting string. For a good reason because it would impose a huge security hole.
#include <stdio.h>
int main(void)
{
char szFormat[256];
unsigned int i;
for (i = 1; i <= 10; i++)
{
sprintf(szFormat, "[%%0%uX]\n", i + 1);
printf(szFormat, i);
}
}
[01]
[002]
[0003]
[00004]
[000005]
[0000006]
[00000007]
[000000008]
[0000000009]
[0000000000A]
There is one obvious area in which the usual C++ library gives smaller programs than the standard C library: printing or formatting to a string or buffer using printf and friends. Because the printf format string is parsed at runtime, there is no way to know whether you're going to be printing strings, or characters or integers or floating point or what. The result is that the code for ALL of them has to be present in your final program.
With either standard C++'s streams library and << operator, or with something like the Arduino library's overloaded print() and println() functions your program binary should end up with formatting code for ONLY the actual datatypes you use.
In C you could avoid printf() and write and use print_char(), print_int(), print_string(), print_float() functions, but the code becomes more tedious to write and most people just stick with printf() and friends.The key to reducing code is not avoiding libraries but using smaller versions of them. The printf example is a classic one. The best solution is to use a smaller printf and not re-invent the wheel.
There is one obvious area in which the usual C++ library gives smaller programs than the standard C library: printing or formatting to a string or buffer using printf and friends. Because the printf format string is parsed at runtime, there is no way to know whether you're going to be printing strings, or characters or integers or floating point or what. The result is that the code for ALL of them has to be present in your final program.
With either standard C++'s streams library and << operator, or with something like the Arduino library's overloaded print() and println() functions your program binary should end up with formatting code for ONLY the actual datatypes you use.
In C you could avoid printf() and write and use print_char(), print_int(), print_string(), print_float() functions, but the code becomes more tedious to write and most people just stick with printf() and friends.The key to reducing code is not avoiding libraries but using smaller versions of them. The printf example is a classic one. The best solution is to use a smaller printf and not re-invent the wheel.
I don't want a heap so I avoid the string functions and I also avoid printf().
What I do is copy the string and conversion functions from "The C Programming Language" (K&R) and write up several functions that will output decimal, hex nibbles, bytes, shorts and ints. Then all I need is character and string IO over the serial port and I'm ready to go. I don't tend to do a lot of serial input. If any...
The thing about this low level code is that it is easy to port from chip to chip. Just copy and paste!
Printf (and associated functions) don't use the heap at all!
There is one obvious area in which the usual C++ library gives smaller programs than the standard C library: printing or formatting to a string or buffer using printf and friends. Because the printf format string is parsed at runtime, there is no way to know whether you're going to be printing strings, or characters or integers or floating point or what. The result is that the code for ALL of them has to be present in your final program.
With either standard C++'s streams library and << operator, or with something like the Arduino library's overloaded print() and println() functions your program binary should end up with formatting code for ONLY the actual datatypes you use.
In C you could avoid printf() and write and use print_char(), print_int(), print_string(), print_float() functions, but the code becomes more tedious to write and most people just stick with printf() and friends.The key to reducing code is not avoiding libraries but using smaller versions of them. The printf example is a classic one. The best solution is to use a smaller printf and not re-invent the wheel.
I don't want a heap so I avoid the string functions and I also avoid printf().
What I do is copy the string and conversion functions from "The C Programming Language" (K&R) and write up several functions that will output decimal, hex nibbles, bytes, shorts and ints. Then all I need is character and string IO over the serial port and I'm ready to go. I don't tend to do a lot of serial input. If any...
The thing about this low level code is that it is easy to port from chip to chip. Just copy and paste!I think you are misinformed here. Printf (and associated functions) don't use the heap at all! They use the buffer(s) you provide and perhaps a very small buffer which is allocated statically or on the stack. Especially C libraries which are written for embedded platforms have a very small memory footprint. If you write your own functions then you are re-inventing the wheel. The C library has been designed in an era when memory was very limited so way the functions are designed is very clever.
QuotePrintf (and associated functions) don't use the heap at all!
People keep saying that, but I don't think many have actually looked. I'd challenge you to write a "hello world" program that uses a standard printf() library and write to a UART on any ARM chip/environment you like, and check the linker output to see if malloc() is there or not. (if not, please post instructions on how to replicate!)
.data 0x1000000c 0x0 libcmini-arm-none-eabi-thumb2-os.a(atoi.o)
.data 0x1000000c 0x0 libcmini-arm-none-eabi-thumb2-os.a(printf.o)
.data 0x1000000c 0x0 libcmini-arm-none-eabi-thumb2-os.a(vuprintf.o)
.data 0x1000000c 0x0 libcmini-arm-none-eabi-thumb2-os.a(puts.o)
.data 0x1000000c 0x0 libcmini-arm-none-eabi-thumb2-os.a(memchr.o)
.data 0x1000000c 0x0 libcmini-arm-none-eabi-thumb2-os.a(strncmp.o)
.data 0x1000000c 0x0 libcmini-arm-none-eabi-thumb2-os.a(strncpy.o)
.data 0x1000000c 0x0 libcmini-arm-none-eabi-thumb2-os.a(memcpy.o)
.data 0x1000000c 0x0 libcmini-arm-none-eabi-thumb2-os.a(memset.o)
.data 0x1000000c 0x0 libcmini-arm-none-eabi-thumb2-os.a(strlen.o)
.data 0x1000000c 0x0 /usr/local/bin/arm-gcc/lib/gcc/arm-none-eabi/4.6.2/armv6-m/libgcc.a(_thumb1_case_uqi.o)
.data 0x1000000c 0x0 /usr/local/bin/arm-gcc/lib/gcc/arm-none-eabi/4.6.2/armv6-m/libgcc.a(_udivsi3.o)
.data 0x1000000c 0x0 /usr/local/bin/arm-gcc/lib/gcc/arm-none-eabi/4.6.2/armv6-m/libgcc.a(_divsi3.o)
.data 0x1000000c 0x0 /usr/local/bin/arm-gcc/lib/gcc/arm-none-eabi/4.6.2/armv6-m/libgcc.a(_dvmd_tls.o)
The C library I use is the one which comes with MSP430 GCC but then compiled for ARM. It is very lean and I have been using it for over a decade. If a C library pulls in malloc then it is not suitable for embedded work but it is still not a reason to avoid printf. You just need to use the appropriate C library. I can't imagine an embedded environment providing an 'unsuitable' C library.I can't imagine an embedded environment providing an 'unsuitable' C library.
The C library I use is the one which comes with MSP430 GCC but then compiled for ARM.
Anecdotal remarks on the Internet suggest that printf() is huge in microcontroller terms. What I didn't see was numbers.