Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: char to String to char conversion on: May 28, 2011, 09:15:11 pm
Quote
Back to the start. It has been my experience that the material downloaded from the net has far too many characters to be saved in total to a string on an arduino. Have you been able to capture all that is downloaded into a single string?
   

Yes. And reliably, too.
2  Using Arduino / Programming Questions / Re: char to String to char conversion on: May 28, 2011, 12:30:45 pm
Nick,

Sorry to be thick about it, but it's not clear to me what you need.  You're asking me for how newsString is defined, but I've posted that twice, which makes me think I'm not interpreting your questions correctly.

newsString is defined like this:
Code:
char newsString[85]; // make sure this is large enough for the largest string it must hold

Are you asking for the contents of newsString[]?  For an example of a replacement instance?  The data is being pulled from here:

http://www.google.com/ig/api?news

I'm randomly selecting <title data>, then parsing the headline.  So a typical string would be:

<title data="Casey Anthony&amp;amp;#39;s mother becomes emotional on stand"/>

which I then peel off everything outside the quotes, leaving the raw headline and that's what ends up in newsString[].

But as you can see, it contains the messiness of the smart quotes, hence the need to search & replace them.

Is that what you're after?

Again, apologies if I've misunderstood.
3  Using Arduino / Programming Questions / Re: char to String to char conversion on: May 26, 2011, 11:08:56 pm
newsString contains a news headline, selected at random, from an XML feed.

