From 3c791b2c0ec96e43f2cd2d28d46e0672e25ede0f Mon Sep 17 00:00:00 2001 From: Matthew Shepard Date: Thu, 29 Oct 2015 14:38:07 -0700 Subject: [PATCH 1/4] Add example directory and webpack config --- client.js | 7 ++++ examples/AUX_MAIN_TERM.bs2 | 37 +++++++++++++++++++ examples/BRANCH.bs2 | 33 +++++++++++++++++ examples/BUTTON.bs2 | 30 +++++++++++++++ examples/COUNT.bs2 | 44 ++++++++++++++++++++++ examples/DATA.bs2 | 70 +++++++++++++++++++++++++++++++++++ examples/DEBUG_DEBUGIN.bs2 | 23 ++++++++++++ examples/DO-LOOP.bs2 | 41 +++++++++++++++++++++ examples/DTMFOUT.bs2 | 55 ++++++++++++++++++++++++++++ examples/EXIT.bs2 | 26 +++++++++++++ examples/FOR-NEXT.bs2 | 24 ++++++++++++ examples/FREQOUT.bs2 | 53 +++++++++++++++++++++++++++ examples/GOSUB.bs2 | 44 ++++++++++++++++++++++ examples/GOTO.bs2 | 29 +++++++++++++++ examples/HIGH_LOW.bs2 | 15 ++++++++ examples/I2C.bs2 | 59 ++++++++++++++++++++++++++++++ examples/IF-THEN-ELSE.bs2 | 36 ++++++++++++++++++ examples/IF-THEN.bs2 | 30 +++++++++++++++ examples/INPUT_OUTPUT.bs2 | 30 +++++++++++++++ examples/LCDCMD.bs2 | 48 ++++++++++++++++++++++++ examples/LCDIN.bs2 | 54 +++++++++++++++++++++++++++ examples/LCDINIT.bs2 | 48 ++++++++++++++++++++++++ examples/LCDOUT.bs2 | 52 ++++++++++++++++++++++++++ examples/LOOKDOWN.bs2 | 26 +++++++++++++ examples/LOOKUP.bs2 | 22 +++++++++++ examples/NAP.bs2 | 17 +++++++++ examples/ON-GOSUB.bs2 | 33 +++++++++++++++++ examples/ON-GOTO.bs2 | 33 +++++++++++++++++ examples/OWIN_OWOUT.bs2 | 63 ++++++++++++++++++++++++++++++++ examples/PAUSE.bs2 | 15 ++++++++ examples/PULSIN.bs2 | 42 +++++++++++++++++++++ examples/PULSOUT.bs2 | 25 +++++++++++++ examples/PWM.bs2 | 31 ++++++++++++++++ examples/RANDOM.bs2 | 41 +++++++++++++++++++++ examples/RCTIME1.bs2 | 25 +++++++++++++ examples/RCTIME2.bs2 | 36 ++++++++++++++++++ examples/READ.bs2 | 30 +++++++++++++++ examples/RETURN.bs2 | 29 +++++++++++++++ examples/REVERSE.bs2 | 24 ++++++++++++ examples/SELECT-CASE.bs2 | 51 ++++++++++++++++++++++++++ examples/SERIN_SEROUT1.bs2 | 49 +++++++++++++++++++++++++ examples/SERIN_SEROUT2.bs2 | 52 ++++++++++++++++++++++++++ examples/SHIFTIN.bs2 | 32 ++++++++++++++++ examples/SHIFTOUT.bs2 | 37 +++++++++++++++++++ examples/SLEEP.bs2 | 17 +++++++++ examples/STOP.bs2 | 12 ++++++ examples/STOREALL.bs2 | 75 ++++++++++++++++++++++++++++++++++++++ examples/TOGGLE.bs2 | 24 ++++++++++++ examples/WRITE.bs2 | 40 ++++++++++++++++++++ examples/XOUT.bs2 | 46 +++++++++++++++++++++++ package.json | 1 + webpack.config.js | 13 ++++++- 52 files changed, 1828 insertions(+), 1 deletion(-) create mode 100755 examples/AUX_MAIN_TERM.bs2 create mode 100755 examples/BRANCH.bs2 create mode 100755 examples/BUTTON.bs2 create mode 100755 examples/COUNT.bs2 create mode 100755 examples/DATA.bs2 create mode 100755 examples/DEBUG_DEBUGIN.bs2 create mode 100755 examples/DO-LOOP.bs2 create mode 100755 examples/DTMFOUT.bs2 create mode 100755 examples/EXIT.bs2 create mode 100755 examples/FOR-NEXT.bs2 create mode 100755 examples/FREQOUT.bs2 create mode 100755 examples/GOSUB.bs2 create mode 100755 examples/GOTO.bs2 create mode 100755 examples/HIGH_LOW.bs2 create mode 100755 examples/I2C.bs2 create mode 100755 examples/IF-THEN-ELSE.bs2 create mode 100755 examples/IF-THEN.bs2 create mode 100755 examples/INPUT_OUTPUT.bs2 create mode 100755 examples/LCDCMD.bs2 create mode 100755 examples/LCDIN.bs2 create mode 100755 examples/LCDINIT.bs2 create mode 100755 examples/LCDOUT.bs2 create mode 100755 examples/LOOKDOWN.bs2 create mode 100755 examples/LOOKUP.bs2 create mode 100755 examples/NAP.bs2 create mode 100755 examples/ON-GOSUB.bs2 create mode 100755 examples/ON-GOTO.bs2 create mode 100755 examples/OWIN_OWOUT.bs2 create mode 100755 examples/PAUSE.bs2 create mode 100755 examples/PULSIN.bs2 create mode 100755 examples/PULSOUT.bs2 create mode 100755 examples/PWM.bs2 create mode 100755 examples/RANDOM.bs2 create mode 100755 examples/RCTIME1.bs2 create mode 100755 examples/RCTIME2.bs2 create mode 100755 examples/READ.bs2 create mode 100755 examples/RETURN.bs2 create mode 100755 examples/REVERSE.bs2 create mode 100755 examples/SELECT-CASE.bs2 create mode 100755 examples/SERIN_SEROUT1.bs2 create mode 100755 examples/SERIN_SEROUT2.bs2 create mode 100755 examples/SHIFTIN.bs2 create mode 100755 examples/SHIFTOUT.bs2 create mode 100755 examples/SLEEP.bs2 create mode 100755 examples/STOP.bs2 create mode 100755 examples/STOREALL.bs2 create mode 100755 examples/TOGGLE.bs2 create mode 100755 examples/WRITE.bs2 create mode 100755 examples/XOUT.bs2 diff --git a/client.js b/client.js index 042d261..8df61ff 100644 --- a/client.js +++ b/client.js @@ -1,9 +1,16 @@ 'use strict'; +const _ = require('lodash'); const Irken = require('irken'); const app = new Irken(); +const examples = _.map(EXAMPLES_LIST, function(name){ + return require('./examples/'+name); +}); + +console.log(examples); + const plugins = [ { register: require('bs2-serial') diff --git a/examples/AUX_MAIN_TERM.bs2 b/examples/AUX_MAIN_TERM.bs2 new file mode 100755 index 0000000..c7081b8 --- /dev/null +++ b/examples/AUX_MAIN_TERM.bs2 @@ -0,0 +1,37 @@ +'{$STAMP BS2p} +'{$PBASIC 2.5} + +'SOURCE: AUX_MAIN_TERM +'This program demonstrates the use of the AUXIO, MAINIO and IOTERM +'commands to affect I/O pins in the auxiliary and main I/O groups. + +Init: + PAUSE 200 'short startup-pause + +#SELECT $STAMP 'Notify of module requirements + #CASE BS2, BS2E, BS2SX + #ERROR "Program requires BS2p40" + #CASE BS2P, BS2PE, BS2PX + DEBUG "Note: This program is designed for the BS2p40.", CR +#ENDSELECT + +port VAR Bit + +Main: + DO + MAINIO 'Switch to main I/O pins + TOGGLE 0 'Toggle state of I/O pin P0 + PWM 1, 100, 40 'Generate PWM on I/O pin P1 + + AUXIO 'Switch to auxiliary I/O pins + TOGGLE 0 'Toggle state of I/O pin X0 + PULSOUT 1, 1000 'Generate a pulse on I/O pin X1 + PWM 2, 100, 40 'Generate PWM on I/O pin X2 + + IOTERM port 'Switch to main or aux I/Os + '-- depending on port + TOGGLE 3 'Toggle state of I/O pin 3 + '-- on main and aux, alternately + port = ~port 'Invert port + PAUSE 1000 '1 second delay + LOOP \ No newline at end of file diff --git a/examples/BRANCH.bs2 b/examples/BRANCH.bs2 new file mode 100755 index 0000000..2884a18 --- /dev/null +++ b/examples/BRANCH.bs2 @@ -0,0 +1,33 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: BRANCH +'This program shows how the value of an index variable (idx) controls the +'destination of the BRANCH instruction. + +idx VAR Byte + +Init: + PAUSE 200 'short startup-pause + +Main: + DEBUG "idx: ", DEC idx, " " + BRANCH idx, [Task_0, Task_1, Task_2] 'branch to task + DEBUG "BRANCH target error...", CR, CR '... unless out of range + +Next_Task: + idx = idx + 1 // 4 'force idx to be 0..3 + PAUSE 250 + GOTO Main + +Task_0: + DEBUG "BRANCHed to Task_0", CR + GOTO Next_Task + +Task_1: + DEBUG "BRANCHed to Task_1", CR + GOTO Next_Task + +Task_2: + DEBUG "BRANCHed to Task_2", CR + GOTO Next_Task \ No newline at end of file diff --git a/examples/BUTTON.bs2 b/examples/BUTTON.bs2 new file mode 100755 index 0000000..0575fca --- /dev/null +++ b/examples/BUTTON.bs2 @@ -0,0 +1,30 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: BUTTON +'Connect an active-low circuit (shown in the BUTTON command description) to pin P0 +'of the BASIC Stamp. When you press the button, the BUTTON command will detect the +'low signal and then the DEBUG command will execute to display an asterisk (*) on +'the Debug Terminal. After the first button press, then BUTTON command will delay +'approximately one second (200 x 5 ms) before auto-repeating at a rate of +'approximately 100 ms (20 x 5 ms). + +Btn PIN 0 +btnWrk VAR Byte + +Init: + PAUSE 200 'short startup-pause + +Main: + 'Try changing the Delay parameter (3rd value) in BUTTON to see the effect: + '0 = no delay; 1-254 = varying delays before auto-repeat; 255 = no auto-repeat + '(only one action per button press) + ' + 'The BUTTON instruction makes the program branch to No_Press unless P0 = 0 + + PAUSE 5 + BUTTON Btn, 0, 200, 20, btnWrk, 0, No_Press + DEBUG "*" + +No_Press: + GOTO Main \ No newline at end of file diff --git a/examples/COUNT.bs2 b/examples/COUNT.bs2 new file mode 100755 index 0000000..ff0780b --- /dev/null +++ b/examples/COUNT.bs2 @@ -0,0 +1,44 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: COUNT +'Connect an active-low button (shown in the BUTTON command description) to pin P0 +'of the BASIC Stamp. The Debug Terminal will prompt you to press the button as +'quickly as possible for a 1-second period. When the count is done, the screen +'will display your "score," the total number of cycles registered by COUNT. Note +'that this score will almost always be greater than the actual number of presses +'because of mechanical switch contact bounce. + +PushBtn PIN 0 'Button on P0 + +Capture CON 1000 '1 second + +#SELECT $STAMP 'Set DurAdj according to module type + #CASE BS2, BS2E + DurAdj CON $100 '/ 1 + #CASE BS2SX + DurAdj CON $280 '/ 0.400 + #CASE BS2P, BS2PX + DurAdj CON $37B '/ 0.287 + #CASE BS2PE + DurAdj CON $163 '/ 0.720 +#ENDSELECT + +cycles VAR Word 'counted cycles + +Init: + PAUSE 200 'short startup-pause + +Main: + DO + DEBUG CLS, "How many times can you press the button in 1 second?", CR + PAUSE 1000 + DEBUG "Ready, set... " + PAUSE 500 + DEBUG "GO!", CR + COUNT PushBtn, (Capture */ DurAdj), cycles + DEBUG CR, "Your score: ", DEC cycles, CR + PAUSE 3000 + DEBUG "Press button to go again." + DO : LOOP UNTIL (PushBtn = 0) 'wait for button press + LOOP \ No newline at end of file diff --git a/examples/DATA.bs2 b/examples/DATA.bs2 new file mode 100755 index 0000000..dc3f7a4 --- /dev/null +++ b/examples/DATA.bs2 @@ -0,0 +1,70 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: DATA +'This program stores a number of large text strings into EEPROM with the DATA +'directive and then sends them, one character at a time via the DEBUG command. +'This is a good demonstration of how to save program space by storing large +'amounts of data in EEPROM directly, rather than embedding the data into separate +'DEBUG commands. + +idx VAR Word 'current location number +phrase VAR Nib 'current phrase number +char VAR Byte 'character to print + +' ----- Define all text phrases (out of order, just for fun!) ----- +' +Text1 DATA "Here is the first part of a large chunk of text " + DATA "data ", CR, "that needs to be transmitted. There's " + DATA "a 5 second delay", CR, "between text paragraphs. ", CR + DATA CR, 0 + +Text3 DATA "The alternative (having multiple DEBUGs or SEROUTs, " + DATA "each ", CR, "with their own line of text) consumes " + DATA "MUCH more EEPROM ", CR, "(program/data) space. ", CR + DATA CR, 0 + +Text6 DATA "The 0 at the end of data blocks is used by this program " + DATA "to indicate", CR, "we've reached the End of Text. The Main " + DATA "routine pauses in-between each", CR, "block of text, " + DATA "and then uses a LOOKUP command to retrieve the location", CR + DATA "of the next desired block of text to print. ", 0 + +Text4 DATA CLS, "This program also demonstrates retrieving data " + DATA "out of order ", CR, "in relation to the way it is " + DATA "stored in EEPROM. Additionally, ", CR, "control codes " + DATA "(like carriage-returns, clear-screens, etc) can ", CR + DATA "be embedded right in the data, as it is here. ", CR + DATA CR, 0 + +Text2 DATA "This is an example of a good way to save space in " + DATA "your ", CR, "BASIC Stamp's program by storing data " + DATA "into EEPROM and ", CR, "retrieving it, one byte at a " + DATA "time, and transmitting it ", CR, "with just a single " + DATA "DEBUG (or SEROUT) command.", CR, CR, 0 + +Text5 DATA "The Print_It routine simply takes the idx variable, " + DATA "retrieves ", CR, "the character at the EEPROM location " + DATA "pointed to by it, and ", CR, "prints it to the screen " + DATA "until it finds a byte with a value of 0.", CR, CR, 0 + +Init: + PAUSE 200 'short startup-pause + +Main: + DEBUG CLS 'Clear DEBUG window + FOR phrase = 1 TO 6 'Print blocks one by one + LOOKUP (phrase - 1), [Text1, Text2, Text3, Text4, Text5, Text6], idx + GOSUB Print_It + PAUSE 5000 'Pause for 5 seconds + NEXT + END + +Print_It: + DO + READ idx, char 'Get next character + idx = idx + 1 'Point to next location + IF (char = 0) THEN EXIT 'If 0, we're done with block + DEBUG char 'Otherwise, transmit it + LOOP + RETURN 'Return to the main routine \ No newline at end of file diff --git a/examples/DEBUG_DEBUGIN.bs2 b/examples/DEBUG_DEBUGIN.bs2 new file mode 100755 index 0000000..8d7d0fa --- /dev/null +++ b/examples/DEBUG_DEBUGIN.bs2 @@ -0,0 +1,23 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: DEBUG_DEBUGIN +'This program demonstrates the ability to accept user input from the +'Debug Terminal, and to accept numeric entry in any valid format. + +myNum VAR Word + +Init: + PAUSE 200 'short startup-pause + +Main: + DO + DEBUG CLS, "Enter a any number: " 'prompt user + DEBUGIN SNUM myNum 'retrieve number in any format + + DEBUG CRSRXY, 0, 2, 'display number in all formats + SDEC ? myNum, + SHEX ? myNum, + SBIN ? myNum + PAUSE 3000 + LOOP 'do it again \ No newline at end of file diff --git a/examples/DO-LOOP.bs2 b/examples/DO-LOOP.bs2 new file mode 100755 index 0000000..c1481f3 --- /dev/null +++ b/examples/DO-LOOP.bs2 @@ -0,0 +1,41 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: DO-LOOP +'This program creates a little guessing game. It starts by creating +'a (psuedo) random number between 1 and 10. The inner loop will run +'until the answer is guessed or 3 tries have been attempted. The +'outer loop has no condition and will cause the inner loop code to +'run until the BASIC Stamp is reprogrammed. + +rVal VAR Word 'random value +answer VAR Byte 'game answer +guess VAR Byte 'player guess +tries VAR Nib 'number of tries + +Init: + PAUSE 200 'short startup-pause + +Main: + DO + RANDOM rVal + answer = rVal.LOWBYTE */ 10 + 1 'create 1 - 10 answer + tries = 0 + + DO 'get answer until out of tries + DEBUG CLS, "Guess a number (1 - 10). ", CLREOL + DEBUGIN DEC guess 'get new guess + tries = tries + 1 'update tries count + IF (guess <> answer) THEN + DEBUG CR, "Wrong." + PAUSE 500 + ENDIF + LOOP UNTIL ((tries = 3) OR (guess = answer)) + + IF (guess = answer) THEN ' test reason for loop end + DEBUG CR, "You got it!" + ELSE + DEBUG CR, "Sorry ... the answer was ", DEC answer, "." + ENDIF + PAUSE 1000 + LOOP ' run again \ No newline at end of file diff --git a/examples/DTMFOUT.bs2 b/examples/DTMFOUT.bs2 new file mode 100755 index 0000000..980b58d --- /dev/null +++ b/examples/DTMFOUT.bs2 @@ -0,0 +1,55 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: DTMFOUT +'This demo program is a rudimentary memory dialer. Since DTMF digits fit +'within a nibble (four bits), the program below packs two DTMF digits into +'each byte of three EEPROM data tables. The end of phone number is marked +'by the nibble $F, since this is not a valid phone-dialing digit. +'Conditional compilation sets the timing adjustment factor so that the +'output will sound the same on any BS2 model. + +Spkr PIN 10 'DTMF output on pin 10 + +#SELECT $STAMP 'Set TmAdj according to module type + #CASE BS2, BS2E, BS2PE + TmAdj CON $100 'x 1.0 (time adjust) + #CASE BS2SX + TmAdj CON $280 'x 2.5 + #CASE BS2P + TmAdj CON $3C5 'x 3.77 + #CASE BS2PX + TmAdj CON $607 'x 6.03 +#ENDSELECT + +eeLoc VAR Byte 'EEPROM address of stored number +eeByte VAR Byte 'Byte containing two DTMF digits +dtDig VAR eeByte.NIB1 'Digit to dial +phone VAR Nib 'Pick a phone # +hiLo VAR Bit 'Bit to select upper and lower nib + +Parallax DATA $19,$16,$62,$48,$33,$3F 'Phone: 1-916-624-8333 +ParallaxFax DATA $19,$16,$62,$48,$00,$3F 'Phone: 1-916-624-8003 +Information DATA $15,$20,$55,$51,$21,$2F 'Phone: 1-520-555-1212 + +Main: + FOR phone = 0 TO 2 + 'retrieve address + LOOKUP phone, [Parallax, ParallaxFax, Information], eeLoc + GOSUB Dial_Number + PAUSE 2000 + NEXT + END + +Dial_Number: + DO + READ eeLoc, eeByte 'Retrieve byte from EEPROM + eeLoc = eeLoc + 1 'point to next pair of digits + FOR hiLo = 0 TO 1 'Dial upper and lower digits + IF (dtDig = $F) THEN EXIT 'Hex $F is end-of-number flag + DTMFOUT Spkr, 'dial digit + 150 */ TmAdj, 25, [dtDig] '150 ms on, 25 ms off + eeByte = eeByte << 4 'Shift in next digit + NEXT + LOOP UNTIL (dtDig = $F) + RETURN diff --git a/examples/EXIT.bs2 b/examples/EXIT.bs2 new file mode 100755 index 0000000..c575a2c --- /dev/null +++ b/examples/EXIT.bs2 @@ -0,0 +1,26 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: EXIT +'This program demonstrates the early termination of DO...LOOP and +'FOR..NEXT loop structures. IF...THEN is used to test a condition, +'and when true, EXIT will terminate the loop. + +col VAR Nib +row VAR Nib + +Setup: + PAUSE 200 'short startup-pause + col = 0 + +Main: + DO WHILE (col < 10) 'attempt 10 iterations + FOR row = 0 TO 15 'attempt 16 iterations + IF (row > 9) THEN EXIT 'terminate when row > 9 + DEBUG CRSRXY, (col * 8), row, 'print col/row at location + DEC col, "/", DEC row, CR + NEXT + col = col + 1 'update column + IF (col = 3) THEN EXIT 'terminate when col = 3 + LOOP + END \ No newline at end of file diff --git a/examples/FOR-NEXT.bs2 b/examples/FOR-NEXT.bs2 new file mode 100755 index 0000000..8089afc --- /dev/null +++ b/examples/FOR-NEXT.bs2 @@ -0,0 +1,24 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: FOR-NEXT +'This example uses a FOR...NEXT loop to churn out a series of sequential +'squares (numbers 1, 2, 3, 4... raised to the second power) by using a +'variable to set the FOR...NEXT's StepValue parameter, and incrementing +'StepValue within the loop. Sir Isaac Newton is generally credited with +'the discovery of this technique. + +square VAR Byte 'FOR/NEXT counter +stepSize VAR Byte 'step size, increase by 2 each loop + +Setup: + PAUSE 200 'short startup-pause + stepSize = 1 + square = 1 + +Main: + FOR square = 1 TO 250 STEP stepSize 'show squares up to 250 + DEBUG DEC ? square 'display on screen + stepSize = stepSize + 2 'add 2 to stepSize + NEXT 'loop until square > 250 + END \ No newline at end of file diff --git a/examples/FREQOUT.bs2 b/examples/FREQOUT.bs2 new file mode 100755 index 0000000..cc0939d --- /dev/null +++ b/examples/FREQOUT.bs2 @@ -0,0 +1,53 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: FREQOUT +'This program demonstrates sound-effects generation by the BASIC Stamp. +'Conditional compilation sets timing and frequency adjustment factors so +'that the output will sound the same on any BS2 model. + +Spkr PIN 10 'output pin for FREQOUT + +#SELECT $STAMP 'Set TmAdj and FrAdj according to module type + #CASE BS2, BS2E + TmAdj CON $100 'x 1.0 (time adjust) + FrAdj CON $100 'x 1.0 (freq adjust) + #CASE BS2SX + TmAdj CON $280 'x 2.5 + FrAdj CON $066 'x 0.4 + #CASE BS2P + TmAdj CON $3C5 'x 3.77 + FrAdj CON $044 'x 0.265 + #CASE BS2PE + TmAdj CON $100 'x 1.0 + FrAdj CON $0A9 'x 0.662 + #CASE BS2PX + TmAdj CON $607 'x 6.03 + FrAdj CON $2A 'x 0.166 +#ENDSELECT + +Init: + PAUSE 200 'short startup-pause + +Main: + DEBUG "Let's make a call...", CR + 'combine 350 Hz & 440 Hz + FREQOUT Spkr, 2000 */ TmAdj, 350 */ FrAdj, 440 */ FrAdj + 'dial number (digits 150 ms on, 25 ms off) + DTMFOUT Spkr, 150 */ TmAdj, 25, [5, 5, 5, 1, 2, 1, 2] + PAUSE 500 + + 'bad connection (SIT sequence) + FREQOUT Spkr, 375 */ TmAdj, 985 */ FrAdj + FREQOUT Spkr, 375 */ TmAdj, 1371 */ FrAdj + FREQOUT Spkr, 375 */ TmAdj, 1777 */ FrAdj + + DEBUG "Oops! -- try again...", CR + PAUSE 1000 + DTMFOUT Spkr, 150 */ TmAdj, 25, [5, 5, 5, 2, 2, 2, 2] + DEBUG "Ringing" + FREQOUT Spkr, 2000 */ TmAdj, 440 */ FrAdj, 480 */ FrAdj + PAUSE 4000 + FREQOUT Spkr, 2000 */ TmAdj, 440 */ FrAdj, 480 */ FrAdj + INPUT Spkr + END diff --git a/examples/GOSUB.bs2 b/examples/GOSUB.bs2 new file mode 100755 index 0000000..cdeee21 --- /dev/null +++ b/examples/GOSUB.bs2 @@ -0,0 +1,44 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: GOSUB +'This program is a guessing game that generates a random number in a +'subroutine called Pick_A_Number. It is written to stop after three +'guesses. To see a common bug associated with GOSUB, delete or comment +'out the line beginning with END after the FOR-NEXT loop. This means +'that after the loop is finished, the program will wander into the +'Pick_A_Number subroutine. When the RETURN at the source's end executes, +'the program will go back to the beginning. This will cause the program +'to execute endlessly. Make sure that your programs can't accidentally +'execute subroutines! + +rounds VAR Byte 'number of reps +numGen VAR Word 'random number holder +myNum VAR Byte 'random number, 1-10 + +Setup: + PAUSE 200 'short startup-pause + numGen = 11500 'initialize random "seed" + +Main: + FOR rounds = 1 TO 3 + DEBUG CLS, "Think of a number from 1 to 10", CR + GOSUB Pick_A_Number + PAUSE 2000 'dramatic pause + DEBUG "My number was: ", DEC myNum 'show the number + PAUSE 1000 'another pause. + NEXT + DEBUG CLS, "Done" + END 'end program + +'Random-number subroutine. A subroutine is just a piece of code with +'the RETURN instruction at the end. Always make sure your program enters +'subroutines with a GOSUB. If you don't, the RETURN won't have the +'correct address, and your program will have a bug! + +Pick_A_Number: + RANDOM numGen 'stir up the bits of NumGen. + DEBUG " (random value = ", DEC numGen, ")", CR, CR + myNum = numGen / 6550 MIN 1 'scale to fit 1-10 range. + RETURN 'go back to instruction after + 'the GOSUB that got us here \ No newline at end of file diff --git a/examples/GOTO.bs2 b/examples/GOTO.bs2 new file mode 100755 index 0000000..85c3407 --- /dev/null +++ b/examples/GOTO.bs2 @@ -0,0 +1,29 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: GOTO +'This program isn't practical at all, but demonstrates the use of GOTO to +'jump around the code. This code jumps between three different routines, +'each of which print something different on the screen. The routines are +'out of order for sake of example. + +Init: + PAUSE 200 'short startup-pause + +Start: + GOTO Routine1 + +Routine2: + DEBUG "We're in routine #2", CR + PAUSE 1000 + GOTO Routine3 + +Routine1: + DEBUG "We're in routine #1", CR + PAUSE 1000 + GOTO Routine2 + +Routine3: + DEBUG "We're in routine #3", CR + PAUSE 1000 + GOTO Routine1 \ No newline at end of file diff --git a/examples/HIGH_LOW.bs2 b/examples/HIGH_LOW.bs2 new file mode 100755 index 0000000..54e5c34 --- /dev/null +++ b/examples/HIGH_LOW.bs2 @@ -0,0 +1,15 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: HIGH_LOW +'This simple program sets I/O pin 0 high for 1/2 second and low for +'1/2 second in an endless loop. Connect an LED to P0 for a simple +'blinker. + +Main: + DO + HIGH 0 + PAUSE 500 + LOW 0 + PAUSE 500 + LOOP diff --git a/examples/I2C.bs2 b/examples/I2C.bs2 new file mode 100755 index 0000000..5c97f5d --- /dev/null +++ b/examples/I2C.bs2 @@ -0,0 +1,59 @@ +'{$STAMP BS2p} +'{$PBASIC 2.5} + +'SOURCE: I2C +'This program demonstrates writing and reading every location in a 24LC16B +'EEPROM using the BS2p/BS2pe's I2C commands. Connect the BS2p, BS2pe, or +'BS2px to the 24LC16B DIP EEPROM as shown in the diagram in the I2CIN OR +'I2COUT command description. + +#IF ($STAMP < BS2P) #THEN 'Notify of module requirements + #ERROR "Program requires BS2p, BS2pe, or BS2px." +#ENDIF + +SDA PIN 0 'I2C SDA pin +SCL PIN SDA + 1 + +addr VAR Word 'internal address +block VAR Nib 'block address in 24LC16 +value VAR Byte 'value to write +check VAR Nib 'for checking retuned values +result VAR Byte(16) 'array for returned value + +Init: + PAUSE 200 'short startup-pause + +Write_To_EEPROM: + DEBUG "Writing...", CR + PAUSE 2000 + FOR addr = 0 TO 2047 STEP 16 'loop through all addresses + block = addr.NIB2 << 1 'calculate block address + value = addr >> 4 'create value from upper 8 bits + 'write 16 bytes + I2COUT SDA, $A0 | block, addr, [REP value\16] + PAUSE 5 + DEBUG "Addr: ", DEC4 addr, "-", DEC4 addr + 15, " ", + "Value: ", DEC3 value, CR + NEXT + PAUSE 2000 + +Read_From_EEPROM: + DEBUG CR, "Reading...", CR + PAUSE 2000 + FOR addr = 0 TO 2047 STEP 16 + block = addr.NIB2 << 1 + value = addr >> 4 + I2CIN SDA, $A1 | block, addr, [STR result\16] + FOR check = 0 TO 15 + IF (result(check) <> value) THEN Error + NEXT + DEBUG "Addr: ", DEC4 addr, "-", DEC4 addr + 15, " ", + "Value: ", DEC3 result, CR + NEXT + PAUSE 100 + DEBUG CR, "All locations passed" + END + +Error: + DEBUG "Error at location: ", DEC4 addr + check, CR, + "Found: ", DEC3 result(check), ", Expected: ", DEC3 value \ No newline at end of file diff --git a/examples/IF-THEN-ELSE.bs2 b/examples/IF-THEN-ELSE.bs2 new file mode 100755 index 0000000..4321788 --- /dev/null +++ b/examples/IF-THEN-ELSE.bs2 @@ -0,0 +1,36 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: IF-THEN-ELSE +'This program is an expansion on IF-THEN. It generates a series of 16-bit +'random numbers and tests each to determine whether they're evenly divisible +'by 3. If a number is evenly divisible by 3, then it is printed, otherwise, +'the program generates another random number. The program quits when it's +'printed 10 numbers. It also displays the tallied up Hits, Misses, and Samples. + +sample VAR Word 'Random number to be tested +hits VAR Nib 'Number of hits +misses VAR Word 'Number of misses + +Setup: + PAUSE 200 'short startup-pause + sample = 11500 + +Main: + DO + RANDOM sample 'Put a random number into sample + IF ((sample // 3) = 0) THEN 'divisible by 3? + DEBUG DEC5 sample, '- yes, print value and message + " is divisible by 3", CR + hits = hits + 1 'count hits (divisble by 3) + ELSE + misses = misses + 1 'count misses + ENDIF + LOOP UNTIL (hits = 10) 'quit after 10 hits + + DEBUG CR, + "All done.", CR, CR, 'display results + "Hits: ", DEC hits, CR, + "Misses: ", DEC misses, CR, + "Samples: ", DEC (hits + misses) + END \ No newline at end of file diff --git a/examples/IF-THEN.bs2 b/examples/IF-THEN.bs2 new file mode 100755 index 0000000..6542227 --- /dev/null +++ b/examples/IF-THEN.bs2 @@ -0,0 +1,30 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: IF-THEN +'The program below generates a series of 16-bit random numbers and tests +'each to determine whether they're evenly divisible by 3. If a number is +'evenly divisible by 3, then it is printed, otherwise, the program generates +'another random number. The program quits when it's printed 10 numbers. + +sample VAR Word 'Random number to be tested +samps VAR Nib 'Number of samples taken +temp VAR Nib 'Temporary workspace + + +Setup: + PAUSE 200 'short startup-pause + sample = 11500 + +Mult3: + RANDOM sample 'Put a random number into sample + temp = sample // 3 + IF temp <> 0 THEN Mult3 'Not multiple of 3? -- try again + DEBUG DEC5 sample, " divides by 3", CR + samps = samps + 1 'Count multiples of 3 + IF samps = 10 THEN Done 'Quit with 10 samples + GOTO Mult3 'keep checking + +Done: + DEBUG CR, "All done." + END \ No newline at end of file diff --git a/examples/INPUT_OUTPUT.bs2 b/examples/INPUT_OUTPUT.bs2 new file mode 100755 index 0000000..211fcec --- /dev/null +++ b/examples/INPUT_OUTPUT.bs2 @@ -0,0 +1,30 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: INPUT_OUTPUT +'This program demonstrates how the input/output direction of a pin is +'determined by the corresponding bit of DIRS. It also shows that the +'state of the pin itself (as reflected by the corresponding bit of INx) +'is determined by the outside world when the pin is an input, and by the +'corresponding bit of INx when it's an output. To set up the demo, +'connect a 10k resistor from +5V to P7 on the BASIC Stamp. The resistor +'to +5V puts a high (1) on the pin when it's an input. The BASIC Stamp +'can override this state by writing a low (0) to bit 7 of OUTS and +'changing the pin to output. + +Init: + PAUSE 200 'short startup-pause + +Main: + INPUT 7 'Make P7 an input + DEBUG "State of P7: ", + BIN1 IN7, CR + + OUT7 = 0 'Write 0 to output latch + DEBUG "After 0 written to OUT7: ", + BIN1 IN7, CR + + OUTPUT 7 'Make P7 an output + DEBUG "After P7 changed to output: ", + BIN1 IN7 + END \ No newline at end of file diff --git a/examples/LCDCMD.bs2 b/examples/LCDCMD.bs2 new file mode 100755 index 0000000..cd33d98 --- /dev/null +++ b/examples/LCDCMD.bs2 @@ -0,0 +1,48 @@ +'{$STAMP BS2p} +'{$PBASIC 2.5} + +'SOURCE: LCDCMD +'This program demonstrates initialization and printing on a 2 x 16 +'character LCD display. The set of "LCD constants", below, are provided +'as pre-defined and useful LCD commands, though not all are actually +'used in this program. + +#IF ($stamp < BS2P) #THEN 'Notify of module requirements + #ERROR "Program requires BS2p, BS2pe, or BS2px." +#ENDIF + +Lcd PIN 0 + +LcdCls CON $01 'clear the LCD +LcdHome CON $02 'move cursor home +LcdCrsrL CON $10 'move cursor left +LcdCrsrR CON $14 'move cursor right +LcdDispL CON $18 'shift chars left +LcdDispR CON $1C 'shift chars right +LcdDDRam CON $80 'Display Data RAM +LcdCGRam CON $40 'Character Generator RAM +LcdLine1 CON $80 'DDRAM address of line 1 +LcdLine2 CON $C0 'DDRAM address of line 2 + + +Init_LCD: + PAUSE 1000 'allow LCD to self-initialize first + LCDCMD Lcd, %00110000 'send wakeup sequence to LCD + PAUSE 5 'pause required by LCD specs + LCDCMD Lcd, %00110000 + PAUSE 0 'pause required by LCD specs + LCDCMD Lcd, %00110000 + PAUSE 0 'pause required by LCD specs + LCDCMD Lcd, %00100000 'set data bus to 4-bit mode + LCDCMD Lcd, %00101000 'set to 2-line mode with 5x8 font + LCDCMD Lcd, %00001100 'display on without cursor + LCDCMD Lcd, %00000110 'auto-increment cursor + +Main: + DO + LCDOUT Lcd, LcdCls, ["Hello, World!"] + LCDOUT Lcd, LcdLine2, ["How are you?"] + PAUSE 3000 + LCDCMD Lcd, LcdCls + PAUSE 500 + LOOP \ No newline at end of file diff --git a/examples/LCDIN.bs2 b/examples/LCDIN.bs2 new file mode 100755 index 0000000..caf3931 --- /dev/null +++ b/examples/LCDIN.bs2 @@ -0,0 +1,54 @@ +'{$STAMP BS2p} +'{$PBASIC 2.5} + +'SOURCE: LCDIN +'This program demonstrates initialization, printing and reading +'from a 2 x 16 character LCD display. + +#IF ($STAMP < BS2P) #THEN 'Notify of module requirements + #ERROR "Program requires BS2p, BS2pe or BS2px." +#ENDIF + +Lcd PIN 0 + +LcdCls CON $01 'clear the LCD +LcdHome CON $02 'move cursor home +LcdCrsrL CON $10 'move cursor left +LcdCrsrR CON $14 'move cursor right +LcdDispL CON $18 'shift chars left +LcdDispR CON $1C 'shift chars right +LcdDDRam CON $80 'Display Data RAM +LcdCGRam CON $40 'Character Generator RAM +LcdLine1 CON $80 'DDRAM address of line 1 +LcdLine2 CON $C0 'DDRAM address of line 2 + +char VAR Byte(16) + +Init_LCD: + PAUSE 1000 'allow LCD to self-initialize first + LCDCMD Lcd, %00110000 'send wakeup sequence to LCD + PAUSE 5 'pause required by LCD specs + LCDCMD Lcd, %00110000 + PAUSE 0 'pause required by LCD specs + LCDCMD Lcd, %00110000 + PAUSE 0 'pause required by LCD specs + LCDCMD Lcd, %00100000 'set data bus to 4-bit mode + LCDCMD Lcd, %00101000 'set to 2-line mode with 5x8 font + LCDCMD Lcd, %00001100 'display on without cursor + LCDCMD Lcd, %00000110 'auto-increment cursor + +Main: + DO + LCDOUT Lcd, LcdCls, ["Hello!"] + GOSUB Read_LCD_Screen + PAUSE 3000 + LCDOUT Lcd, LcdCls, ["I'm a 2x16 LCD!"] + GOSUB Read_LCD_Screen + PAUSE 3000 + LOOP + +Read_LCD_Screen: + DEBUG "LCD now says: " + LCDIN Lcd, LcdLine1, [STR char\16] + DEBUG STR char\16, CR + RETURN \ No newline at end of file diff --git a/examples/LCDINIT.bs2 b/examples/LCDINIT.bs2 new file mode 100755 index 0000000..f7262e5 --- /dev/null +++ b/examples/LCDINIT.bs2 @@ -0,0 +1,48 @@ +'{$STAMP BS2p} +'{$PBASIC 2.5} + +'SOURCE: LCDINIT +'This program demonstrates initialization and printing on a 2 x 16 +'character LCD display. The set of "LCD constants", below, are provided +'as pre-defined and useful LCD commands, though not all are actually +'used in this program. + +#IF ($STAMP < BS2P) #THEN 'Notify of module requirements + #ERROR "Program requires BS2p, BS2pe or BS2px." +#ENDIF + +Lcd PIN 0 + +LcdCls CON $01 'clear the LCD +LcdHome CON $02 'move cursor home +LcdCrsrL CON $10 'move cursor left +LcdCrsrR CON $14 'move cursor right +LcdDispL CON $18 'shift chars left +LcdDispR CON $1C 'shift chars right +LcdDDRam CON $80 'Display Data RAM +LcdCGRam CON $40 'Character Generator RAM +LcdLine1 CON $80 'DDRAM address of line 1 +LcdLine2 CON $C0 'DDRAM address of line 2 + + +Init_LCD: + PAUSE 1000 'allow LCD to self-initialize first + LCDCMD Lcd, %00110000 'send wakeup sequence to LCD + PAUSE 5 'pause required by LCD specs + LCDCMD Lcd, %00110000 + PAUSE 0 'pause required by LCD specs + LCDCMD Lcd, %00110000 + PAUSE 0 'pause required by LCD specs + LCDCMD Lcd, %00100000 'set data bus to 4-bit mode + LCDCMD Lcd, %00101000 'set to 2-line mode with 5x8 font + LCDCMD Lcd, %00001100 'display on without cursor + LCDCMD Lcd, %00000110 'auto-increment cursor + +Main: + DO + LCDOUT Lcd, LcdCls, ["Hello, World!"] + LCDOUT Lcd, LcdLine2, ["How are you?"] + PAUSE 3000 + LCDCMD Lcd, LcdCls + PAUSE 500 + LOOP \ No newline at end of file diff --git a/examples/LCDOUT.bs2 b/examples/LCDOUT.bs2 new file mode 100755 index 0000000..87aba2c --- /dev/null +++ b/examples/LCDOUT.bs2 @@ -0,0 +1,52 @@ +'{$STAMP BS2p} +'{$PBASIC 2.5} + +'SOURCE: LCDOUT +'This program demonstrates initialization and printing on a 2x16 +'character LCD display. + +#IF ($STAMP < BS2P) #THEN 'Notify of module requirements + #ERROR "Program requires BS2p, BS2pe or BS2px." +#ENDIF + +Lcd PIN 0 + +LcdCls CON $01 'clear the LCD +LcdHome CON $02 'move cursor home +LcdCrsrL CON $10 'move cursor left +LcdCrsrR CON $14 'move cursor right +LcdDispL CON $18 'shift chars left +LcdDispR CON $1C 'shift chars right +LcdDDRam CON $80 'Display Data RAM +LcdCGRam CON $40 'Character Generator RAM +LcdLine1 CON $80 'DDRAM address of line 1 +LcdLine2 CON $C0 'DDRAM address of line 2 + + +Init_LCD: + PAUSE 1000 'allow LCD to self-initialize first + LCDCMD Lcd, %00110000 'send wakeup sequence to LCD + PAUSE 5 'pause required by LCD specs + LCDCMD Lcd, %00110000 + PAUSE 0 'pause required by LCD specs + LCDCMD Lcd, %00110000 + PAUSE 0 'pause required by LCD specs + LCDCMD Lcd, %00100000 'set data bus to 4-bit mode + LCDCMD Lcd, %00101000 'set to 2-line mode with 5x8 font + LCDCMD Lcd, %00001100 'display on without cursor + LCDCMD Lcd, %00000110 'auto-increment cursor + + LCDOUT Lcd, LcdCGRam, 'load custom character map + [$00, $0A, $0A, $00, $11, $0E, $06, $00] + +Main: + DO + LCDOUT Lcd, LcdCls, ["Hello my friend."] + PAUSE 750 + LCDOUT Lcd, LcdLine2, ["How are you?"] + PAUSE 1500 + LCDCMD Lcd, LcdCls + LCDOUT Lcd, LcdLine1 + 1, ["I'm doing just"] + LCDOUT Lcd, LcdLine2 + 4, ["fine! ", 0] + PAUSE 2000 + LOOP \ No newline at end of file diff --git a/examples/LOOKDOWN.bs2 b/examples/LOOKDOWN.bs2 new file mode 100755 index 0000000..82c72b5 --- /dev/null +++ b/examples/LOOKDOWN.bs2 @@ -0,0 +1,26 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: LOOKDOWN +'This program uses LOOKDOWN to determine the number of decimal digits in +'a number. Since LOOKDOWN uses a zero-indexed table, the output will be +'the number of digits minus one, so the DEBUG statement corrects for this. + +aNum VAR Word 'random number +stpSz VAR Word 'FOR-NEXT step size +numDig VAR Nib 'digits in aNum + +Setup: + PAUSE 200 'short startup-pause + stpSz = 2 + +Main: + FOR aNum = 0 TO 15000 STEP stpSz + LOOKDOWN aNum, <[0, 10, 100, 1000, 10000, 65535], numDig + 'right-justify output + DEBUG "aNum = ", REP " "\(5-numDig), DEC aNum, TAB, + "Digits = ", DEC numDig, CR + PAUSE 150 + LOOKUP numDig, [2, 2, 5, 25, 250, 500, 1000], stpSz + NEXT + END \ No newline at end of file diff --git a/examples/LOOKUP.bs2 b/examples/LOOKUP.bs2 new file mode 100755 index 0000000..fa3c6ec --- /dev/null +++ b/examples/LOOKUP.bs2 @@ -0,0 +1,22 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: LOOKUP +'This program uses LOOKUP to create a Debug Terminal animation of a spinning +'propeller. The animation consists of the four ASCII characters | / - \ +'which, when printed rapidly in order at a fixed location, appears to spin. +'A little imagination helps a lot here.... + +idx VAR Nib +frame VAR Byte + +Init: + PAUSE 200 'short startup-pause + +Spinner: + DO + LOOKUP idx, ["|/-\"], frame 'lookup current frame character + DEBUG HOME, "Spinner: ", frame 'display + PAUSE 150 'pause between frames + idx = idx + 1 // 4 'update frame index (0..3) + LOOP 'loop forever diff --git a/examples/NAP.bs2 b/examples/NAP.bs2 new file mode 100755 index 0000000..e49d8f0 --- /dev/null +++ b/examples/NAP.bs2 @@ -0,0 +1,17 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: NAP +'This program lights an LED setting pin 0 low. This completes the circuit +'from +5V, through the LED and resistor, to ground. During the NAP interval, +'the LED stays lit, but blinks off for a fraction of a second. This blink +'is caused by the NAP wake-up mechanism. During wake-up, all pins briefly +'slip into input mode, effectively disconnecting them from loads. + +Setup: + LOW 0 'turn LED on + +Snooze: + DO + NAP 4 'nap for fraction of a second + LOOP \ No newline at end of file diff --git a/examples/ON-GOSUB.bs2 b/examples/ON-GOSUB.bs2 new file mode 100755 index 0000000..d891527 --- /dev/null +++ b/examples/ON-GOSUB.bs2 @@ -0,0 +1,33 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: ON-GOSUB +'This program demonstrates a simple task manager that can be used in +'a variety of applications. It is particularly useful in robotics and +'industrial applications. The advantage of this design is that task +'code routines may be called from other places in the program, including +'other tasks, and the overal program flow is maintained. + +task VAR Nib + +Init: + PAUSE 200 'short startup-pause + +Main: + DO + ON task GOSUB Task_0, Task_1, Task_2 'run current task + task = task + 1 // 3 'update task pointer + PAUSE 1000 + LOOP + +Task_0: + DEBUG "Running Task 0", CR + RETURN + +Task_1: + DEBUG "Running Task 1", CR + RETURN + +Task_2: + DEBUG "Running Task 2", CR + RETURN \ No newline at end of file diff --git a/examples/ON-GOTO.bs2 b/examples/ON-GOTO.bs2 new file mode 100755 index 0000000..0889d3c --- /dev/null +++ b/examples/ON-GOTO.bs2 @@ -0,0 +1,33 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: ON-GOTO +'This program shows how the value of an index variable (idx) controls the +'destination of the ON...GOTO instruction. + +idx VAR Byte + +Init: + PAUSE 200 'short startup-pause + +Main: + DEBUG "idx: ", DEC idx, " " + ON idx GOTO Case_0, Case_1, Case_2 'if idx = 0..2 goto label + DEBUG "ON..GOTO target error.", CR 'message if idx is out of range + +Update: + idx = idx + 1 // 4 'force idx to be 0..3 + PAUSE 1000 + GOTO Main + +Case_0: + DEBUG "Running Case_0 routine", CR + GOTO Update + +Case_1: + DEBUG "Running Case_1 routine", CR + GOTO Update + +Case_2: + DEBUG "Running Case_2 routine", CR + GOTO Update \ No newline at end of file diff --git a/examples/OWIN_OWOUT.bs2 b/examples/OWIN_OWOUT.bs2 new file mode 100755 index 0000000..310d82a --- /dev/null +++ b/examples/OWIN_OWOUT.bs2 @@ -0,0 +1,63 @@ +'{$STAMP BS2p} +'{$PBASIC 2.5} + +'SOURCE: OWIN_OWOUT +'This program demonstrates interfacing to a Dallas Semiconductor DS1822 +'1-Wire Digital Thermometer chip using the BS2p's 1-Wire commands. Connect +'the BS2p, BS2pe, or BS2px to the DS1822 as shown in the diagram in the +'OWIN or OWOUT command description. This program uses a simplified +'approach that ignores the fractional portion of the temperature. + +#IF ($STAMP < BS2P) #THEN 'Notify of module requirements + #ERROR "Program requires BS2p, BS2pe or BS2px." +#ENDIF + +DQ PIN 0 '1-Wire buss pin + +RdROM CON $33 'read serial number +MatchROM CON $55 'match SN -- for multiple devices +SkipROM CON $CC 'ignore SN -- use for one device +CvrtTmp CON $44 'start temperature conversion +RdSP CON $BE 'read DS1822 scratch pad + +tempIn VAR Word 'raw temperature +sign VAR tempIn.BIT11 '1 = negative temperature +tLo VAR tempIn.BYTE0 +tHi VAR tempIn.BYTE1 +tSign VAR Bit 'saved sign bit +tempC VAR Word 'final Celsius temp +tempF VAR Word 'final Fahrenheit temp + +Init: + PAUSE 200 'short startup-pause + +Main: + DO + GOSUB Get_Temperature 'read temperature from DS1822 + DEBUG HOME, 'display + "DS1822", CR, + "------", CR, + SDEC tempC, " C ", CR, + SDEC tempF, " F " + PAUSE 1000 + LOOP + END + +Get_Temperature: + OWOUT DQ, 1, [SkipROM, CvrtTmp] 'send convert temperature command + DO 'wait on conversion + PAUSE 25 'small loop pad + OWIN DQ, 4, [tempIn] 'check status (bit transfer) + LOOP UNTIL (tempIn) '1 when complete + OWOUT DQ, 1, [SkipROM, RdSP] 'read DS1822 scratch pad + OWIN DQ, 2, [tLo, tHi] 'get raw temp data + tSign = sign 'save sign bit + tempC = tempIn >> 4 'round to whole degrees + tempC.BYTE1 = $FF * tSign 'correct twos complement bits + tempF = (ABS tempC) * 9 / 5 'start F conversion + IF (tSign) THEN 'finish F conversion + tempF = 32 - tempF 'C was negative + ELSE + tempF = tempF + 32 'C was positive + ENDIF + RETURN \ No newline at end of file diff --git a/examples/PAUSE.bs2 b/examples/PAUSE.bs2 new file mode 100755 index 0000000..de9360d --- /dev/null +++ b/examples/PAUSE.bs2 @@ -0,0 +1,15 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: PAUSE +'This program demonstrates the PAUSE command's time delays. Once a second, +'the program will put the message, "Paused..." on the screen. + +Init: + PAUSE 200 'short startup-pause + +Main: + DO + DEBUG "Paused...", CR + PAUSE 1000 + LOOP \ No newline at end of file diff --git a/examples/PULSIN.bs2 b/examples/PULSIN.bs2 new file mode 100755 index 0000000..d007d2b --- /dev/null +++ b/examples/PULSIN.bs2 @@ -0,0 +1,42 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: PULSIN +'This program uses PULSIN to measure a pulse generated by discharging a +'0.1 uF capacitor through a 1K resistor. Pressing the switch generates +'the pulse, which should ideally be approximately 120 us (60 PULSIN units +'of 2 us) long (for BS2 and BS2e). Variations in component values may +'produce results that are up to 10 units off from this value. For more +'information on calculating resistor-capacitor timing, see the RCTIME +'command. + +Pulse PIN 7 'pulse input pin + +#SELECT $STAMP 'Set Scale according to module type + #CASE BS2, BS2E, BS2PE + Scale CON $200 '2.0 us per unit + #CASE BS2SX + Scale CON $0CC '0.8 us per unit + #CASE BS2P + Scale CON $0C0 '0.75 us per unit + #CASE BS2PX + Scale CON $0CF '0.81 us per unit +#ENDSELECT + +time VAR Word + +Init: + PAUSE 200 'short startup-pause + +Main: + DO + PULSIN Pulse, 1, time 'measure positive pulse + IF (time > 0) THEN 'if not 0 + DEBUG HOME, DEC time, " units ", CLREOL 'display raw input + time = time */ Scale 'adjust for Stamp + DEBUG CR, DEC time, " us " 'display microseconds + ELSE + DEBUG CLS, "Out of Range" 'else error message + ENDIF + PAUSE 200 + LOOP diff --git a/examples/PULSOUT.bs2 b/examples/PULSOUT.bs2 new file mode 100755 index 0000000..6a0c255 --- /dev/null +++ b/examples/PULSOUT.bs2 @@ -0,0 +1,25 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: PULSOUT +'This program blinks an LED on for 25 ms at 1-second intervals. Connect an +'LED (active-low) to I/O pin 0. + +#SELECT $STAMP 'Set Scale according to module type + #CASE BS2, BS2E, BS2PE + Scale CON 500 'to ms for 2 us per unit + #CASE BS2SX, BS2P, BS2PX + Scale CON 1250 'to ms for 0.8 us per unit +#ENDSELECT + +Flash CON 25 * Scale '25 milliseconds + +Setup: + PAUSE 200 'short startup-pause + HIGH 0 'make P0 high (LED off) + +Main: + DO + PULSOUT 0, Flash 'flash LED + PAUSE 1000 'one second delay + LOOP \ No newline at end of file diff --git a/examples/PWM.bs2 b/examples/PWM.bs2 new file mode 100755 index 0000000..66eca9e --- /dev/null +++ b/examples/PWM.bs2 @@ -0,0 +1,31 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: PWM +'Connect a voltmeter (such as a digital multimeter set to its voltage +'range) to the output of the circuit shown in the figure for the PWM +'command. Run the program and observe the readings on the meter. They +'should come very close to 1.96V, then decrease slightly as the capacitor +'discharges. Try varying the interval between PWM bursts (by changing the +'PAUSE value) and the number of PWM cycles to see their effect. + +#SELECT $stamp 'Set CycAdj according to module type + #CASE BS2, BS2E + CycAdj CON $100 'x 1.0, cycle adjustment (for ms) + #CASE BS2SX + CycAdj CON $280 'x 2.5 + #CASE BS2P + CycAdj CON $187 'x 1.53 + #CASE BS2PE + CycAdj CON $09E 'x 0.62 + #CASE BS2PX + CycAdj CON $280 'x 2.5 +#ENDSELECT + +Cycles CON 50 + +Main: + DO + PWM 0, 100, (Cycles */ CycAdj) 'PWM at 100/255 duty (~50 ms) + PAUSE 1000 'wait one second + LOOP \ No newline at end of file diff --git a/examples/RANDOM.bs2 b/examples/RANDOM.bs2 new file mode 100755 index 0000000..4da54dd --- /dev/null +++ b/examples/RANDOM.bs2 @@ -0,0 +1,41 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: RANDOM +'This program uses RANDOM to simulate a coin toss. After 100 trials, it reports +'the total number of heads and tails thrown. Connect a button to I/O pin 7 as +'shown in the RANDOM command description and run this program. + +Btn PIN 7 'button input + +flip VAR Word 'a random number +coin VAR flip.BIT0 'Bit0 of the random number +trials VAR Byte 'number of flips +heads VAR Byte 'throws that come up heads +tails VAR Byte 'throws that come up tails +btnWrk VAR Byte 'workspace for BUTTON + +Init: + PAUSE 200 'short startup-pause + +Start: + DEBUG CLS, "Press button to start" + +Main: + FOR trials = 1 TO 100 'flip coin 100 times + +Hold: + RANDOM flip 'randomize while waiting + BUTTON Btn, 0, 250, 100, btnWrk, 0, Hold 'wait for button press + IF (coin = 0) THEN '0 = heads, 1 = tails + DEBUG CR, "Heads!" + heads = heads + 1 'increment heads counter + ELSE + DEBUG CR, "Tails..." + tails = tails + 1 'increment tails counter + ENDIF + NEXT + +Done: + DEBUG CR, CR, "Heads: ", DEC heads, " Tails: ", DEC tails + END \ No newline at end of file diff --git a/examples/RCTIME1.bs2 b/examples/RCTIME1.bs2 new file mode 100755 index 0000000..fb540ea --- /dev/null +++ b/examples/RCTIME1.bs2 @@ -0,0 +1,25 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: RCTIME1 +'This program shows the standard use of the RCTIME command measuring an +'RC charge/discharge time. Use the circuit in the RCTIME description +'(in the manual) with R = 10K pot and C = 0.1 uF. Connect the circuit to +'pin 7 and run the program. Adjust the pot and watch the value change in +'the Debug Terminal. + +RC PIN 7 + +result VAR Word + +Init: + PAUSE 200 'short startup-pause + +Main: + DO + HIGH RC 'charge the cap + PAUSE 1 ' for 1 ms + RCTIME RC, 1, result 'measure RC discharge time + DEBUG HOME, DEC result 'display value + PAUSE 50 + LOOP \ No newline at end of file diff --git a/examples/RCTIME2.bs2 b/examples/RCTIME2.bs2 new file mode 100755 index 0000000..8a9e9f6 --- /dev/null +++ b/examples/RCTIME2.bs2 @@ -0,0 +1,36 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: RCTIME2 +'This program illustrates the use of RCTIME as a fast stopwatch. The +'program energizes a relay coil, then measures how long it takes for the +'relay contacts to close. Use the circuit found in the RCTIME description. +'Note that RCTIME doesn't start timing instantly -- as with all PBASIC +'commands, it must be fetched from the program's EEPROM before executeing. + +Coil PIN 6 +RC PIN 7 + +#SELECT $STAMP 'Set Adjust according to module type + #CASE BS2, BS2E, BS2PE + Adjust CON $200 'x 2 us per unit + #CASE BS2SX + Adjust CON $0CC 'x 0.8 us per unit + #CASE BS2P, BS2PX + Adjust CON $0C0 'x 0.75 us per unit +#ENDSELECT + +result VAR Word + +Init: + PAUSE 200 'short startup-pause + +Main: + DO + LOW Coil 'energize relay coil + RCTIME RC, 1, result 'measure time to contact closure + result = result */ Adjust 'adjust for device + DEBUG "Time to close: ", DEC Result, CR + HIGH Coil 'release relay + PAUSE 1000 'wait one second + LOOP \ No newline at end of file diff --git a/examples/READ.bs2 b/examples/READ.bs2 new file mode 100755 index 0000000..27933cf --- /dev/null +++ b/examples/READ.bs2 @@ -0,0 +1,30 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: READ +'This program reads a string of data stored in EEPROM. The EEPROM data is +'downloaded to the BS2 at compile-time and remains there (even with the +'power off) until overwritten. Put ASCII characters into EEPROM, followed +'by 0, which will serve as the end-of-message marker. + +strAddr VAR Word +char VAR Byte + +Msg1 DATA "BS2", CR, "EEPROM Storage!", 0 + +Init: + PAUSE 200 'short startup-pause + +Main: + strAddr = Msg1 'set to start of message + GOSUB String_Out + END + +String_Out: + DO + READ strAddr, char 'read byte from EEPROM + strAddr = strAddr + 1 'point to next character + IF (char = 0) THEN EXIT 'if 0, exit routine + DEBUG char 'otherwise print char + LOOP + RETURN \ No newline at end of file diff --git a/examples/RETURN.bs2 b/examples/RETURN.bs2 new file mode 100755 index 0000000..5eaa53f --- /dev/null +++ b/examples/RETURN.bs2 @@ -0,0 +1,29 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: RETURN +'This program demonstrates a potential bug caused by allowing a program +'to "fall into" a subroutine. The program was intented to indicate that it +'is "Starting...", then "Executing Subroutine,", then "Returned..." from +'the subroutine and then stop. Since we left out the END command (indicated +'in the comments), the program then falls into the subroutine, displays the +'message "Executing..." again and then RETURNs to the start of the program and +'runs continuously in an endless loop. + +Init: + PAUSE 200 'short startup-pause + +Reset: + DEBUG "Starting Program", CR 'show start-up + +Main: + PAUSE 1000 + GOSUB Demo_Sub 'call the subroutine + PAUSE 1000 + DEBUG "Returned from Subroutine", CR 'show that we're back + PAUSE 1000 + '<-- Forgot to put END here + +Demo_Sub: + DEBUG " Executing Subroutine", CR 'show subroutine activity + RETURN \ No newline at end of file diff --git a/examples/REVERSE.bs2 b/examples/REVERSE.bs2 new file mode 100755 index 0000000..3dd72b0 --- /dev/null +++ b/examples/REVERSE.bs2 @@ -0,0 +1,24 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: REVERSE +'Connect the circuit shown in the REVERSE command description to I/O pin +'0 and run this program. The LED will alternate between two states, dim +'and bright. The BASIC Stamp is using the REVERSE command to toggle I/O +'pin 0 between input and output states. When pin 0 is an input, current +'flows through R1, through the LED, through R2 to ground. Pin 0 is +'effectively disconnected and doesn't play a part in the circuit. The total +'resistance encountered by current flowing through the LED is R1 + R2 = 1220 +'ohms. When pin 0 is reversed to an output, current flows through R1, through +'the LED, and into pin 0 to ground (because of the 0 written to OUT0). The +'total resistance encountered by current flowing through the LED is R1, +'220 ohms. With only 20% of the resistance, the LED glows brighter. + +Setup: + OUT0 = 0 'Put a low in the pin 0 + ' output driver +Main: + DO + PAUSE 250 '1/4th second pause + REVERSE 0 'reverse pin 0 I/O direction + LOOP 'do forever \ No newline at end of file diff --git a/examples/SELECT-CASE.bs2 b/examples/SELECT-CASE.bs2 new file mode 100755 index 0000000..d6f82e2 --- /dev/null +++ b/examples/SELECT-CASE.bs2 @@ -0,0 +1,51 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: SELECT-CASE +'This program generates a series of 16-bit random numbers and tests each +'to determine odd or even, and where it falls in the possible range: +'lower third, middle third, or upper third. The program is useful for +'testing various seed values for RANDOM. + +test VAR Byte 'counter for tests +sample VAR Word 'random number to be tested +odd VAR Byte 'odd throws +even VAR Byte 'even throws +isLo VAR Byte 'sample in lower third +isMid VAR Byte ' in middle thrid +isHi VAR Byte ' in upper third + +Init: + PAUSE 200 'short startup-pause + +Main: + sample = 11000 'initialize seed + FOR test = 1 TO 100 '"throw" 100 times + RANDOM sample 'randomize + + IF (sample.BIT0) THEN 'check odd/even bit + odd = odd + 1 'increment odd count + ELSE + even = even + 1 'increment even count + ENDIF + + SELECT sample + CASE <= 21845 'test lower third + isLo = isLo + 1 + + CASE 21846 TO 43691 'test middle third + isMid = isMid + 1 + + CASE ELSE 'otherwise upper third + isHi = isHi + 1 + ENDSELECT + NEXT + +Show_Results: + DEBUG CLS, + "Odd Throws.... ", DEC odd, "%", CR, + "Even Throws... ", DEC even, "%", CR, + "Low........... ", DEC isLo, "%", CR, + "Mid........... ", DEC isMid, "%", CR, + "High.......... ", DEC isHi, "%", CR + END \ No newline at end of file diff --git a/examples/SERIN_SEROUT1.bs2 b/examples/SERIN_SEROUT1.bs2 new file mode 100755 index 0000000..c332a17 --- /dev/null +++ b/examples/SERIN_SEROUT1.bs2 @@ -0,0 +1,49 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: SERIN_SEROUT1 +'Using two BS2-IC's, connect the circuit shown in the SERIN command +'description and run this program on the BASIC Stamp designated as the +'Sender. This program demonstrates the use of Flow Control (FPin). +'Without flow control, the sender would transmit the whole word "Hello!" +'in about 1.5 ms. The receiver would catch the first byte at most; by the +'time it got back from the first 1-second PAUSE, the rest of the data +'would be long gone. With flow control, communication is flawless since +'the sender waits for the receiver to catch up. + +SO PIN 1 'serial output +FC PIN 0 'flow control pin + +#SELECT $STAMP 'Set values according to module type + #CASE BS2, BS2E, BS2PE + T1200 CON 813 + T2400 CON 396 + T9600 CON 84 + T19K2 CON 32 + T38K4 CON 6 + #CASE BS2SX, BS2P + T1200 CON 2063 + T2400 CON 1021 + T9600 CON 240 + T19K2 CON 110 + T38K4 CON 45 + #CASE BS2PX + T1200 CON 3313 + T2400 CON 1646 + T9600 CON 396 + T19K2 CON 188 + T38K4 CON 84 +#ENDSELECT + +Inverted CON $4000 +Open CON $8000 +Baud CON T9600 + Inverted + +Init: + PAUSE 200 'short startup-pause + +Main: + DO + SEROUT SO\FC, Baud, ["Hello!", CR] 'send the greeting + PAUSE 2500 'wait 2.5 seconds + LOOP 'repeat forever \ No newline at end of file diff --git a/examples/SERIN_SEROUT2.bs2 b/examples/SERIN_SEROUT2.bs2 new file mode 100755 index 0000000..42adbb3 --- /dev/null +++ b/examples/SERIN_SEROUT2.bs2 @@ -0,0 +1,52 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: SERIN_SEROUT2 +'Using two BS2-IC's, connect the circuit shown in the SERIN command +'description and run this program on the BASIC Stamp designated as the +'Receiver. This program demonstrates the use of Flow Control (FPin). +'Without flow control, the sender would transmit the whole word "Hello!" +'in about 1.5 ms. The receiver would catch the first byte at most; by the +'time it got back from the first 1-second PAUSE, the rest of the data +'would be long gone. With flow control, communication is flawless since +'the sender waits for the receiver to catch up. + +SI PIN 1 'serial input +FC PIN 0 'flow control pin + +#SELECT $STAMP 'Set values according to module type + #CASE BS2, BS2E, BS2PE + T1200 CON 813 + T2400 CON 396 + T9600 CON 84 + T19K2 CON 32 + T38K4 CON 6 + #CASE BS2SX, BS2P + T1200 CON 2063 + T2400 CON 1021 + T9600 CON 240 + T19K2 CON 110 + T38K4 CON 45 + #CASE BS2PX + T1200 CON 3313 + T2400 CON 1646 + T9600 CON 396 + T19K2 CON 188 + T38K4 CON 84 +#ENDSELECT + +Inverted CON $4000 +Open CON $8000 +Baud CON T9600 + Inverted + +letter VAR Byte + +Init: + PAUSE 200 'short startup-pause + +Main: + DO + SERIN SI\FC, Baud, [letter] 'receive one byte + DEBUG letter 'display on screen + PAUSE 1000 'wait one second + LOOP 'repeat forever \ No newline at end of file diff --git a/examples/SHIFTIN.bs2 b/examples/SHIFTIN.bs2 new file mode 100755 index 0000000..057dca3 --- /dev/null +++ b/examples/SHIFTIN.bs2 @@ -0,0 +1,32 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: SHIFTIN +'This program uses the SHIFTIN instruction to interface with the ADC0831 +'8-bit analog-to-digital converter from National Semiconductor. + +CS PIN 0 'chip select +AData PIN 1 'data pin +Clk PIN 2 'clock pin + +adcRes VAR Byte 'ADC result + +Setup: + PAUSE 200 'short startup-pause + HIGH CS 'deselect ADC + +'In the loop below, just three lines of code are required to read the +'ADC0831. The SHIFTIN command does most of the work. The mode argument in +'the SHIFTIN command specifies MSB or LSB-first and whether to sample data +'before or after the clock. In this case, we chose MSB-first, post-clock. +'The ADC0831 precedes its data output with a dummy bit, which we take care +'of by specifying 9 bits of data instead of 8. + +Main: + DO + LOW CS 'activate the ADC0831 + SHIFTIN AData, Clk, MSBPOST, [adcRes\9] 'shift in the data + HIGH CS 'deactivate ADC0831 + DEBUG ? adcRes 'show conversion result + PAUSE 1000 'wait one second + LOOP 'repeat \ No newline at end of file diff --git a/examples/SHIFTOUT.bs2 b/examples/SHIFTOUT.bs2 new file mode 100755 index 0000000..cb684fe --- /dev/null +++ b/examples/SHIFTOUT.bs2 @@ -0,0 +1,37 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: SHIFTOUT +'This program uses the SHIFTOUT command to interface to the 74HC595 shift +'register as an 8-bit output port. The 74HC595 requires a minimum of three +'inputs: data, clock, and latch. See the figure in the SHIFTOUT command +'description for wiring information. SHIFTOUT automatically handles the data +'and clock, pulsing the clock to shift data bits into the 74HC595. An extra +'step (pulsing the latch input) is required to move the shifted bits in +'parallel onto the 74HC595's output pins. Note: this code does not control +'the output-enable or reset lines of the 74HC595. This means that before the +'BASIC Stamp first sends, the 74HC595's output latches are turned on and may +'contain random data. In critical applications, you should hold output-enable +'high (disabled) until the BASIC Stamp can take control. + +Dpin PIN 0 'data pin to 74HC595 +Clk PIN 1 'shift clock to 74HC595 +Latch PIN 2 'latch 74HC595 outputs + +counter VAR Byte + +Setup: + LOW Latch 'initialize latch output + +'This loop moves the 8-bit value 'counter' onto the output lines of the +'74HC595, pauses, then increments counter and repeats. The data is shifted +'msb first so that the msb appears on pin QH and the lsb on QA. Changing +'MSBFIRST to LSBFIRST causes the data to appear backwards on the outputs. + +Main: + DO + SHIFTOUT Dpin, Clk, MSBFIRST, [counter] 'send the bits + PULSOUT Latch, 1 'transfer to outputs + PAUSE 100 'Wait 0.1 seconds + counter = counter + 1 'increment counter + LOOP \ No newline at end of file diff --git a/examples/SLEEP.bs2 b/examples/SLEEP.bs2 new file mode 100755 index 0000000..c10b8f7 --- /dev/null +++ b/examples/SLEEP.bs2 @@ -0,0 +1,17 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: SLEEP +'This program lights an LED and then goes to sleep. Connect an LED to pin +'0 as shown in the description of SLEEP in the manual and run the program. +'The LED will turn on, then the BASIC Stamp will go to sleep. During sleep, +'the LED will remain on, but will blink at intervals of approximately 2.3 +'seconds due to the watchdog timeout and reset. + +Main: + LOW 0 'turn LED on + +Snooze: + DO + SLEEP 10 'sleep for about 10 seconds + LOOP \ No newline at end of file diff --git a/examples/STOP.bs2 b/examples/STOP.bs2 new file mode 100755 index 0000000..fcde00e --- /dev/null +++ b/examples/STOP.bs2 @@ -0,0 +1,12 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: STOP +'This program is similar to END, NAP, and SLEEP except that the LED +'will not blink since the STOP command prevents the BASIC Stamp from +'entering low power mode at the end of the code. Use the circuit +'shown in the description of the SLEEP command for this example. + +Main: + LOW 0 'turn LED on + STOP 'stop program \ No newline at end of file diff --git a/examples/STOREALL.bs2 b/examples/STOREALL.bs2 new file mode 100755 index 0000000..8dc1dad --- /dev/null +++ b/examples/STOREALL.bs2 @@ -0,0 +1,75 @@ +'{$STAMP BS2p} +'{$PBASIC 2.5} + +'SOURCE: STOREALL +'This program demonstrates the STORE command and how it can be used to +'"flatten" the EEPROM space for applications requiring a lot of storage. +'This program writes to EEPROM locations within program slots 1 though 7 +'on the BS2p and BS2px, and 1 through 15 on the BS2pe, thus, has access to +'14 or 30 kBytes of space. + +#SELECT $STAMP 'Notify of module requirements + #CASE BS2, BS2E, BS2SX + #ERROR "Program requires BS2p, BS2pe or BS2px." + #CASE BS2P, BS2PX + HiSlot CON 7 + #CASE BS2PE + HiSlot CON 15 +#ENDSELECT + +LoSlot CON 1 'first slot for "flat" EE +MemSize CON HiSlot - LoSlot + 1 * 2048 + +eeAddr VAR Word 'address pointer +value VAR Word 'cell value +slot VAR Byte 'current R/W slot + +Init: + PAUSE 200 'short startup-pause + +Main: + DEBUG "Flat Memory", CR, + "---------------------", CR, + "First Slot..... ", DEC LoSlot, CR, + "Last Slot...... ", DEC HiSlot, CR, + "Flat EE Size... ", DEC MemSize, CR, CR + + PAUSE 2000 + DEBUG "Writing to flat Memory...", CR + PAUSE 1000 + FOR eeAddr = 0 TO (MemSize - 1) STEP 128 'step through "flat" EE + value = eeAddr * 2 'generate value + GOSUB Write_Word 'write it + GET 127, slot 'get R/W slot + DEBUG "--> Location: ", DEC5 eeAddr, " ", 'show "flat" address + "Value: ", DEC5 value, " ", 'show value + "(", DEC slot.NIB1, ")", CR 'show slot + NEXT + DEBUG CR + + DEBUG "Reading from flat Memory...", CR + PAUSE 1000 + FOR eeAddr = 0 TO (MemSize - 1) STEP 128 + GOSUB Read_Word 'read value from EE + GET 127, slot 'get W/R slot + DEBUG "<-- Location: ", DEC5 eeAddr, " ", + "Value: ", DEC5 value, " ", + "(", DEC slot.NIB1, ") " + IF (value <> (2 * eeAddr)) THEN 'verify location + DEBUG "- Error" + ENDIF + DEBUG CR + NEXT + END + +Write_Word: +'NOTE: only use even-byte eeAddr with this routine + STORE (eeAddr >> 11) + LoSlot 'set slot + WRITE eeAddr, Word value 'write value + RETURN + +Read_Word: +'NOTE: only use even-byte eeAddr with this routine + STORE (eeAddr >> 11) + LoSlot 'set slot + READ eeAddr, Word value 'read value + RETURN \ No newline at end of file diff --git a/examples/TOGGLE.bs2 b/examples/TOGGLE.bs2 new file mode 100755 index 0000000..21d4bb8 --- /dev/null +++ b/examples/TOGGLE.bs2 @@ -0,0 +1,24 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: TOGGLE +'Connect LEDs to pins 0 through 3 as shown in the TOGGLE command description +'and run this program. The TOGGLE command will treat you to a light show. +'You may also run the demo without LEDs; the Debug Terminal will show you the +'states of pins 0 through 3. + +thePin VAR Nib 'pin 0 - 3 + +Setup: + PAUSE 200 'short startup-pause + DIRA = %1111 'make LEDs output, low + +Main: + DEBUG "LED States: " + DO + FOR thePin = 0 TO 3 'loop through pins + TOGGLE thePin 'toggle current pin + DEBUG CRSRXY, 12, 0, BIN4 OUTA 'show on Debug Terminal + PAUSE 250 'short delay + NEXT + LOOP 'repeat forever \ No newline at end of file diff --git a/examples/WRITE.bs2 b/examples/WRITE.bs2 new file mode 100755 index 0000000..e371799 --- /dev/null +++ b/examples/WRITE.bs2 @@ -0,0 +1,40 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: WRITE +'This program writes some data to EEPROM and then reads them back out +'and displays the data in the Debug Terminal. It also demonstrates +'writing both bytes and words, and the results of reading values as +'bytes or words. + +idx VAR Byte 'loop control +value VAR Word(3) 'value(s) + +Init: + PAUSE 200 'short startup-pause + +Main: + WRITE 0, 100 'single byte + WRITE 1, Word 1250 'single word + WRITE 3, 45, 90, Word 725 'multi-value write + +Read_EE: + 'read values as bytes only + DEBUG "Values as bytes:", CR + FOR idx = 0 TO 6 + READ idx, value + DEBUG DEC1 idx, " : ", DEC value, CR + NEXT + DEBUG CR + + 'read values as stored + DEBUG "Values as written:", CR + READ 0, value + DEBUG DEC value, CR + READ 1, Word value + DEBUG DEC value, CR + READ 3, value(0), value(1), Word value(2) + FOR idx = 0 TO 2 + DEBUG DEC value(idx), CR + NEXT + END \ No newline at end of file diff --git a/examples/XOUT.bs2 b/examples/XOUT.bs2 new file mode 100755 index 0000000..c598f86 --- /dev/null +++ b/examples/XOUT.bs2 @@ -0,0 +1,46 @@ +'{$STAMP BS2} +'{$PBASIC 2.5} + +'SOURCE: XOUT +'This program--really two program fragments--demonstrates the syntax and +'use of the XOUT command. XOUT works like pressing the buttons on an X-10 +'control box; first you press one of 16 keys to identify the unit you want +'to control, then you press the key for the action you want that unit to +'take (turn ON, OFF, Bright, or Dim). There are also two group-action keys, +'Lights ON and All OFF. Lights ON turns all lamp modules on without +'affecting appliance modules. All OFF turns off all modules, both lamp and +'appliance types. Connect the BASIC Stamp to a power-line interface as +'shown in the XOUT command description in the manual. + +Mpin PIN 1 'modulation pin +Zpin PIN 0 'zero-cross input + +HouseA CON 0 'House code A = 0 +Unit1 CON 0 'Unit code 1 = 0 +Unit2 CON 1 'Unit code 2 = 1 + +'This first example turns a standard (appliance or non-dimmer lamp) module +'ON, then OFF. Note that once the Unit code is sent, it need not be repeated +'--subsequent instructions are understood to be addressed to that unit. + +Main: + XOUT Mpin, Zpin, [HouseA\Unit1\2] 'select Unit1 (appliance module) + XOUT Mpin, Zpin, [HouseA\UNITON] 'turn it on + + PAUSE 1000 'wait one second + + XOUT Mpin, Zpin, [HouseA\UNITOFF] 'then turn it off + +'The next example talks to a lamp module using the dimmer feature. Dimmers +'go from full ON to dimmed OFF in 19 steps. Because dimming is relative to +'the current state of the lamp, the only guaranteed way to set a predefined +'brightness level is to turn the dimmer fully OFF, then ON, then dim to the +'desired level. + + XOUT Mpin, Zpin, [HouseA\Unit2\2] 'select Unit2 (lamp module) + +'This example shows the use of the optional Cycles argument. Here we DIM +'for 10 cycles. + + XOUT Mpin, Zpin, [HouseA\UNITOFF\2, HouseA\DIM\10] + STOP \ No newline at end of file diff --git a/package.json b/package.json index 109ce1b..9289845 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "iggins": "^0.4.0", "irken": "^0.8.0", "lodash": "^3.9.1", + "raw-loader": "^0.5.1", "react": "^0.13.1", "react-loader": "^1.2.0", "react-material": "git://github.com/iceddev/react-material#fake-release-13", diff --git a/webpack.config.js b/webpack.config.js index a36b101..7b3769d 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,11 +1,15 @@ 'use strict'; +var fs = require('fs'); var path = require('path'); var webpack = require('webpack'); var shouldWatch = (process.argv.indexOf('--watch') !== -1); +var examplesDir = './examples'; +var examples = fs.readdirSync(examplesDir); + module.exports = { devtool: 'source-map', entry: './client.js', @@ -30,6 +34,10 @@ module.exports = { test: /\.json$/, loader: 'json-loader' }, + { + test: /\.bs2$/, + loader: 'raw-loader' + }, { test: /\.js$/, exclude: [ @@ -43,7 +51,10 @@ module.exports = { ] }, plugins: [ - new webpack.optimize.DedupePlugin() + new webpack.optimize.DedupePlugin(), + new webpack.DefinePlugin({ + EXAMPLES_LIST: JSON.stringify(examples) + }) ], resolveLoader: { // this is a workaround for loaders being applied From 55535df01e54e98c7b35727b6ce8eba4843b93b4 Mon Sep 17 00:00:00 2001 From: Matthew Shepard Date: Thu, 29 Oct 2015 17:27:57 -0700 Subject: [PATCH 2/4] Implement example folder creation --- client.js | 19 +++++++++++-------- src/plugins/handlers.js | 14 +++++++++++++- src/plugins/notifications.js | 9 +++++++-- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/client.js b/client.js index 8df61ff..36e9e07 100644 --- a/client.js +++ b/client.js @@ -5,11 +5,10 @@ const Irken = require('irken'); const app = new Irken(); -const examples = _.map(EXAMPLES_LIST, function(name){ - return require('./examples/'+name); -}); - -console.log(examples); +const examples = _.reduce(EXAMPLES_LIST, function(result, name){ + result[name] = require('./examples/'+name); + return result; +}, {}); const plugins = [ { @@ -62,7 +61,7 @@ const plugins = [ } ]; -const defaultProject = 'new-project'; +const exampleFolder = 'examples'; function onRender(err){ console.log('rendered', err); @@ -77,6 +76,7 @@ function onRender(err){ } = app; const { + ensureExampleProject, newFile, showNewVersionOverlay, changeFile, @@ -87,10 +87,13 @@ function onRender(err){ // Finish Loading Plugin // TODO: encapsulate into a startup handler? const config = userConfig.getState(); - const cwd = config.cwd || defaultProject; + const cwd = config.cwd || exampleFolder; const lastFile = config['last-file']; console.log(cwd, lastFile); - changeProject(cwd) + ensureExampleProject(examples, exampleFolder) + .then(function(){ + return changeProject(cwd); + }) .then(() => { if(lastFile){ changeFile(lastFile); diff --git a/src/plugins/handlers.js b/src/plugins/handlers.js index 955380f..17f430c 100644 --- a/src/plugins/handlers.js +++ b/src/plugins/handlers.js @@ -3,6 +3,7 @@ const path = require('path'); const _ = require('lodash'); +const when = require('when'); const cm = require('../code-mirror'); const store = require('../store'); @@ -152,6 +153,17 @@ function handlers(app, opts, done){ handleActionQueue(); } + function ensureExampleProject(examples, dirname){ + return workspace.listProjects() + .then(function(result){ + const hasExamples = _.some(result.payload.projects, (project) => project === dirname); + if(!hasExamples){ + return workspace.changeDirectory(dirname) + .then(() => when.map(examples, (content, name) => workspace.saveFile(name, content, true))); + } + }); + } + function dontSaveFile(){ const { nextFile } = store.getState(); @@ -214,7 +226,6 @@ function handlers(app, opts, done){ }); } - // TODO: should return a promise function changeProject(projectName){ if(!projectName){ return; @@ -687,6 +698,7 @@ function handlers(app, opts, done){ changeProject, deleteProject, deleteProjectConfirm, + ensureExampleProject, // overlay methods showHelpOverlay, showSaveOverlay, diff --git a/src/plugins/notifications.js b/src/plugins/notifications.js index a132d86..8474bfe 100644 --- a/src/plugins/notifications.js +++ b/src/plugins/notifications.js @@ -27,19 +27,24 @@ function notifications({ workspace, toast }, opts, done){ workspace.subscribe(() => { const { status, notification } = workspace.getState(); + const { silent, message } = notification; switch(status){ case SAVE_FILE_SUCCESS: case DELETE_FILE_SUCCESS: case DELETE_DIRECTORY_SUCCESS: - toast.show(notification, { style: styles.successToast, timeout: 5000 }); + if(!silent){ + toast.show(message, { style: styles.successToast, timeout: 5000 }); + } break; case SAVE_FILE_FAILURE: case DELETE_FILE_FAILURE: case CHANGE_FILE_FAILURE: case DELETE_DIRECTORY_FAILURE: case CHANGE_DIRECTORY_FAILURE: - toast.show(notification, { style: styles.errorToast }); + if(!silent){ + toast.show(message, { style: styles.errorToast }); + } break; } }); From a3d00a05765cc4d893e4266d9ee56999bcfa4ee0 Mon Sep 17 00:00:00 2001 From: Matthew Shepard Date: Thu, 29 Oct 2015 18:19:38 -0700 Subject: [PATCH 3/4] Restructure examples, convert to plugin --- client.js | 16 ++++++++++------ examples/{AUX_MAIN_TERM.bs2 => AUX_TERM.bs2} | 2 +- src/plugins/examples.js | 13 +++++++++++++ src/plugins/handlers.js | 18 ++++++++++++------ src/plugins/notifications.js | 9 ++------- 5 files changed, 38 insertions(+), 20 deletions(-) rename examples/{AUX_MAIN_TERM.bs2 => AUX_TERM.bs2} (99%) create mode 100644 src/plugins/examples.js diff --git a/client.js b/client.js index 36e9e07..0458aba 100644 --- a/client.js +++ b/client.js @@ -10,6 +10,8 @@ const examples = _.reduce(EXAMPLES_LIST, function(result, name){ return result; }, {}); +const exampleFolder = 'examples'; + const plugins = [ { register: require('bs2-serial') @@ -35,6 +37,13 @@ const plugins = [ { register: require('./src/plugins/handlers') }, + { + register: require('./src/plugins/examples'), + options: { + examples, + folder: exampleFolder + } + }, { register: require('./src/plugins/keyboard-shortcuts') }, @@ -61,8 +70,6 @@ const plugins = [ } ]; -const exampleFolder = 'examples'; - function onRender(err){ console.log('rendered', err); @@ -90,10 +97,7 @@ function onRender(err){ const cwd = config.cwd || exampleFolder; const lastFile = config['last-file']; console.log(cwd, lastFile); - ensureExampleProject(examples, exampleFolder) - .then(function(){ - return changeProject(cwd); - }) + changeProject(cwd) .then(() => { if(lastFile){ changeFile(lastFile); diff --git a/examples/AUX_MAIN_TERM.bs2 b/examples/AUX_TERM.bs2 similarity index 99% rename from examples/AUX_MAIN_TERM.bs2 rename to examples/AUX_TERM.bs2 index c7081b8..f10bd63 100755 --- a/examples/AUX_MAIN_TERM.bs2 +++ b/examples/AUX_TERM.bs2 @@ -34,4 +34,4 @@ Main: '-- on main and aux, alternately port = ~port 'Invert port PAUSE 1000 '1 second delay - LOOP \ No newline at end of file + LOOP diff --git a/src/plugins/examples.js b/src/plugins/examples.js new file mode 100644 index 0000000..0d641f6 --- /dev/null +++ b/src/plugins/examples.js @@ -0,0 +1,13 @@ +'use strict'; + +function examples(app, opts, done){ + const { handlers } = app; + + handlers.ensureExampleProject(opts.examples, opts.folder || 'examples') + .then(function(){ + return done(); + }) + .catch(done); +} + +module.exports = examples; diff --git a/src/plugins/handlers.js b/src/plugins/handlers.js index 17f430c..d476ecc 100644 --- a/src/plugins/handlers.js +++ b/src/plugins/handlers.js @@ -154,12 +154,18 @@ function handlers(app, opts, done){ } function ensureExampleProject(examples, dirname){ - return workspace.listProjects() - .then(function(result){ - const hasExamples = _.some(result.payload.projects, (project) => project === dirname); - if(!hasExamples){ - return workspace.changeDirectory(dirname) - .then(() => when.map(examples, (content, name) => workspace.saveFile(name, content, true))); + return workspace.changeDirectory(dirname) + .then(function(){ + const { directory } = workspace.getState(); + const missing = _.reduce(examples, (result, content, name) => { + const baseName = path.basename(name, '.bs2'); + if(!_.some(directory, 'name', baseName)){ + result[baseName] = content; + } + return result; + }, {}); + if(_.size(missing) > 0){ + return when.settle(_.map(missing, (content, name) => workspace.saveFile(name, content))); } }); } diff --git a/src/plugins/notifications.js b/src/plugins/notifications.js index 8474bfe..a132d86 100644 --- a/src/plugins/notifications.js +++ b/src/plugins/notifications.js @@ -27,24 +27,19 @@ function notifications({ workspace, toast }, opts, done){ workspace.subscribe(() => { const { status, notification } = workspace.getState(); - const { silent, message } = notification; switch(status){ case SAVE_FILE_SUCCESS: case DELETE_FILE_SUCCESS: case DELETE_DIRECTORY_SUCCESS: - if(!silent){ - toast.show(message, { style: styles.successToast, timeout: 5000 }); - } + toast.show(notification, { style: styles.successToast, timeout: 5000 }); break; case SAVE_FILE_FAILURE: case DELETE_FILE_FAILURE: case CHANGE_FILE_FAILURE: case DELETE_DIRECTORY_FAILURE: case CHANGE_DIRECTORY_FAILURE: - if(!silent){ - toast.show(message, { style: styles.errorToast }); - } + toast.show(notification, { style: styles.errorToast }); break; } }); From 30f3aeb85a7967a10beca97f7feb665315b72832 Mon Sep 17 00:00:00 2001 From: Blaine Bublitz Date: Thu, 29 Oct 2015 20:20:06 -0700 Subject: [PATCH 4/4] small cleanup --- client.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/client.js b/client.js index 0458aba..c864cad 100644 --- a/client.js +++ b/client.js @@ -6,7 +6,7 @@ const Irken = require('irken'); const app = new Irken(); const examples = _.reduce(EXAMPLES_LIST, function(result, name){ - result[name] = require('./examples/'+name); + result[name] = require(`./examples/${name}`); return result; }, {}); @@ -83,7 +83,6 @@ function onRender(err){ } = app; const { - ensureExampleProject, newFile, showNewVersionOverlay, changeFile,