To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.rcx.pbforthOpen lugnet.robotics.rcx.pbforth in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / RCX / pbFORTH / 320
319  |  321
Subject: 
Re: Standard extension suggestions
Newsgroups: 
lugnet.robotics.rcx.pbforth
Date: 
Fri, 12 May 2000 02:41:36 GMT
Reply-To: 
paling@sympatico#Spamcake#.ca
Viewed: 
1202 times
  
I think that's a great idea. I find example code the easiest way for me
to learn. The pbForth can be little intimidating at first, (you can't
even turn off the power with the on-off button).

Earlier last fall Sergey Udovenko posted some code handle the on-off
button. It uses multitasking. It's the first thing I load after
reloading pbForth.

I hope Sergey doen't mind if I post his code again:
-------------------- START OF CODE----------------------------
\
\ From "Sergey Udovenko" <udovenko@bluewin.ch>
\
\ on-off.f
\ RCX on/off monitor
\
MARKER ON-OFF

BASE @ HEX

GET-CURRENT NONSTANDARD-WORDLIST SET-CURRENT

\ RCX utilities

: RCX-INIT-ALL ( -- )
  RCX_INIT
  POWER_INIT
  BUTTON_INIT
  SENSOR_INIT ;

: POWER-BUTTON? ( -- flag )
  RCX_POWER DUP 4000 POWER_GET @ 0= ;

: LCD-SPLASH ( -- )
  LCD_4TH LCD_REFRESH ;

: PLAY-SOUND-AND-WAIT ( sound-nr -- )
  4003 SOUND_PLAY
  BEGIN RCX_SOUND DUP SOUND_GET @ 0= UNTIL ;

\ power

: rcx_boot ( -- )
  RCX-INIT-ALL
  LCD-SPLASH
  1 PLAY-SOUND-AND-WAIT ;

' rcx_boot TO 'boot          \ init RCX every time the pbForth restarted

: rcx_power_off ( -- )
  0 PLAY-SOUND-AND-WAIT
  LCD_CLEAR LCD_REFRESH
  RCX_SHUTDOWN
  POWER_OFF
  rcx_boot ;

\ monitor task

0 32 CELLS 32 CELLS HAT rcx_on_off_task
rcx_on_off_task BUILD

:NONAME
  RCX-INIT-ALL
  LCD-SPLASH
  rcx_on_off_task ACTIVATE
  BEGIN
    POWER-BUTTON? IF rcx_power_off THEN
    PAUSE
  AGAIN ; EXECUTE


SET-CURRENT

BASE !


--------------------------END OF CODE ---------------------------


I created a task that monitors the battery voltage. It will set the
battery low indicator if the voltage goes below 8 volts, it'll shut down
the rcx if the voltage goes below 7 volts. These values are abitrary,
does anyone know how low the voltage can go before the device starts to
act up?

I was hoping I could replace the batteries before they ran out and save
the contents of the memory. Unfortunatly everytime I replace the
batteries the device resets and I have to reload forth.

Here's the program, any comments are welcome. Everyone please post some
of your own software too, even simple stuff can be enlightening.

-------------------- START OF CODE----------------------------



BATTERY_CHECK
MARKER BATTERY_CHECK


\ This task checks the battery voltage. It sets the battery
\ low indicator if below 8 volts, power off if below 7 volts.

\ In the Forth tradition I've tried to make all these routines
\ reusable.


BASE @ HEX

GET-CURRENT NONSTANDARD-WORDLIST SET-CURRENT

-1 CONSTANT set
0 CONSTANT clear
301B CONSTANT battery_low

: LCD ( n f -- ) IF LCD_SHOW ELSE LCD_HIDE THEN LCD_REFRESH ;

\ Return battery voltage in millivolts

: BatteryVolts ( -- n ) RCX_POWER DUP 4001 POWER_GET @ 1C * ;

DECIMAL

: CheckBattery ( -- )
BatteryVolts DUP
8000 > IF battery_low clear LCD  DROP
ELSE 7000 > IF battery_low set LCD
  ELSE rcx_power_off
THEN THEN ;

HEX


\ The deciseconds periodic timer allows undisturbed usage of a timer
\ for periodic tasks. The input value selects bit(s) in the timer
\ for the output flag to follow. Normally the time specified should
\ be a power of 2, to isolate a single bit i.e. [BINARY] 1, 100,
\ 1000, etc.
\
\ Example:
\ 1 deciseconds outputs the bit that alternates every 0.1 seconds,
\ [DECIMAL] 16 deciseconds reports the bit that changes every 1.6
\ seconds. The hardware timer itself is undisturbed so it and this
\ word are both resusable. They can be simultaneously used by
\ other words.

: deciseconds ( n -- f) 0 TIMER_GET AND 0= ;

\ change? returns true if the flag has changed since last time.
\ The flags last state is also returned so that it can be
\ checked next time.

: changed? ( old_flag new_flag -- newold_flag changed? )
DUP ROT = 0= ;

\ Setup the battery checker task and execute it. The decisecond timer
\ is set for [HEX] 10 deciseconds. This will enable the task to
\ check the battery every [DECIMAL] 1.6 seconds.

0 32 CELLS 32 CELLS HAT BatteryChecker
BatteryChecker BUILD

:NONAME
BatteryChecker ACTIVATE
0  \ initial flag for change detector
BEGIN
  10 deciseconds changed? IF CheckBattery
  THEN PAUSE
AGAIN ; EXECUTE


SET-CURRENT

BASE !

MARKER USER_CODE

--------------------------END OF CODE ---------------------------

Glen Paling



Message has 1 Reply:
  RE: Standard extension suggestions
 
Glen Paling Wrote: (...) Snipped code samples... Glen, These are really a good example of how the community can help itself. How does everyone feel about a code repository? How should it be implemented? Where should it live? I'm reluctant to have (...) (24 years ago, 12-May-00, to lugnet.robotics.rcx.pbforth)

Message is in Reply To:
  Standard extension suggestions
 
I suggest that we collectively create a standard extension package that makes life a little simpler for beginners or maybe for developers in general. I have already started what I would use but it occured to me that this might be of general value. (...) (24 years ago, 8-May-00, to lugnet.robotics.rcx.pbforth)

3 Messages in This Thread:

Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact

This Message and its Replies on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR