Part 4 Software

BS2 code and Beacon Code

July 06 2010
This phase of the project was divided up into two parts.

1. Modifying the stepper motor assembler code in the beacon to slow it down  and

2. Writing the BS2 code for the controller.

 

Lets look at the Beacon first.

 

Modifying the beacon to slow it down required the proper can opener!

By carefully cutting in the correct spot, we were able to save most of the threads thus allowing us to just screw the top back on after modifications were made.

I'm not going to show the innards of the beacon but I will describe what I did to access the microcontroller.

Basically there was a programming port on the main PCB that I soldered a RJ45 cable to. The cable then plugged into the Microchip programmer shown below.

Now that I had access to the microcontroller, I could start making my own code to control the stepper motor chip in the beacon. I downloaded the datasheet for the motor driver and then started to make a spreadsheet to assist in writing the code for the microcontroller. Here is my table:

 

 

 

 

 

 

Code Snippets!

 

 
 

 

 
 

 

 

Here is my kitchen programming station. An RS232

cable runs from the box to the laptop.

 

Programming the Ipod with the play list!

 
 

 

Debug code window showing the BS2 CPU in action.

 
 

 

Back to main index

Or you can view the final code below.

Gateway_7p2.bs2 (in BS2 file form)

Note: While Mike L. drove us down to the Jamboree, I was able to spend about 9 hours writing new code and tweaking existing code to get the job done.
I was able to instantly download the SW to the stamp and test it too!

 

' {$STAMP BS2}
' {$PORT COM1}

' Program: Gateway_1.BS2
' Based on the BS2 Data Collection Proto Board demo software.
'
' This program demonstrates use of the BS2 Data Collection Proto Board
' in a typical data-logging application. It exercises all of the
' board's built-in peripheral devices, including two 8kB (or 16kB)
' EEPROMs, real-time clock calendar, 12-bit ADC, and switchable
' precision power supply.
' [[ NOTE: lines that were too long were split with the ~ symbol used
' to indicate continuation on the next line.]]
'
'
'=====================================================================
' PIN ASSIGNMENTS, SYSTEM CONSTANTS, TEMPORARY VARIABLES
'=====================================================================
' The BS2's upper port, pins 8 through 15, is dedicated to the
' operation of the data-collection peripherals. The lower port,
' pins 0 through 7, is available for your application.
'
CLK CON 15 ' Clock line for all serial peripherals.
DATA_ CON 14 ' Data line for all serial peripherals.
REMOTE_KEY_D CON 13 ' Wireless remote Key D state
'EE_CS0 CON 12 ' Chip-select line for EEPROM 0 (U2).
NJU_CE CON 11 ' Chip-enable for NJU6355 clock/calendar.
NJU_IO CON 10 ' IO (read/write) for NJU6355; 1=write.
Shtdwn CON 9 ' Shutdown for peripheral power supply; 1=off.
'ADC_CS CON 8 ' Chip-select line for LTC1298(88) ADC.
PIR_TRIGGER CON 7 'PIR Trigger sensor
GATELIGHT_ON CON 6 'Turns on the light inside the walkway
AMP_ON CON 5 'Turns on the Ipod and the Power Amp
PLAY_PAUSE CON 4 'Play Pause button on Ipod
RIGHT CON 3 'Right button on Ipod
LEFT CON 2 'Left button on Ipod
VOL_UP CON 1 'Volume up button on Ipod
VOL_DOWN CON 0 'Volume down button on Ipod
LAST_TRACK CON 39 'The total number of tracks on the IPOD.
'
'Other constants used for the Gateway
IPOD_INIT_TIME CON 5000 'This is the time we allow for the Ipod to init.
IPOD_KEY_PAUSE CON 100 'The time in mSeconds the Ipod key must be held down to register reliably.
'10 just did not work
'50 was iffy
' Need to keep quiet time. Set Quiet time hours below ==============================================================
LIGHTS_OUT CON 2202
LIGHTS_ON CON 0658
MASTER_VOLUME_CLOCK CON 35 'Set to 20 for testing..probably more like 30 for the actual gateway installation.
MASTER_VOLUME_SONGS CON 25
'===================================================================================================================
'
'Other constants used by the original software.
b96 CON $54 ' Baudmode for 9600 bps.
'timeout CON 60000 ' At startup, wait 60 seconds for serial command.
timeout CON 2000 ' At startup, wait 60 seconds for serial command.
'
'Global Variables
'
LOOP VAR Byte
VOLUME VAR Byte 'Ipod volume
VOLUME_PREVIOUS 'Save the previous volume level to try and speed things up.
TRACK VAR Byte 'IPOD track to play
TRACK_PLAYED_LAST VAR Byte 'IPOD track to play second
PLAY_TIME VAR Word 'Time in seconds that the IPOD will play a track.
TURN_OFF_IPOD_AFTER_TRACK VAR Bit 'Set to 1 to turn off the ipod power after trackis played.
'Reset this to 0 is loop to minimize the number of times we set it.
D_key_DC_state VAR Bit ' Keeps track of the D key DC state. If D Key gets left on by accident then the new state becomed 1

