Skip to content

Create and update example project on launch #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
'use strict';

const _ = require('lodash');
const Irken = require('irken');

const app = new Irken();

const examples = _.reduce(EXAMPLES_LIST, function(result, name){
result[name] = require(`./examples/${name}`);
return result;
}, {});

const exampleFolder = 'examples';

const plugins = [
{
register: require('bs2-serial')
Expand All @@ -29,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')
},
Expand All @@ -55,8 +70,6 @@ const plugins = [
}
];

const defaultProject = 'new-project';

function onRender(err){
console.log('rendered', err);

Expand All @@ -80,7 +93,7 @@ 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)
Expand Down
37 changes: 37 additions & 0 deletions examples/AUX_TERM.bs2
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions examples/BRANCH.bs2
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions examples/BUTTON.bs2
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions examples/COUNT.bs2
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions examples/DATA.bs2
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions examples/DEBUG_DEBUGIN.bs2
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions examples/DO-LOOP.bs2
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions examples/DTMFOUT.bs2
Original file line number Diff line number Diff line change
@@ -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
Loading