Show Posts
Pages: 1 ... 40 41 [42] 43 44 ... 66
616  Using Arduino / Programming Questions / Re: Serial problem with accentized characters on: December 01, 2012, 01:16:34 am
Ok there I fixed it, might be useful for someone else so I post it smiley

Code:
const uint8_t WINDOWS_1252 = 0;

int8_t SerialInputToCharSet( const char *src, char *dest, const size_t num = 128, const uint8_t charset = WINDOWS_1252);
int8_t SerialInputToCharSet( const char *src, char *dest, const size_t num, const uint8_t charset )
{
  uint8_t
    len = strlen( src );
  
  if ( len > num )
  {
    return -1;
  }
  
  uint8_t
    i = 0,
    j = 0,
    n = 0,
    c = 0;
    
  char
    tmp[num];

  switch ( charset )
  {
    case WINDOWS_1252 :
    {
      while ( i < len && j < num - 2 )
      {
        c = (uint8_t)src[i++];
        
        switch ( c )
        {
          case 0 ... 127 :
          {
            tmp[j++] = c;
            break;
          }
          case 128 :
          {
            tmp[j++] = 0xE2;
            tmp[j++] = 0x82;
            tmp[j++] = 0xAC;
            n++;
            break;
          }
          case 130 :
          {
            tmp[j++] = 0xE2;
            tmp[j++] = 0x80;
            tmp[j++] = 0x9A;
            n++;
            break;
          }
          case 131 :
          {
            tmp[j++] = 0xC6;
            tmp[j++] = 0x92;
            n++;
            break;
          }
          case 132 :
          {
            tmp[j++] = 0xE2;
            tmp[j++] = 0x80;
            tmp[j++] = 0x9E;
            n++;
            break;
          }
          case 133 :
          {
            tmp[j++] = 0xE2;
            tmp[j++] = 0x80;
            tmp[j++] = 0xA6;
            n++;
            break;
          }
          case 134 ... 135 :
          {
            tmp[j++] = 0xE2;
            tmp[j++] = 0x80;
            tmp[j++] = c + 0x1A;
            n++;
            break;
          }
          case 136 :
          {
            tmp[j++] = 0xCB;
            tmp[j++] = 0x86;
            n++;
            break;
          }
          case 137 :
          {
            tmp[j++] = 0xE2;
            tmp[j++] = 0x80;
            tmp[j++] = 0xB0;
            n++;
            break;
          }
          case 138 :
          {
            tmp[j++] = 0xC5;
            tmp[j++] = 0xA0;
            n++;
            break;
          }
          case 139 :
          {
            tmp[j++] = 0xE2;
            tmp[j++] = 0x80;
            tmp[j++] = 0xB9;
            n++;
            break;
          }
          case 140 :
          {
            tmp[j++] = 0xC5;
            tmp[j++] = 0x92;
            n++;
            break;
          }
          case 142 :
          {
            tmp[j++] = 0xC5;
            tmp[j++] = 0xBD;
            n++;
            break;
          }
          case 145 ... 146 :
          {
            tmp[j++] = 0xE2;
            tmp[j++] = 0x80;
            tmp[j++] = c + 0x7;
            n++;
            break;
          }
          case 147 ... 148 :
          {
            tmp[j++] = 0xE2;
            tmp[j++] = 0x80;
            tmp[j++] = c + 0x9;
            n++;
            break;
          }
          case 149 :
          {
            tmp[j++] = 0xE2;
            tmp[j++] = 0x80;
            tmp[j++] = 0xA2;
            n++;
            break;
          }
          case 150 ... 151 :
          {
            tmp[j++] = 0xE2;
            tmp[j++] = 0x80;
            tmp[j++] = c - 0x3;
            n++;
            break;
          }
          case 152 :
          {
            tmp[j++] = 0xCB;
            tmp[j++] = 0x9C;
            n++;
            break;
          }
          case 153 :
          {
            tmp[j++] = 0xE2;
            tmp[j++] = 0x84;
            tmp[j++] = 0xA2;
            n++;
            break;
          }
          case 154 :
          {
            tmp[j++] = 0xC5;
            tmp[j++] = 0xA1;
            n++;
            break;
          }
          case 155 :
          {
            tmp[j++] = 0xE2;
            tmp[j++] = 0x80;
            tmp[j++] = 0xBA;
            n++;
            break;
          }
          case 156 :
          {
            tmp[j++] = 0xC5;
            tmp[j++] = 0x93;
            n++;
            break;
          }
          case 158 :
          {
            tmp[j++] = 0xC5;
            tmp[j++] = 0xBE;
            n++;
            break;
          }
          case 159 :
          {
            tmp[j++] = 0xC5;
            tmp[j++] = 0xB8;
            n++;
            break;
          }
          case 160 ... 191 :
          {
            tmp[j++] = 0xC2;
            tmp[j++] = c;
            n++;
            break;
          }
          case 192 ... 255 :
          {
            tmp[j++] = 0xC3;
            tmp[j++] = c - 0x40;
            n++;
            break;
          }
          default :
          {
            tmp[j++] = '?';
            n++;
            break;
          }
        }
      }
    }
  }
  
  tmp[j] = '\0';
  strcpy( dest, tmp );
  return n;
}

