home *** CD-ROM | disk | FTP | other *** search
/ European Smalltalk User Group 2004 September / esugcd.iso / Books-Tutorial-Lectures / Books / FreeBooks / GuzdialBookDrafts / clock.st < prev    next >
Encoding:
Text File  |  2003-03-31  |  6.7 KB  |  275 lines

  1.  
  2. Object subclass: #Appointment
  3.     instanceVariableNames: 'alarm description date '
  4.     classVariableNames: ''
  5.     poolDictionaries: ''
  6.     category: 'ClockWorks'!
  7.  
  8. !Appointment methodsFor: 'appointment alarms' stamp: 'mjg 9/27/1998 14:34'!
  9. on
  10.     "The appointment is today, so turn on alarm."
  11.     alarm alarmBlock: [3 timesRepeat: [Smalltalk beep.].
  12.         Transcript show: 'Appointment: ',description.
  13.         alarm stop.].
  14.     alarm setTime: (Time now printString).
  15.     alarm start.
  16. ! !
  17.  
  18.  
  19. !Appointment methodsFor: 'accessing' stamp: 'mjg 9/27/1998 14:18'!
  20. alarm
  21.     ^alarm
  22. ! !
  23.  
  24. !Appointment methodsFor: 'accessing' stamp: 'mjg 9/27/1998 14:32'!
  25. alarm: someTime
  26.     alarm _ AlarmClock new.
  27.     alarm setTime: Time now printString.
  28.     alarm setAlarmTime: someTime.
  29. ! !
  30.  
  31. !Appointment methodsFor: 'accessing' stamp: 'mjg 9/27/1998 14:19'!
  32. date
  33.     ^date! !
  34.  
  35. !Appointment methodsFor: 'accessing' stamp: 'mjg 9/27/1998 14:19'!
  36. date: aDate
  37.     "Set date of appointment."
  38.     date _ aDate! !
  39.  
  40. !Appointment methodsFor: 'accessing' stamp: 'mjg 9/27/1998 14:19'!
  41. description
  42.     ^description! !
  43.  
  44. !Appointment methodsFor: 'accessing' stamp: 'mjg 9/27/1998 14:19'!
  45. description: aDescription
  46.     description := aDescription.! !
  47.  
  48.  
  49.  
  50. Object subclass: #AppointmentBook
  51.     instanceVariableNames: 'appointments '
  52.     classVariableNames: ''
  53.     poolDictionaries: ''
  54.     category: 'ClockWorks'!
  55.  
  56. !AppointmentBook methodsFor: 'appointment alarms' stamp: 'mjg 9/27/1998 14:36'!
  57. allOff
  58.     appointments do: [:appointment | appointment alarm stop].! !
  59.  
  60. !AppointmentBook methodsFor: 'appointment alarms' stamp: 'mjg 9/27/1998 14:31'!
  61. onToday
  62.     (appointments select: [:each | each date = Date today]) do: [:each | each on].
  63. ! !
  64.  
  65.  
  66. !AppointmentBook methodsFor: 'appointments' stamp: 'mjg 9/27/1998 14:33'!
  67. appointments
  68.     ^appointments! !
  69.  
  70. !AppointmentBook methodsFor: 'appointments' stamp: 'mjg 9/27/1998 14:29'!
  71. makeAppointment: aDescription for: aDate at: aTime
  72.     | a |
  73.     a _ Appointment new.
  74.     a description: aDescription.
  75.     a date: (Date readFrom: (ReadStream on: aDate)).
  76.     a alarm: aTime.
  77.     appointments add: a.
  78. ! !
  79.  
  80.  
  81. !AppointmentBook methodsFor: 'initialization' stamp: 'mjg 9/27/1998 14:07'!
  82. initialize
  83.     appointments _ OrderedCollection new.! !
  84.  
  85.  
  86.  
  87. Object subclass: #Clock
  88.     instanceVariableNames: 'time timer displayFormat '
  89.     classVariableNames: ''
  90.     poolDictionaries: ''
  91.     category: 'ClockWorks'!
  92.  
  93. !Clock methodsFor: 'time management' stamp: 'mjg 9/26/1998 17:22'!
  94. display
  95.     "Display the time in a given format"
  96.     | hours minutes seconds |
  97.     hours _ time hours printString.
  98.     minutes _ time minutes printString.
  99.     (minutes size < 2) ifTrue: [minutes _ '0',minutes]. "Must be two digits"
  100.     seconds _ time seconds printString.
  101.     (seconds size < 2) ifTrue: [seconds _ '0',seconds]. 
  102.     (displayFormat = '12')
  103.     ifTrue: [(hours asNumber > 12) 
  104.         ifTrue: [^((hours asNumber - 12) printString),':',minutes,':',
  105.             seconds,' pm'].
  106.         (hours asNumber < 12)
  107.         ifTrue: [^hours,':',minutes,':',seconds,' am']
  108.         ifFalse: ["Exactly 12 must be printed as pm"
  109.             ^hours,':',minutes ,':',seconds,' pm']]
  110.     ifFalse: ["24-hour time is the default if no displayFormat is set"
  111.         ^hours,':',minutes,':',seconds].! !
  112.  
  113. !Clock methodsFor: 'time management' stamp: 'mjg 9/26/1998 16:57'!
  114. displayFormat: aType
  115.     "aType should be '24' or '12'"
  116.     displayFormat _ aType
  117. ! !
  118.  
  119. !Clock methodsFor: 'time management' stamp: 'mjg 9/27/1998 14:10'!
  120. nextSecond
  121.     time _ time addTime: (Time fromSeconds: 1)
  122.  ! !
  123.  
  124. !Clock methodsFor: 'time management' stamp: 'mjg 9/26/1998 16:56'!
  125. setTime: aString
  126.     time _ Time readFrom: (ReadStream on: aString).
  127. ! !
  128.  
  129. !Clock methodsFor: 'time management' stamp: 'mjg 9/27/1998 14:21'!
  130. start
  131.     timer isNil ifFalse: [timer stopTicking. "Stop one if already existing."].
  132.     timer _ SecondsTimer new.
  133.     timer clock: self.
  134.     timer startTicking.
  135. ! !
  136.  
  137. !Clock methodsFor: 'time management' stamp: 'mjg 9/27/1998 14:37'!
  138. stop
  139.     timer isNil ifFalse: [timer stopTicking].
  140.     timer _ nil.
  141. ! !
  142.  
  143.  
  144. !Clock methodsFor: 'accessing' stamp: 'mjg 9/26/1998 17:02'!
  145. hours
  146.     ^time hours! !
  147.  
  148. !Clock methodsFor: 'accessing' stamp: 'mjg 9/26/1998 16:56'!
  149. time
  150.     ^time! !
  151.  
  152. !Clock methodsFor: 'accessing' stamp: 'mjg 9/27/1998 14:20'!
  153. timer
  154.     ^timer
  155. ! !
  156.  
  157.  
  158.  
  159. Clock subclass: #AlarmClock
  160.     instanceVariableNames: 'alarmTime alarmBlock '
  161.     classVariableNames: ''
  162.     poolDictionaries: ''
  163.     category: 'ClockWorks'!
  164.  
  165. !AlarmClock methodsFor: 'time management' stamp: 'mjg 9/27/1998 14:02'!
  166. alarmBlock: aBlock
  167.     alarmBlock _ aBlock.
  168. ! !
  169.  
  170. !AlarmClock methodsFor: 'time management' stamp: 'mjg 9/27/1998 13:57'!
  171. alarmTime
  172.     ^alarmTime
  173. ! !
  174.  
  175. !AlarmClock methodsFor: 'time management' stamp: 'mjg 9/27/1998 13:58'!
  176. nextSecond
  177.     super nextSecond.
  178.     (time = alarmTime) ifTrue: [alarmBlock value].
  179. ! !
  180.  
  181. !AlarmClock methodsFor: 'time management' stamp: 'mjg 9/27/1998 13:58'!
  182. setAlarmTime: aString
  183.     alarmTime _ Time readFrom: (ReadStream on: aString).
  184. ! !
  185.  
  186.  
  187.  
  188. Object subclass: #SecondsTimer
  189.     instanceVariableNames: 'clock process '
  190.     classVariableNames: ''
  191.     poolDictionaries: ''
  192.     category: 'ClockWorks'!
  193.  
  194. !SecondsTimer methodsFor: 'access' stamp: 'mjg 9/26/1998 17:05'!
  195. clock
  196.     ^clock! !
  197.  
  198. !SecondsTimer methodsFor: 'access' stamp: 'mjg 9/26/1998 17:05'!
  199. clock: aClock
  200.     clock _ aClock.! !
  201.  
  202.  
  203. !SecondsTimer methodsFor: 'time management' stamp: 'mjg 9/27/1998 14:12'!
  204. startTicking
  205.     process := [[true] whileTrue: [(Delay forSeconds: 1) wait. clock nextSecond.]] newProcess.
  206.     process priority: (Processor userBackgroundPriority).
  207.     process resume.! !
  208.  
  209. !SecondsTimer methodsFor: 'time management' stamp: 'mjg 9/26/1998 17:04'!
  210. stopTicking
  211.     process terminate.! !
  212.  
  213.  
  214.  
  215. Object subclass: #VCR
  216.     instanceVariableNames: 'channel recorder '
  217.     classVariableNames: ''
  218.     poolDictionaries: ''
  219.     category: 'ClockWorks'!
  220.  
  221. !VCR methodsFor: 'accessing' stamp: 'mjg 9/27/1998 14:02'!
  222. channel: aChannel
  223.     channel _ aChannel.
  224. ! !
  225.  
  226. !VCR methodsFor: 'accessing' stamp: 'mjg 9/27/1998 13:56'!
  227. clock
  228.     ^recorder clock! !
  229.  
  230.  
  231. !VCR methodsFor: 'vcr functions' stamp: 'mjg 9/27/1998 13:56'!
  232. fastforward
  233.     ^'FastForwarding'! !
  234.  
  235. !VCR methodsFor: 'vcr functions' stamp: 'mjg 9/27/1998 13:55'!
  236. play
  237.     ^'Playing'! !
  238.  
  239. !VCR methodsFor: 'vcr functions' stamp: 'mjg 9/27/1998 13:56'!
  240. record
  241.     ^'Recording'! !
  242.  
  243. !VCR methodsFor: 'vcr functions' stamp: 'mjg 9/27/1998 13:55'!
  244. rewind
  245.     ^'Rewinding'! !
  246.  
  247. !VCR methodsFor: 'vcr functions' stamp: 'mjg 9/27/1998 14:04'!
  248. stop
  249.     ^'Stopping'! !
  250.  
  251.  
  252.  
  253. Object subclass: #VCRRecorder
  254.     instanceVariableNames: 'startTime endTime channel vcr '
  255.     classVariableNames: ''
  256.     poolDictionaries: ''
  257.     category: 'ClockWorks'!
  258.  
  259. !VCRRecorder methodsFor: 'timer functions' stamp: 'mjg 9/27/1998 14:23'!
  260. setEndTime: aTime
  261.     endTime _ AlarmClock new.
  262.     endTime setAlarmTime: aTime.
  263.     endTime alarmBlock: [vcr channel: channel. vcr stop.].
  264.     endTime start.
  265. ! !
  266.  
  267. !VCRRecorder methodsFor: 'timer functions' stamp: 'mjg 9/27/1998 14:23'!
  268. setStartTime: aTime
  269.     startTime _ AlarmClock new.
  270.     startTime setAlarmTime: aTime.
  271.     startTime alarmBlock: [vcr channel: channel. vcr record.].
  272.     startTime start.
  273. ! !
  274.  
  275.