TURN_OFF_IPOD_AFTER_TRACK =0
IPOD_KEEP_ALIVE VAR Bit
IPOD_KEEP_ALIVE=0
D_key_DC_state=0

PIR_delay VAR Nib 'Variable used to keep the PIR on for a minimum time.
PIR_delay =0



pwrOn CON $31 ' Initial state of OUTH at peripheral power-up.
' Turns on LP2951, but deselects all chips.
temp VAR Byte ' Temporary variable used in several routines.


'=====================================================================
' NJU6355 CLOCK/CALENDAR CONSTANTS AND VARIABLES
'=====================================================================
' The NJU6355ED clock/calendar chip maintains a 13-digit BCD account
' of the current year, month, day, day of week, hour, minute, and
' second. The clock subroutines transfer this data to/from a 13-nibble
' array in the BS2's RAM called "DTG" for "date-time group." The
' constants below allow you to refer to the digits by name; e.g.,
' "Y10s" is the tens digit of the year. Note that there's no "am/pm"
' indicator--the NJU6355 uses the 24-hour clock. For instance, 2:00 pm
' is written or read as 14:00 (without the colon, of course).
'


Y10s CON 1 ' Array position of year 10s digit.
Y1s CON 0 ' " " " year 1s "
Mo10s CON 3 ' " " " month 10s "
Mo1s CON 2 ' " " " month 1s "
'
D10s CON 5 ' " " " day 10s "
D1s CON 4 ' " " " day 1s "
H10s CON 8 ' " " " hour 10s "
H1s CON 7 ' " " " hour 1s "
M10s CON 10 ' " " " minute 10s "
M1s CON 9 ' " " " minute 1s "
S10s CON 12 ' " " " second 10s "
S1s CON 11 ' " " " second 1s "
day CON 6 ' " " " day-of-week (1-7) digit.
digit VAR Nib ' Number of 4-bit BCD digits read/written.
DTG VAR Nib(13) ' Array to hold "date/time group" BCD digits.
Hours VAR Byte 'Holds the combined 1s and 10s digits.
Minutes VAR Byte 'Holds the combined 1s and 10s digits.
HoursMins VAR Word 'Holds the combined Hours and Minutes for ease of thinking
'=====================================================================
' DEMONSTRATION PROGRAM
'=====================================================================
' This program implements a simple, user-friendly data logger.
' It can be used in two ways: If a PC running terminal software [9600
' baud, N81, carriage returns (CR) converted to CR plus linefeed,
' no handshaking] is connected, the program displays a menu and prompt
' that allows the user to display the ADC readings; view/set the clock,
' read, dump, or erase memory; or start the logger. If no terminal is
' connected, or the user doesn't press a key within a preset time, the
' program automatically starts logging data. It takes samples from
' both ADC inputs every 60 seconds and records these, along with a
' time-tag and error-detection checksum, into EEPROM.
' Initial setup.
OUTH = pwrOn ' Get ready to turn on peripherals.
DIRH = $DF ' Set all bits of high port to output except P13.
DIRL = $7F ' Set all bits of low port to output except for P7.
' setup =============
' When you apply power to the board, the BS2 will send a message at
' 9600 bps through the built-in serial port asking for setup
' instructions. If you don't press a key within timeout seconds, the
' program will begin logging data.
'
'
setup:

'DEBUG "Hello World. The Gateway is alive.", CR
'PAUSE 5000 'REMOVE THis later..


'Check to see if the PIR is on.
GOSUB pir_check_sub


'Now handle the D key on the remote.
'Lets see if we can read it with a debug statment
'DEBUG "Input 13 is: ", DEC IN13
'DEBUG "Checking D key: ",CR
GOSUB mp3_play_all_sub 'Test the D key and see if we should play all the songs.


'If Mins =0 and seconds = 0 then it must be hour. Time to play westminster chimes and then scout law.
'24:00 is midnight.