void setup ()
{
  Serial.begin(9600);
  delay(1000);
}

void loop()
{
  if ( Serial.available() > 0 )
  {
    static char input[64];
    static uint8_t i;
    char c = Serial.read();

    if ( c != '\r' && i < 64-1)
      input[i++] = c;

    else
    {
      input[i] = '\0';
      i = 0;
      
      char s[256];
      sprintf( s, "In:  <%s>\n", input);
      int8_t n = SerialInputToCharSet( input, input );
      sprintf( s, "%sOut: <%s>\n%d character(s) converted!\n", s, input, n);
      Serial.print( s );
      
      if ( !strcmp( input, "€" )
        || !strcmp( input, "§" )
        || !strcmp( input, "abc" )
        || !strcmp( input, "olé" )
        || !strcmp( input, "h€lloéèàùlol" ) )
      {
        Serial.print( "strcmp: OK!\n\n" );
      }
      else
      {
        Serial.print( "strcmp: FAIL!\n\n" );
      }
    }
  }
}
617  Using Arduino / Programming Questions / Re: Data Type Conversion on: December 01, 2012, 12:44:20 am
Hello and welcome,

Try with type casting, like this:
Code:
char buffer[6];//time returns 6 char
sprintf(buffer, "%d:%d", RTC.hour, RTC.minute);

LCDA.DisplayString(0,0, (unsigned char*) buffer, 16);

It might be a better solution to modify the DisplayString function instead, or make an overloaded copy smiley
618  Using Arduino / Programming Questions / Re: HELP on: November 30, 2012, 10:46:05 pm
Hello and welcome,

That condition (once fixed by adding () smiley ) will never be true because your function doesn't return anything.

You have to do something like this:
Code:
if( FLOW() == 2 )
{
  //do something when FLOW() returns 2
}

...

int FLOW()
{
  if (digitalRead(buttonPin))
  {
    return 1;
  }

  else if (digitalRead(buttonPin2))
  {
    return 2;
  }

  return 0;
}

Also don't double post, click the Remove button near your posts in the other topic, write a clear topic title not just "HELP"... and use code tags (the '#' button) around your code smiley
619  Using Arduino / General Electronics / Re: Relay wiring help/confirmation? on: November 30, 2012, 08:14:49 am
Yes tkbyd is right smiley

Sorry if that wasn't clear, the square-round things are standard wall-sockets (of France)...the one at the top is the one in the wall, the 2 other on the right are to connect devices that will be controlled by the relays smiley
620  International / Français / Re: [Projet en cours] Enlightened Domotics Alarm Monitoring System(EDAMS) on: November 30, 2012, 12:59:40 am
C'est pas mal du tout, je n'utilise pas Linux et je n'ai aucune raison d'utiliser ce programme... mais ça à l'air bien pratique smiley

Bonne chance pour la suite smiley
621  Using Arduino / Programming Questions / Re: Logging multiple temp sensors question ( multiple values and sprintf() ) on: November 29, 2012, 10:31:51 am
But you must NOT use my loop smiley you must just follow the "shape", your code must look like this:
Code:
char buf[128] = "\0"; // IMPORTANT

for (sensor=0;sensor<MAX_DS1820_SENSORS;sensor++)
{
    // rest of your code here
    //...

    Fract = Tc_100 % 100;

    sprintf(buf, "%s%c%d.%d,", buf, SignBit ? '-' : '+', Whole, Fract < 10 ? 0 : Fract); //modified sprintf
}

sprintf(buf, "%s%s", buf, "11/12/13-12:34:56");
Serial.print(buf); // this moved outside of the loop
622  Using Arduino / Programming Questions / Re: Logging multiple temp sensors question ( multiple values and sprintf() ) on: November 29, 2012, 09:59:31 am
Hello, you can do something like this for example:

Code:
char buf[128] = "\0"; // IMPORTANT

for (uint8_t i = 0; i < 4; i++)
{
  sprintf(buf, "%s%c%d.%d,", buf,'-', 32, 9);
}

sprintf(buf, "%s%s", buf, "11/12/13-12:34:56");
Serial.println( buf );
The output:
Code:
-32.9,-32.9,-32.9,-32.9,11/12/13-12:34:56

Adapt to your code smiley
623  Using Arduino / Programming Questions / Re: Serial problem with accentized characters on: November 29, 2012, 09:35:09 am
PaulS, they are char, I changed those value to:
Code:
input[i++] = -61;
c -= 64;

It's the same result, but anyway that's not working for all characters, only the Latin ones. I may give up on this because some of the other characters actually uses more than the 2 characters per character...Here in my code there are 2 characters per character, for example letter é is coded with à and © (so the length specified in strncmp must also be increased accordingly, hopefully strlen return the correct length)


