MultiWii C Source

This forum is dedicated to software development related to MultiWii.
It is not the right place to submit a setup problem.
Software download
Post Reply
itain
Posts: 75
Joined: Tue Aug 23, 2011 10:32 pm

MultiWii C Source

Post by itain »

Continuing my suggestion here. I have moved the MultiWii code to plain c files.
My simple configuration (CRIUS) compiled in the Arduino configuration. So far only tested using MultiWiiConf.

To do:
1. Arduino defines pins this way (Stupid!!!)

Code: Select all

static const uint8_t A2 = 16;

C does not accept it as a compile-time constant, which causes compile error in Output.c. I hacked around it for ProMini. Need to do it for other boards too. I suspect that these definitions also take some real memory, which is duplicated for each C source.

2. Arduino compiles C files without the -std=gnuc99 options. Statements like

Code: Select all

for(uint8_t i=0...

are invalid. Must move the definitions of the iterator outside the for loop. I done it in some places (maybe missed some). I refuse to touch some functions in LCD.h which are _)(*&%**&^%*

Find the code here, or use:

Code: Select all

git clone --branch=Use_C_Files git://github.com/itain/multiwii-firmware.git MultiWii


-- Itai

PS. I decided to post this here and not in the 32bit because I'm actually interested in 8 bit arch that is not supported by Arduino. Has anyone looked at the xmega processors? I think it would be interesting to port to ATXMega32A4U (or other XMegas).

timecop
Posts: 1880
Joined: Fri Sep 02, 2011 4:48 pm

Re: MultiWii C Source

Post by timecop »

Good luck getting multiwii dudes to stop doing for (uint8_t i = blah);
Already asked a week ago and got flamed.

itain
Posts: 75
Joined: Tue Aug 23, 2011 10:32 pm

Re: MultiWii C Source

Post by itain »

dongs, I actually like the C99 feature that allows iterator declaration in for statement. I would use it if I could. (I also use Linux).
mmm.... I think we can agree about some "qualities" of arduino.

Possibly this is also good place to explain why I don't touch LCD.c (just one example):

Code: Select all

void i2c_OLED_send_logo(void){
  unsigned char i,j;
  ...
  for(int i=0; i<1024; i++) {          // fill the display's RAM with graphic... 128*64 pixel picture
     ...
  }
  ...
  for(j=0; j<2; j++){
    for(i=0x01; i<0xff; i++){
      ....
      delay(1);
    }
}

I can accept either traditional C or C99 style. Not this confusing mix between them!
(That code also uses delays sparingly. Is it only used in initialization?)

Tommie
Posts: 438
Joined: Sun Apr 08, 2012 9:50 am

Re: MultiWii C Source

Post by Tommie »

itain wrote:2. Arduino compiles C files without the -std=gnuc99 options. Statements like

Code: Select all

for(uint8_t i=0...

are invalid. Must move the definitions of the iterator outside the for loop. I done it in some places (maybe missed some). I refuse to touch some functions in LCD.h which are _)(*&%**&^%*

That's because it's C++ code - why not use the proper extension .cpp for that?

itain
Posts: 75
Joined: Tue Aug 23, 2011 10:32 pm

Re: MultiWii C Source

Post by itain »

Tommie wrote:That's because it's C++ code - why not use the proper extension .cpp for that?

Thanks Tommie. I already considered it. If you have seen the latest commit the GPS code must compile as C++, so not I have a mix of C and C++. I may switch the other files to .cpp and forget about these issues.

My bigger question now - How can I get this (or similar changes) into the main release? MultiWii requires some changes in order to enable porting to non-arduino boards, this is the main reason why I started this. I look for feedback on the concept and not on the details.
Link

Alexinparis
Posts: 1630
Joined: Wed Jan 19, 2011 9:07 pm

Re: MultiWii C Source

Post by Alexinparis »

If possible, I would prefer to stick on genuine .c + .h code even if it implies some code change. (and I know some code must be adapted)
I don't want to open the door for OO style.
I like only one thing in C++: the ability to comment via // ;)

itain
Posts: 75
Joined: Tue Aug 23, 2011 10:32 pm

Re: MultiWii C Source

Post by itain »

Thanks Alex.
Lets see... GPS.ino is now the only place that's using a C++ class. Translating it to pure C is not a big effort. Is that EOSBandi's code? I would like to ask for his opinion before touching it.

In Output.ino, initialization of PWM_PIN is using A2 etc. that is not supported by C.
Arduino pin numbers are currently used as parameters for pinMode and attachInterrupt. These are mixed with uses of actual ports. I suggest to replace all uses of pinMode with proper assignments to DDRx registers and with ISRs.
* To work around the compile error, I user #define for A2. Can do it also for other processors.
* PWM_PIN is also used in Serial.ino for request MSP_MOTOR_PINS. What's the motivation and is it used by any serial client?

Tommie
Posts: 438
Joined: Sun Apr 08, 2012 9:50 am

Re: MultiWii C Source

Post by Tommie »

itain wrote:In Output.ino, initialization of PWM_PIN is using A2 etc. that is not supported by C.
Arduino pin numbers are currently used as parameters for pinMode and attachInterrupt. These are mixed with uses of actual ports. I suggest to replace all uses of pinMode with proper assignments to DDRx registers and with ISRs.

I support this motion. Breaking free of the Arduino libraries should free up some flash memory, considering that any conversion between Arduino inputs (Ax, Dx) is done during runtime, requiring the inclusion of mapping tables into the firmware.

palton
Posts: 48
Joined: Fri Sep 02, 2011 4:06 pm

Re: MultiWii C Source

Post by palton »

Why isn't OO style good Alex? just curious.

20 years ago when I went to the university, OO programming (c++) was mandatory in the education. The lecturer was banging it into our heads. The main reason then was to be able to encapsulate code in classes, were different programers can program and manage their own classes.

I haven't been practicing programming in many years, so I'm not up to date what's happening in the programming world really. I guess I might be a dinasaur in this disucussion, hehe. As I said, just curious why OO style isn't good?

Something happened regarding OO over time, or just personal perferenses?

itain
Posts: 75
Joined: Tue Aug 23, 2011 10:32 pm

Re: MultiWii C Source

Post by itain »

palton wrote:Why isn't OO style good Alex? just curious.

OOP is actually very good if there are enough computing resources.

MultiWii would mostly run on systems that don't have such resources. The ATMegs328p runs on 16mhz and has only 2KB ram.

My red lines would be:
No dynamic allocation (malloc, new).
No virtual functions.
Mo class inheritance.
No constructor/destructor for C++ classes.
That in fact takes away most of the OOP part of C++.

PS.
The paradigm of OOP requires both encapsulation and inheritance.
Some encapsulation can be achieved by using static variables in C (scope is limited to a single file).

Tommie
Posts: 438
Joined: Sun Apr 08, 2012 9:50 am

Re: MultiWii C Source

Post by Tommie »

itain wrote:The paradigm of OOP requires both encapsulation and inheritance.
Some encapsulation can be achieved by using static variables in C (scope is limited to a single file).

I agree. Proper namespacing would makes things much easier, be it via classes or ye olde file scope (static).

itain
Posts: 75
Joined: Tue Aug 23, 2011 10:32 pm

Re: MultiWii C Source

Post by itain »

I have just created a new branch that's using C++ files. Trying to migrate from arduino to C required too many changes to do in one step.

Using a C++ compiler does not mean that I encourage use of any OOP features in MultiWii.

This branch is also tested (compile only) in more configurations.

https://github.com/itain/multiwii-firmware/tree/Use_CPP_Files

Code: Select all

git clone --branch=Use_CPP_Files git://github.com/itain/multiwii-firmware.git MultiWii

timecop
Posts: 1880
Joined: Fri Sep 02, 2011 4:48 pm

Re: MultiWii C Source

Post by timecop »

itain wrote:Thanks Alex.
Lets see... GPS.ino is now the only place that's using a C++ class. Translating it to pure C is not a big effort. Is that EOSBandi's code? I would like to ask for his opinion before touching it.


It's already converted to C in baseflight:
http://code.google.com/p/afrodevices/so ... /src/gps.c

Gimbal
Posts: 146
Joined: Tue Jul 24, 2012 7:14 pm
Location: Sweden

Re: MultiWii C Source

Post by Gimbal »

Thnx itain

itain
Posts: 75
Joined: Tue Aug 23, 2011 10:32 pm

Re: MultiWii C Source

Post by itain »

dongs wrote:
itain wrote:It's already converted to C in baseflight:
http://code.google.com/p/afrodevices/so ... /src/gps.c


Alex was faster than me! I will merge the changes from the main branch.
(I'm new to git... time to learn how to do that).

Post Reply