'Read Clock
'DEBUG "Reading clock: ",CR
GOSUB read_clock_sub ' Update DTG data.
Minutes =(dtg(m10s)*10) + dtg(m1s)
Hours =(dtg(h10s)*10) + dtg(h1s)
HoursMins = (Hours*100)+Minutes

'''''DEBUG "The clocks says: ",DEC Hours, DEC Minutes, " or :",DEC HoursMins,CR

'Skip to quiet time if necessary so we don't wake up the birdies.
IF HoursMins > LIGHTS_OUT THEN quiet_time
IF HoursMins < LIGHTS_ON THEN quiet_time
'DEBUG " It's not queit time.",CR

'Reset these variables each time through the loop.
TURN_OFF_IPOD_AFTER_TRACK=0
IPOD_KEEP_ALIVE=0 'a flag so we don't readjust the volume if we play two tracks.
'
'Test clock for hourly events and then play any feature sound tracks.
IF Minutes <> 0 THEN fifteen
'Time to play!
VOLUME = MASTER_VOLUME_CLOCK
TRACK = 9 ' big ben
PLAY_TIME = 16
GOSUB mp3gototrack_sub
'now play scout law.
TRACK = 10 + Hours
IF TRACK <23 THEN mark1 'adjust for after 12 Noon.
TRACK=TRACK-12
mark1:
PLAY_TIME = 22
GOSUB mp3gototrack_sub


'TURN_OFF_IPOD_AFTER_TRACK =1
'Now lets see what else we can play on the hour.
'At 0700 we want to play revelie (track 6) followed by I'm a nut (track 23)
'At 0800 we want to play Journey (track 27)
'At 0900 we want to play NY NY (track 34) for 30 seconds
'At 1000 we want to play Beyond the sea (track 31) for 30 seconds
'At 1100 we want to play Pieces of Heaven (Track 38) for 30 seconds
'At 1200 we want to play Lunch call (track 8)
'At 1300 we want to play NM intro (track 24) for 30 seconds
'At 1400 we want to play Sponge Bob (track 2)
'At 1500 we want to play Batman (track 28) for 150
'At 1600 we want to play Enter Sandman (Track 26)
'At 1700 we want to play Mr W, Mr N ( track 7)
'At 1800 we want to play Surfing with the Alien (track 29) for 100
'At 1900 we want to play part of Phantom (track 30)
'At 2000 we want to play Eye of the Tiger (track 36) for 45
'At 2100 we want to play Lay Down your life (track 37) for xx
'At 2200 we want to play taps (track 10).


'Note 130 seems to be the longes delay I can use...the pause statement is only good to 65 seconds.
'OK now we can go for two minutes! I split up the delay into tow lines...
IF Hours <>7 THEN nex_hrs_feature07
TRACK = 6 ' revalie
PLAY_TIME = 24
GOSUB mp3gototrack_sub
TRACK = 23 ' nut song
PLAY_TIME = 130
GOSUB mp3gototrack_sub
nex_hrs_feature07:

IF Hours <>8 THEN nex_hrs_feature08
TRACK = 27 ' Journey
PLAY_TIME = 34
GOSUB mp3gototrack_sub
nex_hrs_feature08:

IF Hours <>9 THEN nex_hrs_feature09
TRACK = 34 ' NY NY
PLAY_TIME = 34
GOSUB mp3gototrack_sub
nex_hrs_feature09:

IF Hours <>10 THEN nex_hrs_feature10
TRACK = 31 ' beyond the sea
PLAY_TIME = 34
GOSUB mp3gototrack_sub
nex_hrs_feature10:

IF Hours <>11 THEN nex_hrs_feature11
TRACK = 50 ' P. O. H
PLAY_TIME = 40
GOSUB mp3gototrack_sub
nex_hrs_feature11:

IF Hours <>12 THEN nex_hrs_feature12
TRACK = 8 ' lunch call
PLAY_TIME = 12
GOSUB mp3gototrack_sub
nex_hrs_feature12:

IF Hours <>13 THEN nex_hrs_feature13
TRACK = 24 ' NM intro
PLAY_TIME = 47
GOSUB mp3gototrack_sub
nex_hrs_feature13:

IF Hours <>14 THEN nex_hrs_feature14
TRACK = 2 ' spongebob
PLAY_TIME = 40
GOSUB mp3gototrack_sub
nex_hrs_feature14:

IF Hours <>15 THEN nex_hrs_feature15
TRACK = 28 ' batman
PLAY_TIME = 150
GOSUB mp3gototrack_sub
nex_hrs_feature15:

IF Hours <>16 THEN nex_hrs_feature16
TRACK = 26 'Enter Sandman
PLAY_TIME = 50
GOSUB mp3gototrack_sub
nex_hrs_feature16:

IF Hours <>17 THEN nex_hrs_feature17
TRACK = 7 ' Mr W
PLAY_TIME = 22
GOSUB mp3gototrack_sub
nex_hrs_feature17:

IF Hours <>18 THEN nex_hrs_feature18
TRACK = 29 ' Surfing with the Alien
PLAY_TIME = 105
GOSUB mp3gototrack_sub
nex_hrs_feature18:

IF Hours <>19 THEN nex_hrs_feature19
TRACK = 30 ' Phantom
PLAY_TIME = 45
GOSUB mp3gototrack_sub
nex_hrs_feature19:

IF Hours <>20 THEN nex_hrs_feature20
TRACK = 36 ' eye of the tiger
PLAY_TIME = 45
GOSUB mp3gototrack_sub
nex_hrs_feature20:

IF Hours <>21 THEN nex_hrs_feature21
TRACK = 37 ' lay down your life
PLAY_TIME = 62
GOSUB mp3gototrack_sub
nex_hrs_feature21:

IF Hours <>22 THEN nex_hrs_feature22
TRACK = 10 ' Taps
PLAY_TIME = 58
GOSUB mp3gototrack_sub
nex_hrs_feature22:
'DEBUG "Turning power off in hour routine..all done: ",CR
GOSUB mp3off_sub 'Just kill it...
PAUSE 45000 'Add a delay to make sure the clock advances a minute

'
'
fifteen: 'Now test clock for 15 minutes past the hour.
IF Minutes <> 15 THEN thirty
'Time to play!
VOLUME = MASTER_VOLUME_CLOCK
TRACK = 3 ' Big Boat
PLAY_TIME = 15
TURN_OFF_IPOD_AFTER_TRACK =1
GOSUB mp3gototrack_sub
'DEBUG "exectuting 15 minute routine delay of 1 minute: ",CR
PAUSE 50000 'Add a delay to make sure the clock advances a minute
'DEBUG "done:",CR

'
'
thirty: 'Now test clock for 30 minutes past the hour.
IF Minutes <> 30 THEN fortyfive
'Time to play!
VOLUME = MASTER_VOLUME_CLOCK
TRACK = 4 ' Seagulls
PLAY_TIME = 15
TURN_OFF_IPOD_AFTER_TRACK =1
GOSUB mp3gototrack_sub
PAUSE 45000
'
'
fortyfive: 'Now test clock for 45 minutes past the hour.
IF Minutes <> 45 THEN quiet_time
'Time to play!
VOLUME = MASTER_VOLUME_CLOCK
TRACK = 5 'foghorn
PLAY_TIME = 6
'DEBUG "45 mins part 1:",CR
GOSUB mp3gototrack_sub
TRACK = 5 'foghorn
'DEBUG "45 mins part 2:",CR
GOSUB mp3gototrack_sub
TRACK = 5 'foghorn
TURN_OFF_IPOD_AFTER_TRACK =1
'DEBUG "45 mins part 3:",CR
GOSUB mp3gototrack_sub 'see if we can play this three timeS!
PAUSE 50000

quiet_time:
'
'Uncomment the two lines "'''''" to get the menu...BUT need to have PC attached or BSII will pause forever.
'SEROUT 16,b96,[CR,CR,"BS2 Gateway"] ' Sign-on and choices.
'''''SEROUT 16,b96,[CR,"(A)DC (T)ime (C)lock (M)p3 on (N)Mp3 off (U)p (D)own (R)ight (L)eft (P)lay (F)irst track (W)alkway light",CR,">"]
''''SERIN 16,b96,timeout,setup,[temp] ' Look for instruction.
' If no instruction arrives within timeout milliseconds, then go back to setup,
' else get choose from setup menu.
'
TURN_OFF_IPOD_AFTER_TRACK =0 ' Reset this every time through the loop.

