+ Reply to Thread
Results 1 to 20 of 20

Thread: How to post program code in the forum.

  1. #1

    Default How to post program code in the forum.

    Program code is unique, in that indentation and spacing are often important, especially with Spin, and it always looks better when rendered in a monospaced font. Fortunately, posting code so that it looks right and can easily be read by others is very easy. For example, here's some Spin/PASM code that was just pasted into the message edit box:

    PUB stop

    '' Stop the overlay object.

    if gray_cog
    cogstop(gray_cog - 1)
    gray_cog~

    DAT

    org 0
    entry mov block_cnt,#0
    movd :wr_code,#disp_line
    mov nsyncs,width
    shl nsyncs,#4
    add nsyncs,#3


    Notice that none of the leading spaces gets displayed, and even interior spaces get compressed. This not only makes the code hard to read but, if copied and pasted, it wouldn't even compile.

    Here's what the same code looks like when properly displayed in a code box:

    Code:
    PUB stop
    
    '' Stop the overlay object.
    
      if gray_cog
        cogstop(gray_cog - 1)
        gray_cog~
    
    DAT
    
                  org       0
    entry         mov       block_cnt,#0          
                  movd      :wr_code,#disp_line    
                  mov       nsyncs,width            
                  shl       nsyncs,#4
                  add       nsyncs,#3
    See how much easier it is to read and how nicely everything lines up? It's not hard to make this happen. When creating or editing your post, just enclose your code between [code] and [/code] tags, like this:




    IMPORTANT NOTE: Do not use the WYSIWYG Editor when entering or editing posts with code. Use the Basic Editor instead. The WYSIWYG Editor messes with numeric data following a percent (%) sign and enters the code incorrectly.
    Last edited by Phil Pilgrim (PhiPi); 03-13-2012 at 06:17 PM. Reason: Added WYSIWYG warning.

  2. #2

    Default ... and how to help others learn to post code.

    If you see a forumista who needs help posting code, copy and paste the following as a reply to his post:

    [url=http://forums.parallax.com/showthread.php?129690&p=978076&viewfull=1#post9780 76][img]http://forums.parallax.com/attachment.php?attachmentid=78421&d=1297987572[/img][/url]

    What will get displayed is this:



    -Phil
    Last edited by Phil Pilgrim (PhiPi); 02-18-2011 at 12:47 AM.

  3. #3

    Default Re: ... and how to help others learn to post code.

    ...tsk...tsk - a blatant hack of an Industry Icon!

    You, Sir, should be ashamed of yourself!



    DJ

    BTW - thanks for the info.

  4. #4

    Default Re: ... and how to help others learn to post code.


  5. #5

    Default Re: ... and how to help others learn to post code.




    Good one!

    DJ

  6. #6

    Thumbs up Re: How to post program code in the forum.

    Hey, this thing is great!

  7. #7

    Default Re: How to post program code in the forum.

    Incredibly useful post!
    It has been made sticky.

    J

  8. #8

    Default Re: How to post program code in the forum.

    Thanks, Jen!

    -Phil

  9. #9

    Default Re: How to post program code in the forum.

    'bout time!

    That's what I like about these forums. Stuff that just helps!

    Thanks for doing that Jen, (couldn't Phil have done that, being a Moderator, or does that take a Staff or Admin to do that?)
    Infernal Machine

  10. #10

    Default Re: How to post program code in the forum.

    Quote Originally Posted by Publison
    ...couldn't Phil have done that, being a Moderator, or does that take a Staff or Admin to do that?
    I'm sure it's restricted to Admins, as it should be. Even if I could have, though, stickifying one's own thread unilaterally would be considered bad form. BTW, I'm not a Moderator. Browser (my cat) has the mod status in this household -- and not just in the forum.

    -Phil

  11. #11

    Default Re: How to post program code in the forum.

    I can remember the picture of Browser high atop the Phil 1980-90's magazine archives in the Priority box. I guess Browser can read, Priority means Authority .

    Check the shrimp status.

    Quote Originally Posted by Phil Pilgrim (PhiPi) View Post
    I'm sure it's restricted to Admins, as it should be. Even if I could have, though, stickifying one's own thread unilaterally would be considered bad form. BTW, I'm not a Moderator. Browser (my cat) has the mod status in this household -- and not just in the forum.

    -Phil
    Infernal Machine

  12. #12

    Default Re: How to post program code in the forum.

    I got an error message about some token but I resubmitted it anyway.It didn'nt copy as it was written.
    [' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' COM Parameters
    COM_RX PIN 0 ' rx pin
    COM_TX PIN 2 ' tx pin
    COM_SPEED CON 84 ' baud 9600
    COM_10MS CON 10 ' 10ms unit
    ' Protocol Command
    CMD_BREAK CON "b" ' abort recog or ping
    CMD_SLEEP CON "s" ' go to power down
    CMD_KNOB CON "k" ' set si knob <1>
    CMD_LEVEL CON "v" ' set sd level <1>
    CMD_LANGUAGE CON "l" ' set si language <1>
    CMD_TIMEOUT CON "o" ' set timeout <1>
    CMD_RECOG_SI CON "i" ' do si recog from ws <1>
    CMD_RECOG_SD CON "d" ' do sd recog at group <1> (0 = trigger mixed si/sd)
    ' Protocol Status
    STS_AWAKEN CON "w" ' back from power down mode
    STS_ERROR CON "e" ' signal error code <1-2>
    STS_INVALID CON "v" ' invalid command or argument
    STS_TIMEOUT CON "t" ' timeout expired
    STS_INTERR CON "i" ' back from aborted recognition (see 'break')
    STS_SUCCESS CON "o" ' no errors status
    STS_RESULT CON "r" ' recognised sd command <1> - training similar to sd <1>
    STS_SIMILAR CON "s" ' recognised si <1> (in mixed si/sd) - training similar to si <1>
    ' Protocol arguments are in the range 0x40 (-1) TO 0x60 (+31) inclusive
    ARG_MIN CON 64 ' 0x40
    ARG_MAX CON 96 ' 0x60
    ARG_ZERO CON 65 ' 0x41
    ARG_ACK CON 32 ' 0x20 'TO READ more status arguments
    'Groups and Commands
    GROUP_1 CON 1 '(Command count: 1)
    G1_UNNAMED_COMMAND_0 CON 0
    RES_ERROR CON 255
    RES_TIMEOUT CON 254
    RES_COMMFAIL CON 253
    RES_BUILTIN CON 32

    'Robot Constant
    VRLED PIN 4
    'Global Variable
    VRA VAR Byte
    VRA1 VAR Byte
    VRGROUP VAR Byte
    VRCOMMAND VAR Byte
    ' Main Start
    INPUT COM_RX
    HIGH COM_TX
    Restart:
    LOW VRLED
    VRGROUP = 0
    DEBUG CR, "Setting up VRbot... "
    'Wake up or stop recognition
    GOSUB VR_Wakeup
    DEBUG "awake... "
    'Set SI Language
    VRA1 = 0
    GOSUB VR_SetLanguage
    DEBUG "language ", DEC VRA1, "... "
    'Set 5 seconds timeout
    VRA1 = 5
    GOSUB VR_SetTimeout
    DEBUG "timeout ", DEC VRA1, "... "
    DEBUG CR, "VRbot ready!"
    VR_Loop:
    DEBUG CR, "VRbot in group ", DEC VRGROUP, " waiting for command... "
    LOW VRLED
    PAUSE 150
    IF VRGROUP > 0 THEN HIGH VRLED
    VRA1 = VRGROUP
    GOSUB VR_RecognizeSD
    '-- handle errors or timeout
    IF VRA1 = RES_ERROR THEN
    DEBUG "error"
    'try again in the same group
    GOTO VR_Loop
    ENDIF
    IF VRA1 = RES_TIMEOUT THEN
    DEBUG "timed out"
    VRGROUP = 0 ' back to trigger
    GOTO VR_Loop
    ENDIF
    IF VRA1 = RES_COMMFAIL THEN
    DEBUG "comm failed"
    'resync and try again
    GOSUB VR_Wakeup
    GOTO VR_Loop
    ENDIF
    '-- got a command
    VRCOMMAND = VRA1
    IF VRCOMMAND <= RES_BUILTIN THEN GOSUB VR_Action
    GOTO VR_Loop
    VR_Action:
    DEBUG "got ", DEC VRCOMMAND
    SELECT VRGROUP
    CASE GROUP_1
    SELECT VRCOMMAND
    CASE G1_UNNAMED_COMMAND_0
    PAUSE 0 '-- write your code here
    ENDSELECT
    ENDSELECT
    RETURN
    '=== VR Routines ===
    ' Wake up:
    VR_Wakeup:
    TOGGLE VRLED
    VRA = CMD_BREAK
    SEROUT COM_TX, COM_SPEED, [VRA]
    SERIN COM_RX, COM_SPEED, 100*COM_10MS, VR_Wakeup, [VRA]
    IF (VRA <> STS_SUCCESS) THEN GOTO VR_Wakeup
    LOW VRLED
    RETURN
    ' Inputs:
    ' VRA1 = language index (0 = english, ...)
    VR_SetLanguage:
    VRA = CMD_LANGUAGE
    SEROUT COM_TX, COM_SPEED, [VRA]
    VRA1 = VRA1 + ARG_ZERO
    SEROUT COM_TX, COM_SPEED, [VRA1]
    SERIN COM_RX, COM_SPEED, 20*COM_10MS, VR_CommFailed, [VRA]
    VRA1 = VRA1 - ARG_ZERO
    'IF (VRA <> STS_SUCCESS) THEN GOTO VR_Wakeup
    RETURN
    ' Inputs:
    ' VRA1 = timeout (in ms, 0=forever, 255=default)
    VR_SetTimeout:
    VRA = CMD_TIMEOUT
    SEROUT COM_TX, COM_SPEED, [VRA]
    VRA1 = VRA1 + ARG_ZERO
    SEROUT COM_TX, COM_SPEED, [VRA1]
    SERIN COM_RX, COM_SPEED, 20*COM_10MS, VR_CommFailed, [VRA]
    VRA1 = VRA1 - ARG_ZERO
    'IF (VRA <> STS_SUCCESS) THEN GOTO VR_Wakeup
    RETURN
    ' Inputs:
    ' VRA1 = SI knob (0=loosest, 2=normal, 4=tightest)
    VR_SetKnob:
    VRA = CMD_KNOB
    SEROUT COM_TX, COM_SPEED, [VRA]
    VRA1 = VRA1 + ARG_ZERO
    SEROUT COM_TX, COM_SPEED, [VRA1]
    SERIN COM_RX, COM_SPEED, 20*COM_10MS, VR_CommFailed, [VRA]
    VRA1 = VRA1 - ARG_ZERO
    'IF (VRA <> STS_SUCCESS) THEN GOTO VR_Wakeup
    RETURN
    ' Inputs:
    ' VRA1 = SD level (1=easy, 2=default, 5=hard)
    VR_SetLevel:
    VRA = CMD_LEVEL
    SEROUT COM_TX, COM_SPEED, [VRA]
    VRA1 = VRA1 + ARG_ZERO
    SEROUT COM_TX, COM_SPEED, [VRA1]
    SERIN COM_RX, COM_SPEED, 20*COM_10MS, VR_CommFailed, [VRA]
    VRA1 = VRA1 - ARG_ZERO
    'IF (VRA <> STS_SUCCESS) THEN GOTO VR_Wakeup
    RETURN
    ' Inputs:
    ' VRA1 = wordset (0=trigger)
    ' Ouputs:
    ' VRA1 = result (0-31=word, 32..=builtin, 253=comm err, 254=timeout, 255=error)
    VR_RecognizeSI:
    VRA = CMD_RECOG_SI
    GOTO VR_Recognize0
    VR_RecognizeSD:
    VRA = CMD_RECOG_SD
    VR_Recognize0:
    SEROUT COM_TX, COM_SPEED, [VRA]
    ' send Group/WS
    VRA1 = VRA1 + ARG_ZERO
    SEROUT COM_TX, COM_SPEED, [VRA1]
    ' wait for answer
    SERIN COM_RX, COM_SPEED, 600*COM_10MS, VR_CommFailed, [VRA]
    IF VRA = STS_RESULT THEN
    ' send ack
    VRA = ARG_ACK
    SEROUT COM_TX, COM_SPEED, [VRA]
    ' wait for recognised command code
    SERIN COM_RX, COM_SPEED, 20*COM_10MS, VR_CommFailed, [VRA1]
    VRA1 = VRA1 - ARG_ZERO
    ELSEIF VRA = STS_SIMILAR THEN
    ' send ack
    VRA = ARG_ACK
    SEROUT COM_TX, COM_SPEED, [VRA]
    ' wait for recognised command code
    SERIN COM_RX, COM_SPEED, 20*COM_10MS, VR_CommFailed, [VRA1]
    VRA1 = VRA1 - ARG_ZERO + RES_BUILTIN
    ELSEIF VRA = STS_TIMEOUT THEN
    VRA1 = RES_TIMEOUT
    ELSE
    VRA1 = RES_ERROR
    ENDIF
    RETURN
    VR_CommFailed:
    VRA1 = RES_COMMFAIL
    RETURN ]

  13. #13

    Default Re: How to post program code in the forum.

    Paul,

    You want to use code tags. The brackets are part of the tags. Don't replace the word "code" with your program. The word "code" is part of the tag. Here's what you'd enter: (I used a zero instead of an "o" so the word "code" will show up here.)

    [C0DE]' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' COM Parameters
    COM_RX PIN 0 ' rx pin
    COM_TX PIN 2 ' tx pin
    COM_SPEED CON 84 ' baud 9600
    COM_10MS CON 10 ' 10ms unit

    '<more code here>


    SERIN COM_RX, COM_SPEED, 20*COM_10MS, VR_CommFailed, [VRA1]
    VRA1 = VRA1 - ARG_ZERO + RES_BUILTIN
    ELSEIF VRA = STS_TIMEOUT THEN
    VRA1 = RES_TIMEOUT
    ELSE
    VRA1 = RES_ERROR
    ENDIF
    RETURN
    VR_CommFailed:
    VRA1 = RES_COMMFAIL
    RETURN[/C0DE]

    This will show up as:

    Code:
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    ' COM Parameters
    COM_RX PIN 0 ' rx pin
    COM_TX PIN 2 ' tx pin
    COM_SPEED CON 84 ' baud 9600
    COM_10MS CON 10 ' 10ms unit
    
    '<more code here>
    
    
    SERIN COM_RX, COM_SPEED, 20*COM_10MS, VR_CommFailed, [VRA1]
    VRA1 = VRA1 - ARG_ZERO + RES_BUILTIN
    ELSEIF VRA = STS_TIMEOUT THEN
    VRA1 = RES_TIMEOUT
    ELSE
    VRA1 = RES_ERROR
    ENDIF
    RETURN
    VR_CommFailed:
    VRA1 = RES_COMMFAIL
    RETURN

  14. #14

    Default Re: How to post program code in the forum.

    I don't know if it was intentional, but, when you use simple code tags for C++ or C code it just shows in one color, but when you put it between PHP tags even though it is not php code it displays multi color coded which IMO easier to read.

    Code:
    // ---------------------------------------------------------------------------
    // The loop routine runs over and over again forever
    // ---------------------------------------------------------------------------
    
    void loop() {
    
      for (int i=0; i <= 9; i++) {
      turnLEDon(i);
     }
    
      for (int i=9; i >= 0; i--) {
      turnLEDon(i);
      }
      
    }
    vs

    PHP Code:
    // ---------------------------------------------------------------------------
    // The loop routine runs over and over again forever
    // ---------------------------------------------------------------------------

    void loop() {

      for (
    int i=0<= 9i++) {
      
    turnLEDon(i);
     }

      for (
    int i=9>= 0i--) {
      
    turnLEDon(i);
      }
      


  15. #15

    Default Re: How to post program code in the forum.

    That's a neat trick, mrma! Thanks for sharing it with us!

    -Phil

  16. #16

    Default Re: How to post program code in the forum.

    That is neat! Teach an old dog new tricks.

    Me, not you Phil.
    Infernal Machine

  17. #17

    Default Re: How to post program code in the forum.

    LOL

    Now why in the world do you suppose they would highlight that kind of syntax? ;-)

    It is always very helpful to indent code for readability whether the language requires it or not.

  18. #18

    Default Re: How to post program code in the forum.

    Quote Originally Posted by jazzed
    It is always very helpful to indent code for readability whether the language requires it or not.
    That's what I kept telling my students. They finally paid attention when I told them I wouldn't help them with compile errors unless their programs were indented neatly.

    -Phil

  19. #19

    Default Re: ... and how to help others learn to post code.


  20. #20
    Parallax Engineering

    Chris Savage's Avatar
    Location
    Rocklin, CA
    Posts
    12,517
    Blog Entries
    8

    Default Re: ... and how to help others learn to post code.

    Paul,

    The reply was for those of us trying to help you with the code tags and how to use them. You followed the instructions in the second post, which weren't about your issue. Please read post #1 in this thread and the edit your post in your original thread with the code tags to post your code. Optionally you can attach it.
    Chris Savage
    Engineering Tech, Parallax Inc.
    (916) 624-8333 x3005
    Website / Twitter / Google+

+ Reply to Thread

Similar Threads

  1. forum post timestamps
    By David B in forum Support for Using this Forum
    Replies: 1
    Last Post: 08-17-2010, 07:50 PM
  2. Which forum to post in?
    By ns7r in forum Support for Using this Forum
    Replies: 2
    Last Post: 11-19-2009, 06:38 AM
  3. the post on the announcement forum has a bad url
    By FireHopper in forum General Discussion
    Replies: 1
    Last Post: 02-03-2009, 02:25 AM
  4. Propeller Forum post just topped the Stamp forum
    By parts-man73 in forum Propeller 1 Multicore Microcontroller
    Replies: 7
    Last Post: 10-12-2008, 10:15 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts