Shop OBEX P1 Docs P2 Docs Learn Events
Forum Request: Scrolling codebox, please — Parallax Forums

Forum Request: Scrolling codebox, please

Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
edited 2015-09-14 03:50 in General Discussion
As long as the [noparse]
[/noparse] thing is being worked on, can we have scrolling code boxes like we did before, please?

[code]CON

SINE          = $e000

OBJ

u      : "umath"

PUB distance_m(lat0, long0, lat1, long1) | dx, dy

dy := lat1 - lat0
dx := (long1 - long0) * cos(lat0 / 100_000) / 10_000
return u.multdiv(^^(dx * dx + dy * dy),  111_319, 1_000_000)

PUB distance_mm(lat0, long0, lat1, long1) | dx, dy

dy := lat1 - lat0
dx := (long1 - long0) * cos(lat0 / 100_000) / 10_000
return u.multdiv(^^(dx * dx + dy * dy),  111_319, 1_000)

PUB distance_cm(lat0, long0, lat1, long1) | dx, dy

dy := lat1 - lat0
dx := (long1 - long0) * cos(lat0 / 100_000) / 10_000
return u.multdiv(^^(dx * dx + dy * dy),  111_319, 10_000)

PUB bearing(lat0, long0, lat1, long1) | dx, dy

dy := lat1 - lat0
dx := (long1 - long0) * cos(lat0 / 100_000) / 10_000
result := 900 - atan2(dy, dx)
repeat while result < 0
result += 3600
result //= 3600

PUB degminsec2deg(degrees, minutes, second_hundredths)

'Arguments are in whole degrees, whole minutes, and whole seconds.
'Return value is in degrees * 1_000_000.

return degrees * 1_000_000 + minutes * 100_000 / 6 + second_hundredths * 100 / 36

PUB degmin2deg (degrees, minute_hundred_thousandths)

'Arguments are whole degrees, minutes * 100_000.
'Return value is in degrees * 1_000_000.

return degrees * 1_000_000 + minute_hundred_thousandths / 6

PUB cos(x)

'Argument x is in tenths of a degree.

return sin(x + 900)

PUB sin(x) : value

'Argument x is in tenths of a degree.

x := x * $2000 / 3600

if (x & $fff == $800)
value := $1_0000
elseif (x & $800)
value := word[SINE][-x & $7ff]
else
value := word[SINE][x & $7ff]
if (x & $1000)
value := -value
value := (value * 10_000) ~> 16

PUB atan2(y, x) : a | negate, i, da, dx, dy

'CORDIC atan2 algorithm
'
'  - angle range: -1800 to 1799 tenths of a degree.
'  - hypotenuse of x,y must be within ±1_300_000_000 to avoid overflow

i := 29 - >|(||x #> ||y)    'Make the arguments as large as possible.
if (i > 0)
x <<= i
y <<= i

a~
negate := x < 0             'check quadrant 2 | 3

if negate                   'if quadrant 2 | 3, (may be outside ±106° convergence range)
a += $80000000            '..add 180 degrees to angle
-x                        '..negate x
-y                        '..negate y

repeat i from 0 to 26       'do CORDIC iterations (27 is optimal for 32-bit values)
da := corlut[i]
dx := y ~> i
dy := x ~> i

negate := y < 0           'if atan2 mode, drive y towards 0

if negate
-da
-dx
-dy

a += da
x += dx
y -= dy

a := (((a ~> 16) * 3600) ~> 16) 'Convert to tenths of a degree.

PUB error_angle(angle)

result := angle
repeat while (result > 1800)
result -= 3600
repeat while (result < -1800)
result += 3600

PUB compass_angle(angle)

result := angle
repeat while result > 3600
result -= 3600
repeat while result < 0
result += 3600

DAT

corlut        long $20000000    'CORDIC angle lookup table
long $12E4051E
long $09FB385B
long $051111D4
long $028B0D43
long $0145D7E1
long $00A2F61E
long $00517C55
long $0028BE53
long $00145F2F
long $000A2F98
long $000517CC
long $00028BE6
long $000145F3
long $0000A2FA
long $0000517D
long $000028BE
long $0000145F
long $00000A30
long $00000518
long $0000028C
long $00000146
long $000000A3
long $00000051
long $00000029
long $00000014
long $0000000A

Thanks,
-Phil

Comments

  • Nicely done, Phil. I'm gong to save that template. But what is </led_data> for?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-07-11 23:32
    </led_data>? I have no idea. I thought the forum software stuck  it in there.
    So I took it out, and it still works...

    -Phil
  • jmgjmg Posts: 15,173
    edited 2015-07-12 01:03
    The better code focused forums have CODE blocks like this :
    CODE: [SELECT ALL] [EXPAND/COLLAPSE] [DOWNLOAD] 

    with a File name and a language selector for the code highlighter.

    Surprising Parallax have code support so far down their list. 
  • I've modified the HTML in the top post to accommodate those who are using Stylish. The code box width now fills the available space.
    -Phil
  • While you're tweaking the setting of the code box I wonder about making the code font size a bit smaller.
    It looks like the code text is just a bit larger than the normal forum text. It's not a big deal but I wonder if anyone else thinks the text looks kind of large?
    Thanks for posting this example. I'm adding the HTML to my bag of forum tricks (most of the other tricks I learned from Heater ("tricks" is a nicer name than some of his "formatting techniques" deserve)).

  • jmgjmg Posts: 15,173
     It's not a big deal but I wonder if anyone else thinks the text looks kind of large?



    Agreed - on 2 browsers here (Firefox,Chrome) it is larger than needs to be, and also the line-line spacing is a little larger too.
    Background colour differs slightly in the 2 browsers.
  • It's the Parallax font. I've got it set at 11 pts. Unfortunately, it doesn't scale well much below that. What I could try to do, though, is tighten the line spacing. Would that help?
    -Phil
  • jmgjmg Posts: 15,173
    It's the Parallax font. I've got it set at 11 pts. Unfortunately, it doesn't scale well much below that. What I could try to do, though, is tighten the line spacing. Would that help ?



    It renders as a 'somewhat large' 11pts - less line spacing is probably a good idea, - put in some of the tallest chars in a comment and try it ?

    Can you do another window, (maybe in a post below this one) with simple courier font to see how that scales ?  Prop font support is nice to have, but not all code needs it.
  • I think it looks pretty good as it is but the line spacing is greater than the line spacing in the Propeller Tool.Of course in the Propeller Tool there isn't any space between rows of text so schematics can be built from the special characters. The parenthesis has one row of unused pixels across the top and across the bottom of its bitmap so there would only be two pixels between two rows of texts with a parenthesis character. The underscore uses the lowest row of pixels so there would only be one pixel between an underscore character and the top of a parenthesis in the next line down.
    I'm not sure if it's a good idea to try to replicate the look of the Prop Tool or not but it would be nice if schematics in the code would show up in the forum.
    Again, it looks good as it is so it's not a big deal IMO if you don't change it at all.
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2015-07-13 02:30
    Nice work Phil. I tried it on the iPhone6 and it looks fine in whatever they call the native monospace font. It scrolls okay in either portrait or landscape mode.

    Here trying your template. Want to see how it comes out with some of the Parallax schematic drawing characters.

    [code]
    '' EXAMPLE 1.?? : Light meter, LED both measure light and indicates brightness
    ''
    '' DIFFICULTY LEVEL: Very easy
    ''
    '' Submitted by Tracy Allen, 1 March 2006
    '' File: Example01_lightMeter1.spin
    ''
    ''PURPOSE:
    '' -- This program shows how an LED can be used to measure light by using in in reverse bias
    '' mode as a photodiode. The current produced by light discharges a capacitor. and a
    '' propeller input pin detects when the voltage level passes through the ~1.5 volt input
    '' threshold. The time required to discharge the capacitor changes with light level,
    '' and that changes the rate at which the program repeats its main loop.
    '' Each time through the loop, the program turns the LED to its normal use and has it emit a
    '' brief flash of light. This indicates how fast the program loop is running, and
    '' therefore the light level.
    '' -- This example illustrates the electronics of the LED as both an input and an output device,
    '' and how to wire up a simple circuit on the prototyping block.
    '' -- This program shows how to set a Propeller pin for input and how to make it wait
    '' while the input is high, until the capacitor discharges, and the voltage at the
    '' hits the propeller input threshold of ~1.5 volts, and the input suddenly reads as
    '' zero. The program shows how to do that using a coded repeat loop with the "until"
    '' modifier, and alternatively how to use the unique propeller commands waitpeq and waitpne
    '' to accomplish the same thing. The program uses only one COG.
    '' -- Parts required, one green LED, one 330 ohm resistor, one 220pf capacitor, one jumper wire.
    '' These parts must be mounted on the white prototyping block with connection to the
    '' Propeller pins.
    '' The LED is connected between two input pins, with the anode to pin LEDp=1 and the cathode
    '' to pin LEDn=0. It uses two pins so it can use the LED in normal forward mode to blink it,
    '' and reverse bias as a photodiode. Note that the longer leg of an LED is usually the anode.
    ''
    '' -- The following is the circuit diagram rendered in ascii...
    ''
    '' 330 green LED
    '' a1?
    //---o--->|
    , a1 (+)
    >|
    (-) a0
    '' | p n |
    > forward bias
    '' | | lights LED
    '' === 220pf |
    '' | |
    '' a0?
    o
    ' a1 (-)
    >|
    (+) a0
    ''
  • Tracy AllenTracy Allen Posts: 6,664
    edited 2015-08-06 21:09
    For some reason that cut off before it got to the version in the Parallax font.  Did you do something special to add a longer listing?  Here is the continuation...
    ''
    '' -- The following are the same diagrams, rendered in the sweet new Parallax Spin font...
    ''    Find the character chart on the HELP menu.
    ''
    ''        330?  green Led
    ''   a1 ??????????????????                        a1 (+)  ?????????????  (-) a0
    ''             ?         ?                                  I ?????
    ''             ???????????
    ''             ?  220pf  ?
    ''   a0 ??????????????????                         a1 (-) ?????????????   (+) a0
    ''                                                           ??????? I
    ''
    ''*************************************************************************************************
    '' CORRECT OUTPUT: The LED mounted on the prototyping block will blink at a rate that depends on
    '' ambient light level.  Note: if you actually touch the led or even if you almost touch it,
    '' even in the dark, it will start to blink very fast. That is due to 60hz noise coupling from the
    '' AC power lines.
    ''*************************************************************************************************
    
    
    
    CON
    ledn = 0       ' LED cathode (negative, n-type semiconductor) attached to this pin
    ledp = 1       ' LED anode (positive, p-type semiconductor) attached to this pin
    
    'This program leaves the clock at the default ~12 mHz RC clock source.
    
    
    PUB light_meter
    repeat        ' repeat the following two routines forever.
    blipLED
    readLED
    
    
    PRI blipLED                ' briefly flash the LED, forward bias
    outa[ledp] := 1          ' LEDp is a high output
    dira[ledp] := 1
    outa[ledn] := 0          ' LEDn is a low output
    dira[ledn] := 1
    waitcnt(240000 + cnt)    ' wait 24000 counts = about 2 milliseconds with 12 mhz clocking
    
    PRI readLED
    outa[ledp] := 0                       ' reverse bias the LED and charge the 100 pf capacitor
    dira[ledp] := 1               ' both pins are outputs, opposite polarity
    outa[ledn] := 1
    dira[ledn] := 1
    waitcnt(2400 + cnt)           ' allow time for the capacitor to charge, about 200 microseconds
    dira[ledn] := 0               ' make pin LEDn an input, no longer forced high,
    ' capacitor starts to discharge through LED.
    repeat until ina[ledn] == 0   ' pin LEDn reads high to start, but after time will drop to low
    ' the program loops here until the pin reading drops to low.
    
    '' -- Other things to try & observe...
    '' -- alternative, comment out the whole repeat line just above and comment in the following:
    ''   waitpeq(0, |< ledn, 0)
    ''   this single command means "wait until pin equals".  The first parameter selects the state
    ''   to wait for (=zero), and the second paramter is a mask for "which pin".   The syntax,
    ''    |< ledn
    ''    means, "create a binary pattern that has only the ledn'th bit set to one, all others zero.
    '' -- A second alternative is "wait for pin not equal":
    ''   waitpne( |< ledn, |< ledn, 0)
    ''   Here it is waiting for the state to drop from its starting value of 1
    ''   (in the correct bit position, ledn).
    '' -- Experiment with different capacitors and different LEDs.
    ''    Maybe green LED is not the best?  Maybe you need to measure higher or lower light levels.
    ''    Remember that light our eyes can respond to varies over 9 orders of magnitude,
    ''    from 100000 lux in bright sunlight to less than 0.001 lux in quarter moonlight.
    ''    The illumination level in a typical room is 10-1000 lux
    '' -- Note that the program stops working if you change the time from 2400 to 240 in the waitcnt instruction
    ''   That is not because the capacitor wouldn't have time
    ''   to charge.  The time constant is 330 * 100 = 33 nanoseconds, so 20 microseconds should
    ''   be plenty.  It must be delays inherent in the Spin interpreter.  With 240 there,
    ''   the system counter moves faster than the interpreter's capability to check it.
    ''   Keep time delays in mind as you program in Spin.
    
    Okay, it worked! But no surprise, it doesn't render on the iPhone.




  • jmgjmg Posts: 15,173
    edited 2015-07-13 02:50
    Okay, it worked! But no surprise, it doesn't render on the iPhone.
    I do prefer simple ASCII Schematics. No questions of 'how portable'.
    Even the ASCII version above, seems to have a couple of Non ASCII chars after a0.a1?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2015-08-06 21:08
    Here's a more condensed line spacing, with Tracy's schematic:

    ''        330?  green Led
    ''   a1 ??????????????????                        a1 (+)  ?????????????  (-) a0
    ''             ?         ?                                  I ?????
    ''             ???????????
    ''             ?  220pf  ?
    ''   a0 ??????????????????                         a1 (-) ?????????????   (+) a0
    ''                                                           ??????? I
    ''
    

    This is what I'm using now for the <pre> tag:

    <pre style="background-color:#ffffe0;max-height:400px;max-width:fill-available;overflow:auto;font-family:Parallax,Courier,monospaced;font-size:11pt;line-height:11pt;border-style:solid;border-width:1px;padding:5px">

    I changed the line-height style here and in the first post to 11pt to match the font-size.

    Tracy, I didn't do anything special to get a complete listing. It's odd that yours got cut off.

    -Phil
  • jmgjmg Posts: 15,173

    I changed the line-height style here and in the first post to 11pt to match the font-size.



    Looks here like that is an improvement. 
  • Heater.Heater. Posts: 21,230
    Duane,
    "most of the other tricks I learned from Heater ("tricks" is a nicer name than some of his "formatting techniques" deserve)"
    But Duane, I don't have any "tricks". Nothing  underhand or up my sleeve. Just straight, vanilla (get it?) HTML. The same HTML we are invited to  use by being given a WYSIWYG HTML text editor box to post in to.
    By the way there is an excellent user manual for using this input box here: http://www.w3schools.com

  • BBCode is going to be reimplemented.

    Code boxes should again be scrollable once it is in place.
  • BBCode is going to be reimplemented.

    Code boxes should again be scrollable once it is in place.


    Thanks!
  • Apparently, this thread was sunk. See this thread.
  • In hopes of making it easier for others to use code tags, I'm adding a really short example here. I copied Phil's top post and deleted the code between the tags. To use these tags, first quotes this post. Once you have this text in the editor box you need to click on the "Show Source" icon "<>". While in this html viewer, copy the text between the lines of asterisks. You should then be able to paste the html into another editor box which is also in html mode.*****************************************************************************[code]
  • [code]
  • [code]
  • PublisonPublison Posts: 12,366
    edited 2015-07-25 14:09
    In hopes of making it easier for others to use code tags, I'm adding a really short example here. I copied Phil's top post and deleted the code between the tags. To use these tags, first quotes this post. Once you have this text in the editor box you need to click on the "Show Source" icon "<>". While in this html viewer, copy the text between the lines of asterisks. You should then be able to paste the html into another editor box which is also in html mode.*****************************************************************************[code]{{
    *****************************************
    * Servo32v9 Driver v9 *
    * Author: Beau Schwabe *
    * Copyright (c) 2013 Parallax *
    * See end of file for terms of use. *
    *****************************************


    *****************************************************************
    Control up to 32-Servos Version 9 04-05-2013
    *****************************************************************
    Coded by Beau Schwabe (Parallax).
    *****************************************************************

    History:
    Version 1 - initial concept

    Version 2 - (03-08-2006) Beta release

    Version 3 - (11-04-2007) Improved servo resolution to 1uS

    Version 4 - (05-03-2009) Ability to disable a servo channel
    and remove channel preset requirement

    Version 5 - (05-08-2009) Added ramping ability

    Version 6 - (07-18-2009) Fixed slight timing skew in ZoneLoop and
    fixed overhead timing latency in ZoneCore

    Version 7 - (08-18-2009) Fixed servo jitter in ramping function when
    servo reached it's target position

    Version 8 - (12-20-2010) Added PUB method to retrieve servo position

    Version 9 - (04-05-2013) cnt rollover issue corrected with ZonePeriod
    by setting up Counter A and using the Phase
    accumulator instead of the cnt

    Note: This also eliminates the need to setup
    a 'NoGlitch' variable


    Theory of Operation:

    Each servo requires a pulse that varies from 1mS to 2mS with a period of 20mS.
    To prevent a current surge nightmare, I have broken the 20mS period into four
    groups or Zones of 8 servos each with a period of 5mS. What this does, is to
    ensure that at any given moment in time, a maximum of only 8 servos are receiving
    a pulse.

    Zone1 Zone2 Zone3 Zone4 In Zone1, servo pins 0- 7 are active
    ???????????????????????????? In Zone2, servo pins 8-15 are active
    ???5mS????5mS????5mS????5mS?? In Zone3, servo pins 16-23 are active
    ????????????20mS????????????? In Zone4, servo pins 24-31 are active
    ?? ?? ?? ??
    ?? 1-2mS servo pulse ??

    The preferred circuit of choice is to place a 4.7K resistor on each signal input
    to the servo. If long leads are used, place a 1000uF cap at the servo power
    connector. Servo's seem to be happy with a 5V supply receiving a 3.3V signal.

    }}

    CON
    _1uS = 1_000_000 / 1 'Divisor for 1 uS

    ZonePeriod = 5_000 '5mS (1/4th of typical servo period of 20mS)

    LowRange = 500 ' Width Pin Width Pin
Sign In or Register to comment.