temp = temp & $0DF ' Convert to uppercase.
'IF temp = "A" THEN showADC ' Show current ADC measurement.
IF temp = "T" THEN time ' Show the current time/date.
IF temp = "C" THEN clock ' Set the clock/calendar.
'
'IPOD functions
IF temp = "M" THEN mp3on 'Turn on the power amp and the Ipod
IF temp = "N" THEN mp3off 'Turn off the power amp and the Ipod
IF temp = "U" THEN mp3volup 'Crank up the Volume
IF temp = "D" THEN mp3voldown 'Crank up the Volume
IF temp = "Z" THEN mp3vol_zero 'volume all the way down
IF temp = "Y" THEN mp3vol_big_up '
'
IF temp = "R" THEN mp3right 'Right Button
IF temp = "L" THEN mp3left 'Left Button
IF temp = "P" THEN mp3play 'Left Button
IF temp = "F" THEN mp3firsttrack 'First Track

'Light functions
IF temp = "W" THEN walkway_light_on 'Walkway Light On
IF temp = "X" THEN walkway_light_off 'Walkway Light On

'Test function
IF temp = "E" THEN mp3_test_track

GOTO setup ' Unrecognized entry; try again.

'Menu Processing...This is where we call the subroutines from
mp3on:
GOSUB mp3on_sub
GOTO setup

