The below code starts off with lots of data in the beginning, distracting the reader from the actual code. What is the best practice to clean this up:
#include <Wire.h>
// For LED Backpack
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
// For LCD Shield
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
// For pixel Units, such as hero
#include <Unit.h>
Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
Unit *hero;
boolean doesNeedRedraw = true;
boolean textNeedsRedraw = true;
int8_t lastButtonPress = 0;
int8_t frame = 0;
void setup() {
/* Important Setup Routine */
}
static const uint8_t PROGMEM
smile_bmp[] =
{ B00111100,
B01000010,
B10100101,
B10000001,
B10100101,
B10011001,
B01000010,
B00111100 },
neutral_bmp[] =
{ B00111100,
B01000010,
B10100101,
B10000001,
B10111101,
B10000001,
B01000010,
B00111100 },
frown_bmp[] =
{ B00111100,
B01000010,
B10100101,
B10000001,
B10011001,
B10100101,
B01000010,
B00111100 },
walls_bmp[] =
{ B10100000,
B10110000,
B10010000,
B11011100,
B01000100,
B01110100,
B00010100,
B00011100 };
void loop() {
/* Important Main Loop Routine */
}
-
Who is your reader? What is the propose of cleaning your code? What is the skill level of the reader?benathon– benathon2015年05月11日 13:05:37 +00:00Commented May 11, 2015 at 13:05
2 Answers 2
One way to clean that up is to create a "Globals.inc" file and cut and paste all of your globals into there. After that's done, simply include the globals file from your main .ino file with #include "Globals.inc"
(Main.ino)
#include <Wire.h>
// For LED Backpack
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
// For LCD Shield
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
// For pixel Units, such as hero
#include <Unit.h>
#include "Globals.inc"
void setup() {
/* Important Setup Routine */
}
void loop() {
/* Important Main Loop Routine */
}
(Globals.inc)
Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
Unit *hero;
boolean doesNeedRedraw = true;
boolean textNeedsRedraw = true;
int8_t lastButtonPress = 0;
// 0-255
uint8_t frame = 0;
static const uint8_t PROGMEM
smile_bmp[] =
{ B00111100,
B01000010,
B10100101,
B10000001,
B10100101,
B10011001,
B01000010,
B00111100 },
neutral_bmp[] =
{ B00111100,
B01000010,
B10100101,
B10000001,
B10111101,
B10000001,
B01000010,
B00111100 },
frown_bmp[] =
{ B00111100,
B01000010,
B10100101,
B10000001,
B10011001,
B10100101,
B01000010,
B00111100 },
walls_bmp[] =
{ B10100000,
B10110000,
B10010000,
B11011100,
B01000100,
B01110100,
B00010100,
B00011100 };
-
I wouldn't use the .h extension, as this is usually used for header files which have a connection to a .cpp or .c file with the same name.Joris Groosman– Joris Groosman2015年05月09日 14:55:32 +00:00Commented May 9, 2015 at 14:55
-
Thanks, I will edit it the answer. I take it that it should be a .cpp extension instead of .h extension?Ninjaxor– Ninjaxor2015年05月11日 18:14:53 +00:00Commented May 11, 2015 at 18:14
-
.cpp is a possibility. Personally I prefer .incJoris Groosman– Joris Groosman2015年05月11日 18:52:55 +00:00Commented May 11, 2015 at 18:52
There is actually not a lot wrong with the way the code is. You are writing / using embedded code. It is not expected to be some planned out UML model, and does not need to look like it is.
If you just want less lines, start by converting your binary data to hex. Otherwise the answer of a global.h is valid (and I would still convert binary to hex). You could easily shrink 24 lines down to 3.
If you could use a real IDE navigation would be unimportant, but since Arduino chose not to use a standard build process it takes extra effort (but still worth it). You could also switch to native AVR (and easily use a real IDE), which is what I plan to do as soon as the proof of concept is finished.
-
I'm developing from a command line basis as that I'm a rails developer and have learned how powerful the CLI approach can be. I found something called
platformio
which compiles and uploads source code to an arduino, and can even be set to compile for additional boards too based on a configuration file. You might wanna checkplatformio
out after AVR. Another benefit is library management.Ninjaxor– Ninjaxor2015年05月08日 16:27:46 +00:00Commented May 8, 2015 at 16:27 -
Interesting. I had not heard of it. I bookmarked it and will look at it, thanks. Best case is Atmel comes out with a new Studio that uses the new VS 2015 stuff soon. Until then I limp around using both VS 2015 and Arduino IDE at the same time (external editor setting). It is livable. CLI vs GUI is a preference, nothing more.user6569– user65692015年05月08日 17:01:34 +00:00Commented May 8, 2015 at 17:01