'From MIT Squeak 0.9.4 (June 1, 2003) [No updates present.] on 15 May 2011 at 11:15:59 am'! Object subclass: #Gainer instanceVariableNames: 'state button portName port semaphore dIn aIn ' classVariableNames: 'DefaultGainer ' poolDictionaries: '' category: 'Scratch-Gainer'! !Gainer methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 15:37'! analogAt: aNumber | result | result _ self sendCommand: 'I'. "aIn _ Number readFrom: result base: 16. ^ dIn anyMask: (2 raisedTo: aNumber)"! ! !Gainer methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 15:34'! analogAt: aNumber put: aValue | str command size v | v := (aValue min: 255) max: 0. str _ '0000' , ((v printStringBase: 16) allButFirst: 3). size _ str size. command _ 'a' , aNumber asString , (str copyFrom: size - 1 to: size). ^ self sendCommand: command! ! !Gainer methodsFor: 'as yet unclassified'! buttonPressed self sendCommand: 'E'. ^ button! ! !Gainer methodsFor: 'as yet unclassified'! checkResponse: aString | stream result token | result _ nil. stream _ ReadStream on: aString. [stream atEnd] whileFalse: [token _ stream upTo: $*. token = 'N' ifTrue: [button _ true]. token = 'F' ifTrue: [button _ false]. (token ~= 'N' and: [token ~= 'F']) ifTrue: [result _ token]]. ^ result! ! !Gainer methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 15:18'! digitalAt: aNumber | result | result _ self sendCommand: 'R'. dIn _ Number readFrom: result base: 16. ^ dIn anyMask: (2 raisedTo: aNumber)! ! !Gainer methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 15:12'! digitalAt: aNumber put: aBoolean | command | command _ (aBoolean ifTrue: ['H'] ifFalse: ['L']) , aNumber asString. ^ self sendCommand: command! ! !Gainer methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 15:53'! initialize state _ #init. semaphore _ Semaphore forMutualExclusion. button _ false! ! !Gainer methodsFor: 'as yet unclassified'! isOpened ^ state = #opened! ! !Gainer methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 14:35'! konfiguration: aNumber self sendCommand: 'KONFIGURATION_' , aNumber asString! ! !Gainer methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 14:35'! led: aBoolean self sendCommand: (aBoolean ifTrue: ['h'] ifFalse: ['l'])! ! !Gainer methodsFor: 'as yet unclassified' stamp: 'EiichiroIto 5/13/2011 23:30'! portName ^ portName! ! !Gainer methodsFor: 'as yet unclassified'! sendCommand: aString | result | self isOpened ifFalse: [^ self]. semaphore critical: [Delay waitMSecs: 10. port nextPutAll: aString , '*'. Delay waitMSecs: 20. result _ port readString]. result _ self checkResponse: result. self assert: [aString first = result first]. ^ result allButFirst! ! !Gainer methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 12:51'! setPortName: aString portName := aString! ! !Gainer methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 12:53'! start | reply | state = #init ifFalse: [^ true]. (portName isNil or: [portName isEmpty]) ifTrue: [^ false]. port _ SerialPort2 new openPortNamed: portName baud: 38400. port isOpen ifFalse: [self error: 'can''t open port:' , portName]. [Delay waitMSecs: 10. port nextPutAll: 'Q*'. Delay waitMSecs: 20. reply _ port readString. reply = 'Q*' ifFalse: [self error: '']] ifError: [port close. port _ nil. ^ false]. state _ #opened. ^ true! ! !Gainer methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 12:50'! stop port ifNotNil: [port close]. port _ nil. state _ #init! ! !Gainer class methodsFor: 'as yet unclassified'! checkPortNamed: aString | port result | (aString isNil or: [aString isEmpty]) ifTrue: [^ false]. port _ SerialPort2 new openPortNamed: aString baud: 38400. port isOpen ifFalse: [^ false]. result _ false. [Delay waitMSecs: 10. port nextPutAll: 'Q*'. Delay waitMSecs: 20. result _ port readString = 'Q*'] ifError: []. port close. ^ result! ! !Gainer class methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 18:11'! default ^ DefaultGainer! ! !Gainer class methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 18:16'! defaultPortNamed: aString DefaultGainer ifNotNil: [DefaultGainer stop]. DefaultGainer _ self newPortNamed: aString. DefaultGainer start; konfiguration: 1! ! !Gainer class methodsFor: 'as yet unclassified'! isOpened DefaultGainer ifNil: [^ false]. ^ DefaultGainer isOpened! ! !Gainer class methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 12:54'! new self shouldNotImplement! ! !Gainer class methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 12:54'! newPortNamed: aString | obj | obj _ self basicNew initialize. obj setPortName: aString. ^ obj! ! !Gainer class methodsFor: 'as yet unclassified' stamp: 'EiichiroIto 5/13/2011 23:20'! portName ^ SerialPort2 portNames detect: [:each | self checkPortNamed: each] ifNone: []! ! !Gainer class methodsFor: 'as yet unclassified' stamp: 'e-itoh 5/12/2011 15:44'! sample1 | g | g _ Gainer newPortNamed: '/dev/cu.usbmodem1a21'. g inspect. g start. g konfiguration: 1. g led: true. g led: false. g digitalAt: 1. g digitalAt: 1 put: false. g analogAt: 0 put: 90. g stop! ! !ScriptableScratchMorph methodsFor: 'blocks'! defaultArgsFor: blockSpec "Answer the default argument for the given block specification." | defaultArgs stage sel currentSize list portName | defaultArgs _ blockSpec copyFrom: 4 to: blockSpec size. "may be empty" stage _ self ownerThatIsA: ScratchStageMorph. sel _ (blockSpec at: 3) asSymbol. #gotoX:y: = sel ifTrue: [ defaultArgs _ Array with: self referencePosition x rounded with: self referencePosition y rounded]. #glideSecs:toX:y:elapsed:from: = sel ifTrue: [ defaultArgs _ Array with: 1 with: self referencePosition x rounded with: self referencePosition y rounded]. #motor:direction: = self ifTrue: [ defaultArgs _ Array with: 'reverse' localized with: 'this way' localized with: 'that way']. #setSizeTo: = sel ifTrue: [ currentSize _ (100.0 * (self scalePoint x max: self scalePoint y)) rounded. defaultArgs _ Array with: currentSize]. #getAttribute:of: = sel ifTrue: [ (stage _ self ownerThatIsA: ScratchStageMorph) ifNotNil: [ list _ stage submorphs select: [:m | m isKindOf: ScratchSpriteMorph]. list sort: [:s1 :s2 | s1 objName asLowercase < s2 objName asLowercase]. list size > 0 ifTrue: [defaultArgs _ Array with: 'x position' with: list first] ifFalse: [defaultArgs _ Array with: 'background #' with: stage]] ifNil:[defaultArgs _ Array with: 'x position' with: self]]. #concatenate:with: = sel ifTrue: [ defaultArgs _ Array with: 'hello ' localized with: 'world' localized]. #doAsk = sel ifTrue: [ defaultArgs _ Array with: 'What''s your name?' localized]. #letter:of: = sel ifTrue: [ defaultArgs _ Array with: 1 with: 'world' localized]. #stringLength: = sel ifTrue: [ defaultArgs _ Array with: 'world' localized]. #say:duration:elapsed:from: = sel ifTrue: [ defaultArgs _ Array with: 'Hello!!' localized with: 2]. #say: = sel ifTrue: [ defaultArgs _ Array with: 'Hello!!' localized]. #think:duration:elapsed:from: = sel ifTrue: [ defaultArgs _ Array with: 'Hmm...' localized with: 2]. #think: = sel ifTrue: [ defaultArgs _ Array with: 'Hmm...' localized]. (#(lookLike: showBackground:) includes: sel) ifTrue: [ defaultArgs _ Array with: self costumeNames last]. (#(playSound: doPlaySoundAndWait) includes: sel) ifTrue: [ list _ self soundNames. defaultArgs _ list size <= 2 ifTrue: [Array with: ''] ifFalse: [Array with: (list at: (list size - 2))]]. (#(broadcast: doBroadcastAndWait) includes: sel) ifTrue: [ stage ifNotNil: [defaultArgs _ Array with: stage defaultEventName]]. (#(append:toList: deleteLine:ofList: insert:at:ofList:) includes: sel) ifTrue: [ defaultArgs size >= 1 ifTrue: [ defaultArgs at: 1 put: (defaultArgs at: 1) localized]]. (#(append:toList: deleteLine:ofList: getLine:ofList: insert:at:ofList: lineCountOfList:) includes: sel) ifTrue: [ defaultArgs _ defaultArgs copyWith: self defaultListName]. #setLine:ofList:to: = sel ifTrue: [ defaultArgs size >= 3 ifTrue: [ defaultArgs at: 2 put: self defaultListName. defaultArgs at: 3 put: (defaultArgs at: 3) localized]]. #appendLettersOf:toList: = sel ifTrue: [ defaultArgs size >= 2 ifTrue: [ defaultArgs at: 1 put: (defaultArgs at: 1) localized. defaultArgs at: 2 put: self defaultListName]]. #list:contains: = sel ifTrue: [ defaultArgs size >= 2 ifTrue: [ defaultArgs at: 1 put: self defaultListName. defaultArgs at: 2 put: (defaultArgs at: 2) localized]]. #gainerPort: = sel ifTrue: [ Gainer isOpened ifTrue: [portName := Gainer default portName] ifFalse: [portName := Gainer portName. portName ifNil: [portName := 'not connected']]. defaultArgs at: 1 put: portName. ]. ^ defaultArgs ! ! !ScriptableScratchMorph methodsFor: 'gainer' stamp: 'e-itoh 5/12/2011 18:51'! gainerAOut: aNumber put: aValue Gainer default analogAt: aNumber put: aValue! ! !ScriptableScratchMorph methodsFor: 'gainer'! gainerButtonPressed ^ Gainer default buttonPressed! ! !ScriptableScratchMorph methodsFor: 'gainer' stamp: 'e-itoh 5/12/2011 18:52'! gainerDIn: aNumber ^ Gainer default digitalAt: aNumber! ! !ScriptableScratchMorph methodsFor: 'gainer' stamp: 'e-itoh 5/12/2011 18:52'! gainerDOut: aNumber put: aValue Gainer default digitalAt: aNumber put: (aValue ~= 0)! ! !ScriptableScratchMorph methodsFor: 'gainer' stamp: 'e-itoh 5/12/2011 18:52'! gainerLED: aNumber Gainer default led: aNumber ~= 0! ! !ScriptableScratchMorph methodsFor: 'gainer' stamp: 'e-itoh 5/12/2011 18:52'! gainerPort: aString Gainer defaultPortNamed: aString! ! !ScriptableScratchMorph methodsFor: 'gainer' stamp: 'e-itoh 5/12/2011 18:52'! gainerStop Gainer default stop! ! !ScriptableScratchMorph class methodsFor: 'block specs'! blockSpecs "Answer a collection of block specifications for the blocks that are common to all objects. Block specificatons (Arrays) are interspersed with category names (Strings). A block specification is an Array of the form: ( [optional initial argument values]). Explanation of flags: - no flags b boolean reporter c c-shaped block containing a sequence of commands (always special form) r reporter s special form command with its own evaluation rule t timed command, like wait or glide E message event hat K key event hat M mouse-click event hat S start event hat W when hat (obsolete)" | blocks | blocks _ #( 'motion' - ('gainer port %s' - gainerPort: '/dev/cu.usbmodem1a21') ('gainer stop' - gainerStop) ('Set gainer digital output %n to %n' - gainerDOut:put: 0 0) ('Set gainer analog output %n to %n' - gainerAOut:put: 0 0) ('Set gainer led to %n' - gainerLED: 1) 'control' ('when %m clicked' S -) ('when %k key pressed' K -) ('when %m clicked' M -) - ('wait %n secs' t wait:elapsed:from: 1) - ('forever' c doForever) ('repeat %n' c doRepeat 10) - ('broadcast %e' - broadcast:) ('broadcast %e and wait' s doBroadcastAndWait) ('when I receive %e' E -) - ('forever if %b' c doForeverIf) ('if %b' c doIf) ('if %b' c doIfElse) ('wait until %b' s doWaitUntil) ('repeat until %b' c doUntil) - ('stop script' s doReturn) ('stop all' - stopAll) 'operators' ('%n + %n' r + - -) ('%n - %n' r - - -) ('%n * %n' r * - -) ('%n / %n' r / - -) - ('pick random %n to %n' r randomFrom:to: 1 10) - ('%s < %s' b < '' '') ('%s = %s' b = '' '') ('%s > %s' b > '' '') - ('%b and %b' b &) ('%b or %b' b |) ('not %b' b not) - ('join %s %s' r concatenate:with: 'hello ' 'world') ('letter %n of %s' r letter:of: 1 'world') ('length of %s' r stringLength: 'world') - ('%n mod %n' r \\ - -) ('round %n' r rounded -) - ('%f of %n' r computeFunction:of: 'sqrt' 10) 'sound' ('play sound %S' - playSound:) ('play sound %S until done' s doPlaySoundAndWait) ('stop all sounds' - stopAllSounds) - ('play drum %D for %n beats' t drum:duration:elapsed:from: 48 0.2) ('rest for %n beats' t rest:elapsed:from: 0.2) - ('play note %N for %n beats' t noteOn:duration:elapsed:from: 60 0.5) ('set instrument to %I' - midiInstrument: 1) - ('change volume by %n' - changeVolumeBy: -10) ('set volume to %n%' - setVolumeTo: 100) ('volume' r volume) - ('change tempo by %n' - changeTempoBy: 20) ('set tempo to %n bpm' - setTempoTo: 60) ('tempo' r tempo) 'motor' ('motor on for %n secs' t motorOnFor:elapsed:from: 1) ('motor on' - allMotorsOn) ('motor off' - allMotorsOff) ('motor power %n' - startMotorPower: 100) ('motor direction %W' - setMotorDirection: 'this way') 'variables' ('show variable %v' - showVariable:) ('hide variable %v' - hideVariable:) 'list' ('add %s to %L' - append:toList: 'thing') - ('delete %y of %L' - deleteLine:ofList: 1) ('insert %s at %i of %L' - insert:at:ofList: 'thing' 1) ('replace item %i of %L with %s' - setLine:ofList:to: 1 'list' 'thing') - ('item %i of %L' r getLine:ofList: 1) ('length of %L' r lineCountOfList:) ('%L contains %s' b list:contains: 'list' 'thing') 'sensing' ('gainer digital input %n is on?' b gainerDIn: 0) ('gainer button pressed?' b gainerButtonPressed) ). ^ blocks, self obsoleteBlockSpecs ! ! Gainer class removeSelector: #gainerPortName! Gainer class removeSelector: #initialize! Gainer removeSelector: #button! Gainer removeSelector: #initialization! Gainer removeSelector: #portName:! Gainer removeSelector: #startPortNamed:!