mp3off:
GOSUB mp3off_sub
GOTO setup

'IPOD Volume functions
mp3volup:
GOSUB mp3volup_sub
GOTO setup

mp3voldown:
GOSUB mp3voldown_sub
GOTO setup

mp3vol_zero:
VOLUME = 0
GOSUB mp3_set_volume_sub
GOTO setup

mp3vol_big_up: 'Big up will increase increase the volume by VOLUME.
VOLUME = 5
FOR LOOP = 1 TO VOLUME 'Read the Global variable VOLUME
GOSUB mp3volup_sub
NEXT
GOTO setup

'Ipod navigation keys
mp3right:
'DEBUG "Right Button",cr
GOSUB mp3right_sub:
GOTO setup

mp3left:
'DEBUG "Left BUTTON" ,cr
LOW LEFT
PAUSE IPOD_KEY_PAUSE
HIGH LEFT
PAUSE IPOD_KEY_PAUSE
GOTO setup

mp3play:
GOSUB mp3play_sub
GOTO setup

walkway_light_on:
GOSUB walkway_light_on_sub
GOTO setup

walkway_light_off:
GOSUB walkway_light_off_sub
GOTO setup

mp3firsttrack:
GOSUB mp3firsttrack_sub
GOTO setup



'Test track Test track Test track Test track Test track Test track Test track Test track Test track Test track Test track
mp3_test_track:

'DEBUG "run test track"
'These are the variables we care about

'VOLUME VAR Byte 'Ipod volume
'TRACK VAR Byte 'IPOD track to play
'PLAY_TIME VAR Byte 'Time in seconds that the IPOD will play a track.
'DONT_TURN_OFF_IPOD
VOLUME = MASTER_VOLUME_CLOCK
TRACK = 23 ' Nut Song
PLAY_TIME = 15
GOSUB mp3gototrack_sub


TRACK = 34 ' NY NY
PLAY_TIME = 15
GOSUB mp3gototrack_sub

TRACK = 6 ' rev
PLAY_TIME = 22
TURN_OFF_IPOD_AFTER_TRACK =1
GOSUB mp3gototrack_sub
GOTO setup


' time =============
' Put the time and date from the NJU clock chip on the screen and
' go back to the setup routine.
time:
GOSUB read_clock_sub ' Update DTG data.
GOSUB show_time ' Display the time.
GOSUB show_date ' Display the date.
GOTO setup ' Back to setup.
' clock =============
' Let the user set the current time and date via the set_clock subroutine.
' This code just jury-rigs an IF ... THEN GOSUB ... instruction, which
' PBASIC lacks. When done, goes back to setup.
'
clock: ' Set the internal clock.
GOSUB set_clock_sub
GOTO setup ' Return to setup for more instructions.


'
'Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines
'Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines
'Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines Subroutines


'IPOD On/OFF Subs
mp3on_sub:
'Set all the Ipod keys High
HIGH VOL_UP
HIGH VOL_DOWN
HIGH PLAY_PAUSE
HIGH LEFT
HIGH RIGHT
HIGH AMP_ON
PAUSE IPOD_INIT_TIME
RETURN

mp3off_sub:
LOW AMP_ON
RETURN

mp3firsttrack_sub:
'The iPod shuffle includes a handy shortcut FOR going back TO the beginning of its playlist:
'just press Play/PAUSE three times AND the shuffle will start playing at the beginning
'of the first song. (You don't even have to say, "There's no place like home.")
'Must push fast..
'1 press
FOR loop = 1 TO 3
LOW PLAY_PAUSE
PAUSE IPOD_KEY_PAUSE
HIGH PLAY_PAUSE
PAUSE IPOD_KEY_PAUSE
NEXT
TRACK_PLAYED_LAST = 1 'We are now at track 1 and it is playing so save the fact.
RETURN