PeterH, ok that's what I thought, is there a way to change the code page used by the Arduino ??
624  International / Français / Re: data glove on: November 29, 2012, 09:05:57 am
Sinon il y a ceci je ne sais pas ce que ca vaut:

http://www.adafruit.com/products/519

J'imagine que tu pourrais en couper des petits bouts, pour les mettre entre les doigts, et aussi pour "attacher" les extrémités des doigts à la paume (près du poignet) si tu vois ce que je veux dire... Je pense que c'est un peu le même principe que la mousse dont vous discutiez...
625  Using Arduino / Programming Questions / Re: Serial problem with accentized characters on: November 29, 2012, 07:57:00 am
Ok, but I think I almost fixed it

No, not at all smiley-grin

Ok I think I got it, will do some more tests!
Code:
if ( c != '\r' && i < 15)
{
  if ( c < 0 )
  {
    input[i++] = 195;
    c += 192;
  }

  input[i++] = c;
}
626  Using Arduino / Programming Questions / Re: Serial problem with accentized characters on: November 29, 2012, 07:23:34 am
PaulS, maybe you can explain what you mean, I know a char can store -127 to + 127 or whatever that is...But if I change the data type to a byte array or int array then how do I use it with strncmp?

Anyway... I think I'm close of getting something working:
Code:
/*
for (int i = 128; i < 256; i++)
  Serial.print( (char) i );
//€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ
*/  
  
char s[] = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ";
  
for (int i = 1; i < strlen(s); i+=2)
  Serial.print( (char) (s[i]+64) ); //the magic number
Code:
ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ
627  Using Arduino / Programming Questions / Re: Ethernetclient not sending all data ? on: November 29, 2012, 05:49:36 am
Hello and welcome,

Good that you solved it yourself smiley
628  Using Arduino / Programming Questions / Re: Serial problem with accentized characters on: November 29, 2012, 05:09:32 am
Yes it's a PC and I use Windows 7 64 bits, and I was wrong about the ASCII value 130, I edited my post. Strange problem smiley-confuse


Edit: Not sure why, but if I do:
Code:
if ((byte)c == 233) c = 195;

Then the string comparison will work.

I got the value 195 by doing
Code:
char test[] = "olé";
Serial.println( test[2] );

Which printed 'Ã', that is 195 in the ASCII table, and that is 233 - 38... While substracting 38 worked for 'é', it doesn't work for other accentized characters... I will update this post later if I find more about this problem smiley
629  Using Arduino / Programming Questions / Serial problem with accentized characters on: November 29, 2012, 04:38:35 am
Hello, I have a problem with Serial and characters such as é, ô, etc. Well it's a strange problem! Look this code
Code:
void setup ()
{
  Serial.begin(9600);
}

void loop()
{
  if ( Serial.available() > 0 )
  {
    static char input[16];
    static uint8_t i;
    char c = Serial.read();

    if ( c != '\r' && i < 15)
      input[i++] = c;
    
    else
    {
      input[i] = '\0';
      i = 0;
      
      if ( !strncmp( input, "olé", 3 ) )
      {
      }
      else
      {
        Serial.print( "The strings are NOT equal...\nHowever the input looks correct: \"" );
        Serial.print( input );
        Serial.print( "\"\n\nNow testing the hardcoded strings...\n" );
      }
      
      const char test[] = "olé";
      if ( !strncmp( test, "olé", 3 ) )
      {
        Serial.println( "The strings are equal!" );
      }
    }
  }
}

Now in serial windows i type olé, here is the output:
Code:
The strings are NOT equal...
However the input looks correct: "olé"

Now testing the hardcoded strings...
The strings are equal!

And with this very small program:
Code:
void setup ()
{
  Serial.begin(9600);
}

void loop()
{
  if ( Serial.available() > 0 )
    Serial.println( Serial.read() );
}

When I type olé, the output is:
Code:
111
108
233

And according to the ASCII table, that should be 111 108 130
Err, no, 233 is correct (I think): http://www.ascii-code.com (different than what this site is showing http://www.asciitable.com smiley-confuse )

Do you know how I can fix the problem? Thanks in advance!
630  Using Arduino / Programming Questions / Re: the map() function on: November 29, 2012, 02:28:08 am
Yes just make a copy of it:

Code:
int map(int x, int x1, int x2, int y1, int y2)
{
  return (x - x1) * (y2 - y1) / (x2 - x1) + y1;
}

float map(float x, float x1, float x2, float y1, float y2)
{
  return (x - x1) * (y2 - y1) / (x2 - x1) + y1;
}

Or, maybe easier...
Code:
template <typename T> T map(T x, T x1, T x2, T y1, T y2);
template <typename T> T map(T x, T x1, T x2, T y1, T y2)
{
  return (x - x1) * (y2 - y1) / (x2 - x1) + y1;
}

I just started learning about templates and overloaded functions, they are cool smiley-grin...But typeless languages are even better!
Pages: 1 ... 40 41 [42] 43 44 ... 66