Arduino program code in .data - arduino

It seems as though some of my functions are being placed into the .data section. This is for a library that has classes.
I've looked at the memory map as suggested here:
http://www.nongnu.org/avr-libc/user-manual/group_demo_project.html
I've also been using avr-size to see the size of the .data and .text questions.
Any ideas why the program code is getting placed in .data and not .text?

I think I figured out what was happening.
It only looked like code was going into the .data section. What was actually going in there were the char * from debug messages and thus taking up large fractions of space.
For example, I had a bunch of Serial.println("debug message that is a long string."); An easy way around this for Serial.println is the use of the F() macro, which stores the string in FLASH instead of RAM (.data section I was seeing).
Also, this link provides some good info on memory conservation of strings:
http://arduino.cc/en/Reference/PROGMEM

Related

sprintf causing timing-issues?

I'm experiencing a perculiar issue when using sprintf.
I need a char array to look like this: g;cmd;arg;e;, where arg gets leading zeros so it is always 3 chars long, and cmd gets leading zeros so it's always 15 chars long.
As an example, if cmd = 20 and arg = 3749, I need a char array looking like this: g;020;000000000003749;e;.
Both arg and cmd are integers.
I initially accomplished this in pretty inefficient way, but I changed it to something much simpler using sprintf because I needed my code to be faster. Both my initial code and my change can be found on github.
My current implementation looks like this:
#define cmdMsgLength 3
#define argMsgLength 15
#define totalFormatedMsgLength (2+cmdMsgLength+1+argMsgLength+3)
#define msgFormater "g;%03d;%015d;e;"
char msgToSendFormated[totalFormatedMsgLength];
void sendMsg(int _cmd, int _arg) {
sprintf(msgToSendFormated, msgFormater, _cmd, _arg);
Serial.print(msgToSendFormated);
}
This seemed to work well, until my uC also had to control 4 ESC's. I honestly can't find any relation between the two, but it seems like this implementation leads to problems with the ESC's, where of course timing is quite important. The ESC's are being programmed correctly, but when using the Arduino function servo.writeMicroseconds to actuate them, they seem to act randomly. After quite a lot of tests, only this change to my code seems to be causing the issue. As this piece code is so simple and the old code (check the github link) also used Serial.print, I assume sprintf is the culprit.
Is sprintf known to cause these sort of timing-issues? Could it be anything else?
As JVApen pointed out, sprintf always writes a null terminator. As msgToSendFormated was not long enough for that, I'd get an overflow. Setting char msgToSendFormated[totalFormatedMsgLength + 1]; fixed the problem.

make the middle letter blink