'IPOD PLAY ROUTINE
mp3gototrack_sub:
'TRACK will contain the track we want to jump to.
'TRACK_PLAYED_LAST will contain the last track played. 1= first track.
'LAST_TRACK will be a constant to track how many tracks are on the IPOD
'Rules: Lets always jump ahead to keep things sime.
'

IF IPOD_KEEP_ALIVE=1 THEN skip_on4
'DEBUG "Turning Ipod On",CR
GOSUB mp3on_sub
'select first track
'DEBUG "setting first track: ",CR
GOSUB mp3firsttrack_sub:
GOSUB mp3_set_volume_sub
'DEBUG "Pause track 1",CR
GOSUB mp3play_sub
skip_on4:
IPOD_KEEP_ALIVE=0 'Reset our flag
'Track 1 is now 5 minutes of silence.
'The Ipod must be playing a file in order to let the user adjust the volume.
'
'
'
'Activating the right button will take the ipod out of Pause mode.
'
'DEBUG "going to track: " ,DEC TRACK , " From Track: ", DEC TRACK_PLAYED_LAST, CR
'Now implement the code
' Lets assume we never play the same track twice.
loopy VAR Byte ' a temporary byte used in this sub
'
'DEBUG "TRACK - TRACK_PLAYED_LAST )", DEC (TRACK-TRACK_PLAYED_LAST ) , CR
'
loopy=1
IF (TRACK=TRACK_PLAYED_LAST ) THEN move_track 'must mean we want to play the same track a few times.

IF (TRACK-TRACK_PLAYED_LAST ) >1000 THEN wrap 'When using bytes, the difference comes out to some number
'bigger than 65000.
loopy=(TRACK-TRACK_PLAYED_LAST)
GOTO move_track
wrap:
'DEBUG "wrapit:",CR
loopy=(LAST_TRACK -TRACK_PLAYED_LAST +TRACK)
'DEBUG "LAST_TRACK -TRACK_PLAYED_LAST +TRACK : ",DEC LAST_TRACK -TRACK_PLAYED_LAST +TRACK ,CR

move_track:
'DEBUG "loopy = ", DEC loopy ,CR
FOR loop = 1 TO loopy
GOSUB mp3right_sub
NEXT
IF loopy <>1 THEN play_it
'Loopy is on so hit the left key so we can repeat the track
GOSUB mp3left_sub
play_it:
'Delay program execution here to allow the track to play.

'DEBUG "Playing the track now for ", DEC play_time, " Seconds." ,CR
'The Pause function is only good for 65 seconds!
'If I want to be able to play a two minute track, I need to multiply the delay.

'DEBUG "Play_time is:", DEC PLAY_TIME,CR
'DEBUG "Pauseing for ", DEC PLAY_TIME*1000, " m seconds", CR
PAUSE PLAY_TIME/4*1000 'Pause is in miliseconds, PLAYTIME is in seconds
'DEBUG "P2", CR
PAUSE PLAY_TIME/4*1000 'Pause is in miliseconds, PLAYTIME is in seconds
'DEBUG "P3", CR
PAUSE PLAY_TIME/4*1000 'Pause is in miliseconds, PLAYTIME is in seconds
'DEBUG "P4", CR
PAUSE PLAY_TIME/4*1000 'Pause is in miliseconds, PLAYTIME is in seconds

GOSUB mp3play_sub 'Pause the IPOD after the track has been played.
'
'Save the track numnber of the last played track
TRACK_PLAYED_LAST = TRACK
'Do we need to turn the power off to the Ipod?
'DEBUG "Deciding if we should turn off the power: ",cr
IF TURN_OFF_IPOD_AFTER_TRACK=0 THEN no_kill_power
'DEBUG "killing Power",CR
GOSUB mp3off_sub
RETURN
no_kill_power:
'DEBUG "not killing power",CR
TURN_OFF_IPOD_AFTER_TRACK=0
IPOD_KEEP_ALIVE=1 'a flag so we don't readjust the volume if we play two tracks.
'DEBUG "Time to return from mp3gototrack_sub:",CR
RETURN


