Shop OBEX P1 Docs P2 Docs Learn Events
replace a variable and DAT sections — Parallax Forums

replace a variable and DAT sections

TumblerTumbler Posts: 323
edited 2015-04-16 07:58 in Propeller 1
Hello

I want to replace a byte, word or long value into a dat section.

example:
DAT
  temp_in                             byte  "Inside temp : ---",0
  temp_out                            byte  "Outside temp: ---",0

VAR
  byte in
  byte out

PUB Main
  in := 22
  out := 16
  ' replace --- in temp_in with variable in
  ' and --- in temp_out with variable out


is there a way to do this in a simple way?

Comments

  • Heater.Heater. Posts: 21,230
    edited 2015-04-12 03:45
    Edit: deleted old answer as it was gibberish. Remind me not to post when I'm half asleep!
  • AribaAriba Posts: 2,690
    edited 2015-04-12 05:32
    I think you want to replace the "---" with a decimal representation of the value. For that you can use an object like "simple numbers", or you add a little methode like the following:
    DAT
      temp_in       byte  "Inside temp : ---",0
      temp_out      byte  "Outside temp: ---",0
    
    VAR
      byte in
      byte out
    
    PUB Main
      in := 22
      out := 16
      ' replace --- in temp_in with variable in
      decByte(@temp_in+14, in)
      ' and --- in temp_out with variable out
      decByte(@temp_out+14, out)
    
    PRI decByte(pos, value)
      byte[pos++] := value / 100 // 10 + "0"
      byte[pos++] := value / 10 // 10 + "0"
      byte[pos] := value // 10 + "0"
    
  • kwinnkwinn Posts: 8,697
    edited 2015-04-12 06:18
    @Tumbler

    You may want to take a look at JonnyMac's "jm-strings" object. There are a lot of very handy string and conversion routines in there. Binary number to ascii numeric string like you need here is only one of them.
  • JonnyMacJonnyMac Posts: 9,108
    edited 2015-04-12 06:37
    For this task I would do what Ariba shows above, though for nice displays I tend to remove the leading zeros.
    pub put_dec3(value, p_str)
    
    '' Insert 3-column decimal value into string at p_str
    '' -- suppresses leading zeros
    
      if (value => 100)
        byte[p_str][0] := value / 100 + "0"
      else
        byte[p_str][0] := " "
    
      if (value => 10)
        byte[p_str][1] := (value // 100) / 10 + "0"
      else
        byte[p_str][1] := " "
    
      byte[p_str][2] := value // 10 + "0"
    
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2015-04-12 11:36
    Another possibility is format.spin by Peter Verkaik.


    OBJ
      fmt: "Format"
      fds: "fullDuplexSerial"
    VAR
      byte buffer[128]    '  the output string with run time value will be built in this buffer
      long celsius
    DAT
      temp_in       byte  "Inside temp : %d celsius",0    ' %d specifies decimal format value to be entered at run time
    
    PUB example
       celsius := 24
       fmt.sprintf(@buffer, @temp_in, celsius)    ' build string with run time value
       fds.str(@buffer)                '  print it
    
  • TumblerTumbler Posts: 323
    edited 2015-04-12 19:14
    i have to test these when i'm back from work.
    Thanks all
  • Cluso99Cluso99 Posts: 18,069
    edited 2015-04-12 21:55
    A lot depends on what you are trying to do. If you are outputting to fullduplexserial the I would do this...
      fdx.str(string("Inside Temp: "))
      Fdx.dec(temp)
    
  • TumblerTumbler Posts: 323
    edited 2015-04-13 07:43
    Ok folks

    Have testing Ariba, JonnyMac's (without a h) code: works perfect.
    Thx Tracy for the link for the Peter's format object. (have to test this one)

    cluso: the fullduplexserial object only accepts 16 bytes for the buffer. I need more (Gsm/cellphone modem needs 264 bytes)

    At the moment i use the FullDuplexSerial4port object. But not testing for receive sms messages.
  • Cluso99Cluso99 Posts: 18,069
    edited 2015-04-13 13:02
    I modified fdx for buffer sizes up to 256 bytes. But it must be a power of 2, so 16,32,64,128,256. See ibex fullduplexserial_rr004.
    You would need a few more mods to make it larger.
  • Dave HeinDave Hein Posts: 6,347
    edited 2015-04-13 13:36
    Even though the FDX buffers are 16 bytes you can transmit or receive strings longer than 16 bytes. There would be no problem transmitting a string that is 1000 bytes long. On receive, you just need to make sure that you read the data fast enough to keep the 16-byte buffer from overflowing.
  • TumblerTumbler Posts: 323
    edited 2015-04-15 11:05
    hmmm, using rts/cts?
    i tried some objects, but all not working like i want. (incomplete or missing parts in data)
    :(
  • Cluso99Cluso99 Posts: 18,069
    edited 2015-04-15 13:26
    Do you have spare cogs? If so, then use fdx and a second cog to interface between fdx and your program. It can then provide a huge buffer(s).
  • sidecar-racersidecar-racer Posts: 82
    edited 2015-04-15 20:43
    I used a DAT pack for the .csv (excel sheet) for writing data to a SD card. My programs inserted properly formatted data into the "string" at the proper place to modify the DAT before being sent to SD. Worked well and pretty fast. Look up "sidecar" in forums; I posted the code for entire telemetry project. May work for what you want.
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2015-04-15 21:04
    char *Short2Str(n)
    short n;
    {
      short tmp, tmp2, t;
      static char str[6];
    
      tmp = 10000;
    
      for ( ; (n / tmp) ; tmp = tmp / 10);   /* C standard tells us that the n/tmp will be integer math, and thus false if under 1. remove leading 0's. */
    
      for(tmp2 = 0; tmp ; tmp =  tmp / 10)         /* convert integer to string representation. */
        str[tmp2] = (n / tmp) + 0x30;
    
      str[tmp2+1] = 0;
      return str;
    }
    
    Convert to what ever language you use, easy as can be. I chose to give the example in C to assure understandability, I did not use any short hand operators to keep it readable.
  • davidsaundersdavidsaunders Posts: 1,559
    edited 2015-04-16 04:26
    Last night I was half asleep, and left out an assignment above, corrected.
  • Dave HeinDave Hein Posts: 6,347
    edited 2015-04-16 07:58
    In C you could just do something like:
    printf("Inside temp : %3.3d", in);
    printf("Outside temp: %3.3d", out);
    
    You can do a similar thing in Spin using the CLIB object.
Sign In or Register to comment.