I've already solved this by not displaying the last letter of the word then locating the last letter and making it blink then I displayed the word inversely minus the last letter of course.
#include<string.h>
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
char text[255];
int txtposition,txtlength;
void main()
{
clrscr();
gets(text);
txtlength=strlen(text);
char lastchar=text[txtlength-1];
cout<<"Your text is: ";
for(txtposition=0;txtposition<txtlength-1;txtposition++)
{
cout<<text[txtposition];
}
textcolor(WHITE+128);
cprintf("%c", lastchar);
for(txtposition=txtlength-2;txtposition>=0;txtposition--)
{
cout<<text[txtposition];
}
getch();
}
Thank you for all your help!
To make the middle character blink, either your output terminal needs to be capable to present blinking characters using a special terminal control code as described here, or use the gotoxy() function from a separate thread, that displays a ' ' or the actual character, alternating for a specific blink frequency.
The standard C++ library does not provide any facility for making characters blink.
You can do that in platform-specific ways, but it's worth noting that Windows console windows do not (as far as I know) directly support text blinking, like the original IBM PC's text screen mode did. On the original IBM PC one bit of the color specification could be configured to either yield high intensity or blinking, with blinking control as the default. I always reconfigured it to high intensity in my programs, and in the corresponding mechanism for Windows console windows the bits always determine color.
So, it would be complicated to do even in Windows, unless you're running in a DOSBox, which emulates the old PC. I don't know what functionality it offers. Maybe it even does blinking.
But you can easily mark the relevant letters in other ways.
For example, you could use
uppercase versus lowercase,
underlining characters placed on the next line,
parentheses (as you did in your example here),
colors (platform specific),
a different font, boldness, whatever.
I recommend updating to a modern compiler, if you have an ordinary modern PC. Compilers are free. Also you need better learning material, e.g. void main is non-standard and is only accepted by a few compilers.
Looks like for Turbo C/C++ you can you use the Graphics library and/or builtin conio functions. ( https://answers.yahoo.com/question/index?qid=20080813072809AAEguz0 )
But the above is not portable as the graphics library is specific to Turbo and conio is specific to some dos based compilers/libraries.
If you move to the a complier like gcc/g++ then you might want to look at curses library: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

Is there a length limitation of qDebug()<<?

In my code, I found one qDebug()<<s; didn't output the content given to it. It's very strange, so I output s.length(). It says 135254, so I'm wondering whether there is a limitation of length which is shorter than 135254? I have read the source header and do not find the result.
i noticed something similar...
This might narrow it down a bit (using Qt 5.5.1 with Windows mingw):
45000 character trace did not show up in qDebug.
10000 character trace did show up in qDebug.
I was able to qDebug my 45000 characters as 5 separate qDebug calls, each was something like this:
qDebug()<<foo.mid(20000, 10000);
then piece them together in a text editor.
it may have been a little nicer to use qDebug's noQuotes feature too, but i don't need to run my script again today so I'm not prettying it up right now.

Reading multiple lines from QTextedit

I'm trying to write my own code-editor, I figure its a good way to learn pyQt.
I am using a qtextedit, in which i can write code(it's not real code, more pseudo code).
Each line represents ending in a semi-colon represents some command
e.g.
PSEUDO->FWD->90;
PSEUDO->STOP;
PSEUDO->RIGHT 90;
PSEUDO->FWD 10;
These are relatively easy to read, as the user presses the [ENTER] the current line is read, parsed and checked for errors so the following
PSEUDO->RIGHT -pi/2
would generate an error because the line doesn't end in a semi-colon and the value following RIGHT needs to be a number.(my editor, my rules).All this I have more or less got working.
I would like to know how to do multiple lines though. I am familiar with editors such as Eclipse,sublime or visual studio which handle muliple lines very well, in my case
PSEUDO->DO:
FWD->90
RIGHT->45
FWD->10
LEFT->55
FWD->50
STOP;
Should all be read in and treated as one statement, starting at the keyword PSEUDO and ending at the semi-colon.
However the following should be read as 3 separate statements.
PSEUDO->DO:
FWD->90
RIGHT->45
FWD->10
LEFT->55
FWD->50
STOP;
PSEUDO->DO:
FWD->90
RIGHT->45
STOP;
PSEUDO->BACK 10;
My question how can I go about reading muliple lines as described above from QTextEditor as discreet statements.
Should I do the parse/check whenever I press the [ENTER] key for a new line?
I'm using python2.7,pyQT, and QTextEdit.
Basically what you are trying to do, can be done with all the text of the document and some regular expressions. I would be careful with this course, because it may slow things down.
So to stay lean and to use the right classes of Qt, I would use anything related to the Rich Text Processing, QTextDocument, and QTextCursor.
I have used QSyntaxHighlighter quite a bit, and QRegExp. There is now QRegularExpression, too. Here are the classes and general documentation I'd look at to get started with Rich Text processing.
http://doc.qt.io/qt-5/qtextblock.html#details
http://doc.qt.io/qt-5/richtext-structure.html
http://doc.qt.io/qt-5/richtext.html
http://doc.qt.io/qt-5/richtext-cursor.html
http://doc.qt.io/qt-5/richtext-common-tasks.html#finding-text
Hope that helps.

Unix write() function (libc)

I am making a C application in Unix that uses raw tty input.
I am calling write() to characters on the display, but I want to manipulate the cursor:
ssize_t
write(int d, const void *buf, size_t nbytes);
I've noticed that if buf has the value 8 (I mean char tmp = 8, then passing &tmp), it will move the cursor/pointer backward on the screen.
I was wondering where I could find all the codes, for example, I wish to move the cursor forward but I cannot seem to find it via Google.
Is there a page that lists all the code for the write() function please?
Thank you very much,
Jary
8 is just the ascii code for backspace. You can type man ascii and look at all the values (the man page on my Ubuntu box has friendlier names for the values). If you want to do more complicated things you may want to look at a library like ncurses.
You have just discovered that character code 8 is backspace (control-H).
You would probably be best off using the curses library to manage the screen. However, you can find out what control sequences curses knows about by using infocmp to decompile the terminfo entry for your terminal. The format isn't particularly easy to understand, but it is relatively comprehensive. The alternative is to find a manual for the terminal, which tends to be rather hard.
For instance, I'm using a color Xterm window; infocmp says:
# Reconstructed via infocmp from file: /usr/share/terminfo/78/xterm-color
xterm-color|nxterm|generic color xterm,
am, km, mir, msgr, xenl,
colors#8, cols#80, it#8, lines#24, ncv#, pairs#64,
acsc=``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
bel=^G, bold=\E[1m, clear=\E[H\E[2J, cr=^M,
csr=\E[%i%p1%d;%p2%dr, cub=\E[%p1%dD, cub1=^H,
cud=\E[%p1%dB, cud1=^J, cuf=\E[%p1%dC, cuf1=\E[C,
cup=\E[%i%p1%d;%p2%dH, cuu=\E[%p1%dA, cuu1=\E[A,
dch=\E[%p1%dP, dch1=\E[P, dl=\E[%p1%dM, dl1=\E[M, ed=\E[J,
el=\E[K, enacs=\E)0, home=\E[H, ht=^I, hts=\EH, il=\E[%p1%dL,
il1=\E[L, ind=^J,
is2=\E[m\E[?7h\E[4l\E>\E7\E[r\E[?1;3;4;6l\E8, kbs=^H,
kcub1=\EOD, kcud1=\EOB, kcuf1=\EOC, kcuu1=\EOA,
kdch1=\E[3~, kf1=\E[11~, kf10=\E[21~, kf11=\E[23~,
kf12=\E[24~, kf13=\E[25~, kf14=\E[26~, kf15=\E[28~,
kf16=\E[29~, kf17=\E[31~, kf18=\E[32~, kf19=\E[33~,
kf2=\E[12~, kf20=\E[34~, kf3=\E[13~, kf4=\E[14~,
kf5=\E[15~, kf6=\E[17~, kf7=\E[18~, kf8=\E[19~, kf9=\E[20~,
kfnd=\E[1~, kich1=\E[2~, kmous=\E[M, knp=\E[6~, kpp=\E[5~,
kslt=\E[4~, meml=\El, memu=\Em, op=\E[m, rc=\E8, rev=\E[7m,
ri=\EM, rmacs=^O, rmcup=\E[2J\E[?47l\E8, rmir=\E[4l,
rmkx=\E[?1l\E>, rmso=\E[m, rmul=\E[m,
rs2=\E[m\E[?7h\E[4l\E>\E7\E[r\E[?1;3;4;6l\E8, sc=\E7,
setab=\E[4%p1%dm, setaf=\E[3%p1%dm, sgr0=\E[m, smacs=^N,
smcup=\E7\E[?47h, smir=\E[4h, smkx=\E[?1h\E=, smso=\E[7m,
smul=\E[4m, tbc=\E[3g, u6=\E[%i%d;%dR, u7=\E[6n,
u8=\E[?1;2c, u9=\E[c,
That contains information about box drawing characters, code sequences generated by function keys, various cursor movement sequences, and so on.
You can find out more about X/Open Curses (v4.2) in HTML. However, that is officially obsolete, superseded by X/Open Curses v7, which you can download for free in PDF.
If you're using write just so you have low-level cursor control, I think you are using the wrong tool for the job. There are command codes for many types of terminal. VT100 codes, for example, are sequences of the form "\x1b[...", but rather than sending raw codes, you'd be much better off using a library like ncurses.

Resources