mp3_play_all_sub:
'If the D key is activated for 10 seconds then begin playing all the music tracks.
'I added a feature where we monitor the DC state of the D key. Now we can trigger on a transition
'instead of a state in the routine below.
'D_key_DC_state starts out at 0 when the power is switched on.
'
'DEBUG "Input 13 is: ", BIN IN13, "D_KEY_DC_state is: ",BIN D_key_DC_state, " invert is: ", BIN1 ~D_key_DC_state,CR
'
loopy2 VAR Byte ' a temporary byte used in this sub
'
'Test #1 of the D key
'
IF IN13<>D_key_DC_state THEN no_d 'was 1 check to see if true.
'DEBUG "Now Pausing in D key routine. Key was pressed",CR
PAUSE 5000 'Pause 5 seconds and then see if the key is still pressed.
'
'Now test to see that the user held the key for 5 seconds..
'
'Test 2
'
IF IN13<>D_key_DC_state THEN no_d' was 1...key was released so exit
'DEBUG "test2 D key check ",CR
PAUSE 5000 'Pause 5 seconds and then see if the key is NOT pressed.
'
IF IN13<>D_key_DC_state THEN test3b 'was 1...Key pressed
'If we made it here then the key is stuck....change the dc level
D_key_DC_state = ~D_key_DC_state
'DEBUG "D key stuck. Change level. DC Key state is now: ",BIN1 D_key_DC_state, CR
GOTO no_d
'
test3b:
IF IN13=D_key_DC_state THEN no_d 'was 0
'ok so we made it this far
'DEBUG "Start playing all the songs",CR
'Time to play!
VOLUME = MASTER_VOLUME_SONGS
TRACK = 22
PLAY_TIME = 1
TURN_OFF_IPOD_AFTER_TRACK=0
GOSUB mp3gototrack_sub
GOSUB mp3play_sub 'Now unpause the ipod
'Now Ipod will keep playing everyting including the sound effect.
'Now to detect when to exit.

wait_to_exit:
'New feature....If the key is held for 5 seconds then skip to the next song else exit out.
'So what happens if the key is held longer.
PAUSE 1000
IF IN13<>D_key_DC_state THEN wait_to_exit 'was 1
'Must have been 0 so shut down and exit
GOSUB mp3play_sub
'DEBUG "killing Power for mp3_play_all subsoutine",CR
GOSUB mp3off_sub
no_d:
RETURN



'IPOD Volume subroutines
mp3volup_sub:
FOR LOOP = 1 TO VOLUME 'Read the Global variable VOLUME
LOW VOL_UP
PAUSE IPOD_KEY_PAUSE
HIGH VOL_UP
'PAUSE IPOD_KEY_PAUSE test this.. add back if necessary
NEXT
RETURN

mp3voldown_sub:
'Note: The Ipod will not let you adjust the volume if the unit is paused...
'May need to add a blank track in to allow me to adjust the volume.
'DEBUG "Now in Vol Down sub"
LOW VOL_DOWN
PAUSE IPOD_KEY_PAUSE
HIGH VOL_DOWN
'PAUSE IPOD_KEY_PAUSE test this.. add back if necessary
RETURN

mp3_set_volume_sub: 'Set the volume to the number set by VOLUME
'first reset the volume to 0
'DEBUG "resetting volume to 0",CR
FOR LOOP = 1 TO 64 'I'll assume 64 is enough to kill it.
GOSUB mp3voldown_sub
NEXT
'
FOR LOOP = 1 TO VOLUME 'Read the Global variable VOLUME
LOW VOL_UP
PAUSE IPOD_KEY_PAUSE
HIGH VOL_UP
PAUSE IPOD_KEY_PAUSE
NEXT
RETURN




'IPOD Buttons
'
mp3play_sub:
LOW PLAY_PAUSE
PAUSE IPOD_KEY_PAUSE
HIGH PLAY_PAUSE
PAUSE IPOD_KEY_PAUSE
RETURN

mp3right_sub:
'DEBUG "Right Button"
LOW RIGHT
PAUSE IPOD_KEY_PAUSE
HIGH RIGHT
PAUSE IPOD_KEY_PAUSE 'I tried to remove this delay but it didn't work too good.
RETURN

mp3left_sub:
'DEBUG "Right Button"
LOW LEFT
PAUSE IPOD_KEY_PAUSE
HIGH LEFT
PAUSE IPOD_KEY_PAUSE
RETURN

pir_check_sub:
'Add some smarts to the PIR routine so that we don't hold up execution of the main program if the PIR is activated.
'will need to get back to this...
'DEBUG "pin 7 is: ",BIN1 IN7, CR
'If IN7 = 1 then PIR is on
'If IN7 = 0 the PIR is Off
'PIR_delay will let us multitask the light timer..it's a nib..4 bits.
'
IF IN7 = 0 THEN no_pir 'PIR is off so lets exit this subroutine
'PIR must be on then so lets turn on the LED gateway light
'DEBUG "Turn on the light: ",CR
GOSUB walkway_light_on_sub
PIR_delay = 10 'This comes out to about 10 seconds
'As a check step lets make sure the light gets turned off.
'IF IN7 = 1 THEN no_ 'PIR is on and detecting movement so turn on LED light
'
no_pir:
'DEBUG "Turn off the light! :" ,CR
IF PIR_delay <>0 THEN pir_test1
GOSUB walkway_light_off_sub 'Now turn it off