Code:
  if (client.connected()) {   
    strcpy_P(buffer, (char*)pgm_read_word(&(string_table[14]))); // "title data="
    finder.find(buffer);
    for (int i = 0; i < randomInt; i++)  {
      if (finder.getString(buffer,"\"", newsString, 84))  {
      }
    }


That's immediately followed by your example code.

Interestingly, I tried the approach suggested by robtillaart, prior to your regex solution:

Code:
void replace(char* source, char* from, char* to)
{
  uint8_t f = strlen(from);
  uint8_t t = strlen(to);
  char *p = source;

  if (t> f) return;
  while (*p != '\0')
  {
    if (strncmp(p, from, f) == 0)
    {
      strncpy(p, to, t);
      p += t;
      strcpy(p, p+f-t);
    }
    else p++;
  }
}

and it works like a charm, substituting at will.  I'd still like to get the regex method working as it's a lot more versatile, I suspect.
4  Using Arduino / Programming Questions / Re: char to String to char conversion on: May 26, 2011, 05:26:16 pm
It's still as I originally posted.

Code:
char newsString[85]; // make sure this is large enough for the largest string it must hold

And yes, the "&amp" should properly be "&amp;#39;"

Cheers,
5  Using Arduino / Programming Questions / Re: char to String to char conversion on: May 26, 2011, 11:54:08 am
Nick,

This is brilliant.  I've grabbed the updated library in order to use GlobalReplace.  So now, I'm doing this:
Code:
    // for matching regular expressions 
    MatchState ms (newsString);
   
    // replace the &amp with a single quote '
    ms.GlobalReplace ("&amp", "\'");

But executing the GlobalReplace causes the sketch to crash. 

1. Does the GlobalReplace method use the memcpy techniques from your suggested code above?

2. Do I need to escape the single quote as I've done above?

Cheers,
6  Using Arduino / Programming Questions / Re: char to String to char conversion on: May 12, 2011, 10:57:48 pm
You are not defining d incorrectly. You are simply not defining it the way that you thought you were.

You should probably spend some time looking at the String source code. If you do, you will see that the copy operator and the assignment operator are defined for the String class, so one can assign a character array to a String object, and the String object will be automatically sized to hold the character array, whatever size the character array is, provided there is memory available to hold the String object that is to be created.

Aha.  With that in mind, this revision is probably a good idea:

Code:
  //Define d to be however big it needs to be in order to hold newsString and do a find and replace at the same time.
   String d = String(newsString).replace("&amp;amp39;", "\'");

   //Convert d back into newsString.
    d.toCharArray(newsString, 84);

If this looks right to you, and if I'm not walking off the end of the string, then that leaves your suggestion, that I'm running out of memory, as the only plausible explanation.

So, is the code itself correct?
7  Using Arduino / Programming Questions / Re: char to String to char conversion on: May 12, 2011, 10:38:06 am
Paul,

You wrote:

Quote
 
Quote
Code:
String d = String(85);
This creates a String object, d, equal to the String object that contains a character representation of the value 85, not one that contains room for 85 characters.

Aha.  That'll be the problem.

So, what is the correct way to create a String that is capable of holding the contents of newsString[]?

It's definitely possible that I'm running out of RAM. I'm already using techniques to move the static strings into program memory.

However, given that I'm creating d in correctly, not giving it 85 characters, but rather assigning it the value 85, isn't it more likely I'm running off the end of d?  And if that's the case, how do I create d with enough space for newsString[85]?  The String reference page shows many cases, none of which allow you create an empty string with 85 characters.  Am I really left with doing this?

Code:
String d = String("                                                                                                                                               ");
8  Using Arduino / Programming Questions / Re: char to String to char conversion on: May 12, 2011, 01:20:48 am
Quote
So, what is the correct way to create a String that is capable of holding the contents of newsString[]?

I doubt you will be able to capture the total feed to a string unless it is very small. You can capture the feed characters in a string and then send the string to the serial monitor to see just how much of the feed was captured.

I think this may be a distraction.  The existing code, prior to the .replace call works reliably and has done for a couple of weeks.  It's the .replace call that's the issue here.

As previously quoted, this bit of code:

Code:
if (finder.getString(buffer,"\"", newsString, 84))  {

uses the TextFinder.getstring call to stuff up to 84 characters of the current feed from the text contained in buffer[], up to the quote mark that's at the other end of the code.

In brief, the existing code works. 

The issue now is that I want to pass the resulting char newsString[] to String.replace, replace the string, then convert it back to a char[] and I'm clearly not calling String correctly in the process.
9  Using Arduino / Programming Questions / Re: char to String to char conversion on: May 11, 2011, 05:27:14 pm
To resolve your problem, we'd need to see more of your code. How is newsString defined? Is it a char array with 85 or more elements? How is buffer defined?

They're both globals.

Code:
char buffer[85]; // make sure this is large enough for the largest string it must hold
char newsString[85]; // make sure this is large enough for the largest string it must hold

Quote
 
Code:
String d = String(85);
This creates a String object, d, equal to the String object that contains a character representation of the value 85, not one that contains room for 85 characters.

Aha.  That'll be the problem.

So, what is the correct way to create a String that is capable of holding the contents of newsString[]?
10  Using Arduino / Programming Questions / char to String to char conversion on: May 11, 2011, 03:27:56 pm
Hi there.

I've been doing some string manipulation using the Ethernet shield and TextFinder for output onto an 8x8 RGB Matrix.

I can reliably pull an RSS news item like this:

Code:

  if (client.connected()) {   

    strcpy_P(buffer, (char*)pgm_read_word(&(string_table[14]))); // "title data="

    finder.find(buffer);

    for (int i = 0; i < randomInt; i++)  {
      if (finder.getString(buffer,"\"", newsString, 84))  {
      }
    }

The feed displays just fine on the matrix.

But the news item is not clean text.  It occasionally has "smart quotes" in it.  These appear in the feed as "&amp;amp;#39;" in the displayed feed. 

I want to replace the ugliness with a simple quote, '.

So, after searching high and low for a reliable replace function, I stumbled on the one right under my nose: String.replace.

However, the feed is dumped into char newsString[].  In order to use .replace, I need a String, so:

Code:
  // Make a new string.
  String d = String(85);

  //Replace the HTML nastiness with a single '
  d = String(newsString).replace("&amp;amp39;", "\'");
But then in order to display the information, I need to have a char again.  So:
Code:
    d.toCharArray(newsString, 84);

Kaboom.  The board freezes or resets itself.

Inserting debug statements shows that the replace is actually reached.

It feels like I'm misusing or incorrectly calling the .replace or handling the array badly.

Can anyone point me in the right direction?

Cheers,
11  Using Arduino / LEDs and Multiplexing / Re: Extending RGBMatrix.cpp for the SparkFun RGB Matrix with backpack. on: February 03, 2011, 03:21:15 am
Bingo.

Code:

extern const PROGMEM prog_char letters[]; //Get access to the 8X8 font to display letters on the matrix (located in RGB.h)
...
const PROGMEM prog_char letters[] = {
...
//Strange looking algorithm converts rows to columns and vice versa.
if(pgm_read_byte(&letters[letter_offset+(7-column)]) & (1<<row))
  screenBuffer[screen].pixel[row*8+column]=color;
else screenBuffer[screen].pixel[row*8+column]=BLACK;
12  Using Arduino / LEDs and Multiplexing / Flashing a Sparkfun 8 x 8 RGB Matrix with an Uno. on: February 02, 2011, 11:41:17 pm
I'm posting this to help anyone using 0022 who's got an Uno and an 8x8 RGB Matrix & Serial Backpack from Sparkfun.  Out of the box, the matrix was exhibiting some tearing and what looked like off-by-one errors when using the RGBMatrix library to control it.  These errors were supposed to be fixed in RGB_Backpack_v5.hex. 

If you've got a Duemilanove, you can use ArduinoISP sketch to upload the new firmware.  I've got an Uno, so I thought I wasn't able to upload the firmware, per:

http://arduino.cc/en/Tutorial/ArduinoISP

This afternoon, I stumbled on a patch for ArduinoISP here:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1286589145

I applied the patch manually, saved it as a new sketch. Then, I did this:

   1. Uploaded the patched ArduinoISP.
   2. Unplugged from USB.
   3. Disabled the autoreset.
   4. Connected the Matrix ISP pins in the prescribed manner.
   5. Connected the USB cable and let the Arduino boot.
   6. Ran the following from a terminal:

Code:
avrdude -P /dev/tty.usbmodem621 -b 19200 -c avrisp -p m328p -F -v -e -U flash:w:RGBBackpackv5.hex

And it worked. The firmware uploaded. Once it was done, I

   1. Unplugged the USB cable to power off the Arduino.
   2. Removed the disable of the autoreset.

No more "tearing" letters, no off-by-one problems.

Hope that helps someone.
13  Using Arduino / LEDs and Multiplexing / Extending RGBMatrix.cpp for the SparkFun RGB Matrix with backpack. on: February 02, 2011, 04:01:01 pm
Hi.

I've been working on a project that uses an Uno, the Arduino Ethernet board and a SparkFun 8 x 8 RGB Matrix with serial backpack.

Didn't take me long to figure out that the RGB.h file only contains A-Z, no numbers, no common punctuation.  So I went about the business of adding those characters.

Two hours later, I'd bashed a flat spot on my forehead - my program didn't work and it was because of the additional SRAM storage required by those characters.  When I removed them, everything was fine. 

That led me to using PROGMEM, but my programming skills are somewhat limited and I could use a little help, which is what brings me here.

That said, here's the issue:

RGBMatrix.cpp contains:

Code:
extern const char letters[]; //Get access to the 8X8 font to display letters on the matrix (located in RGB.h)

RGB.h contains:
Code:
const char letters[]  = {
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //0/ --> ' '
    ...
    0x00,0x61,0x51,0x49,0x45,0x43,0x00,0x00, //90/ --> Z
};

letters[] is called exactly once in RGBMatrix.cpp, in RGBMatrix.fillChar()

Code:
//Strange looking algorithm converts rows to columns and vice versa.
if(letters[letter_offset+(7-column)] & (1<<row))
screenBuffer[screen].pixel[row*8+column]=color;
            else screenBuffer[screen].pixel[row*8+column]=BLACK;

So, in order to use PROGMEM, did the following:

In RGB.h:
Code:
const prog_char letters[] PROGMEM = {
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //0/ --> ' '
and in

In RGBMatrix.cpp
Code:
//Strange looking algorithm converts rows to columns and vice versa.
if(pgm_read_byte_near(letters[letter_offset+(7-column)]) & (1<<row))
screenBuffer[screen].pixel[row*8+column]=color;
            else screenBuffer[screen].pixel[row*8+column]=BLACK;
and updated the lines pertaining to letter_offset:
Code:
letter_offset = letter - '0'+1;
and
Code:
if(!isalnum(letter))letter_offset=0;

It compiles, but I get garbage.  Font colours are being correctly transmitted, so I know that the RGBMatrix.scroll() is functioning.

My bet is that I'm not using pgm_read_byte_near correctly.

Can anyone point me in the right direction?
14  Forum 2005-2010 (read only) / Interfacing / Re: Arduino w/ Ethernet & Sparkfun LED matrix? on: January 12, 2011, 09:14:07 pm
Got it.  Thanks, Paul.  When the hardware arrives, I'll bash my head on this problem a little.

Cheers.
15  Forum 2005-2010 (read only) / Interfacing / Re: Arduino w/ Ethernet & Sparkfun LED matrix? on: January 10, 2011, 07:33:39 pm
NewSoftSerial.  Do I need to use it to move the CS pin to an unused pin? Or do I simply declare the CS pin I want to use inside the sketch code?
Pages: [1] 2