pir_test1:
IF PIR_delay = 0 THEN pir_test2
PIR_delay = PIR_delay-1
pir_test2:
'DEBUG "PIR Dealy = ",DEC PIR_delay
RETURN

'Walkway light Subs
walkway_light_on_sub:
HIGH GATELIGHT_ON
PAUSE IPOD_KEY_PAUSE
RETURN

walkway_light_off_sub:
LOW GATELIGHT_ON
PAUSE IPOD_KEY_PAUSE
RETURN


' ==================================================================
' NJU6355 CLOCK/CALENDAR SUBROUTINES
' ==================================================================
' read_clock =============
' Get the current date/time group from the NJU6355 clock and store
' it in the array DTG(n).
read_clock_sub:
LOW NJU_IO ' Set for read.
HIGH NJU_CE ' Select the chip.
FOR digit = 0 TO 12 ' Get 13 digits.
SHIFTIN DATA_,CLK,LSBPRE,[DTG(digit)\4] ' Shift in a digit.
NEXT ' Next digit.
LOW NJU_CE ' Deselect the chip.
RETURN ' Return to program.
' write_clock =============
' Get the time stored in DTG(n) and write it to the NJU6355 clock.
' Note that the NJU6355 does not allow you to write the seconds digits.
' If clears the seconds digits when written, so if you set it for
' 08:30 (hh:mm), when the write is complete, the NJU6355 starts at
' 08:30:00 (hh:mm:ss).
write_clock:
HIGH NJU_IO ' Set for write.
HIGH NJU_CE ' Select the chip.
FOR digit = 0 TO 10 ' Write 11 digits.

SHIFTOUT DATA_,CLK,LSBFIRST,[DTG(digit)\4] ' Shift out a digit.
NEXT ' Next digit.
LOW NJU_CE ' Deselect the chip.
RETURN ' Return to program.
' set_clock =============
' Set the clock/calendar based on data entered by the user at
' 9600 bps through the built-in serial-port connector.
set_clock_sub:
SEROUT 16,b96,[CR,"Year (YY): "]: GOSUB get2BCD ' Get year.
DTG(Y10s) = temp.HIGHNIB:DTG(Y1s) = temp.LOWNIB ' Store.
SEROUT 16,b96,[CR,"Month (MM): "]: GOSUB get2BCD ' Get month.
DTG(Mo10s) = temp.HIGHNIB:DTG(Mo1s) = temp.LOWNIB ' Store.
SEROUT 16,b96,[CR,"Day (DD): "]: GOSUB get2BCD ' Get day.
DTG(D10s) = temp.HIGHNIB:DTG(D1s) = temp.LOWNIB ' Store.
SEROUT 16,b96,[CR,"DOW (1-7): "] ' Get day of wk.
SERIN 16,b96,[HEX1 temp]
DTG(day) = temp.LOWNIB ' Store.
SEROUT 16,b96,[CR,"Hour (HH): "]: GOSUB get2BCD ' Get year.
DTG(H10s) = temp.HIGHNIB:DTG(H1s) = temp.LOWNIB ' Store.
SEROUT 16,b96,[CR,"Minute (MM): "]: GOSUB get2BCD ' Get month.
DTG(M10s) = temp.HIGHNIB:DTG(M1s) = temp.LOWNIB ' Store.
GOSUB write_clock
RETURN
' show_date =============
' Display the date stored in DTG.
show_date:
SEROUT 16,b96,[CR,HEX DTG(Mo10s), HEX DTG(Mo1s),"/",HEX DTG(D10s), HEX DTG(D1s),"/",HEX DTG(Y10s), HEX DTG(Y1s)]
RETURN
' show_time =============
' Display the time stored in DTG.
show_time:
SEROUT 16,b96,[CR,HEX DTG(H10s), HEX DTG(H1s),":",HEX DTG(M10s), HEX DTG(M1s),":",HEX DTG(S10s), HEX DTG(S1s)]
RETURN
' get2BCD =============
' Get two BCD digits using the HEX2 modifier. Within the range 0-9,
' hex and BCD are the same, so this is useful for setting the clock
' in its BCD format.
get2BCD:
SERIN 16,b96,[HEX2 temp]
RETURN



 

Back to main index