[squeak-dev] The Trunk: EToys-eem.411.mcz

commits at source.squeak.org commits at source.squeak.org
Fri Oct 16 18:00:05 UTC 2020


Eliot Miranda uploaded a new version of EToys to project The Trunk:
http://source.squeak.org/trunk/EToys-eem.411.mcz

==================== Summary ====================

Name: EToys-eem.411
Author: eem
Time: 16 October 2020, 10:59:59.90188 am
UUID: b638cb31-a1eb-4d9d-b244-412b5f7ebec9
Ancestors: EToys-eem.410

CameraInterface: Complete (*) the interrupt-driven interface using it (if available) to wait for camera start, and to wait for the next frame.  Have closeCamera: unregister any external semaphore.  Ypdate the class comment with this example:


[| form |
form := Form extent: 352 @ 288 depth: 32.
CameraInterface
	openCamera: 1 width: form width height: form height;
	trySetSemaphoreForCamera: 1.
[Sensor noButtonPressed] whileTrue:
	[CameraInterface
		waitForNextFrame: 1 timeout: 2000;
		getFrameForCamera: 1 into: form bits.
	 form displayAt: Sensor cursorPoint]] ensure: [CameraInterface closeCamera: 1]

(*) nothing is ever finished, but the essentials are here.

=============== Diff against EToys-eem.410 ===============

Item was changed:
  Object subclass: #CameraInterface
  	instanceVariableNames: ''
  	classVariableNames: ''
  	poolDictionaries: ''
  	category: 'Etoys-Squeakland-MorphicExtras-WebCam'!
  
+ !CameraInterface commentStamp: 'eem 10/16/2020 10:53' prior: 0!
+ CameraInterface: Simple cross-platform webcam access interface adapted from MIT Scratch. Changes made so that different cameras can be tested when more than one is connected, and so that the interface is simpler and may be interrupt-driven.
- !CameraInterface commentStamp: '<historical>' prior: 0!
- CameraInterface: Simple cross-platform webcam access interface from MIT Scratch. Small changes made so that different cameras can be tested when more than one is connected. The "CameraPlugin" binary should soon be included in the VM. On Linux the plugin is designed to take advantage of libv4l2 (if found) to support a wide range of cameras.
  
+ [| form |
+ form := Form extent: 352 @ 288 depth: 32.
+ CameraInterface
+ 	openCamera: 1 width: form width height: form height;
+ 	trySetSemaphoreForCamera: 1.
+ [Sensor noButtonPressed] whileTrue:
+ 	[CameraInterface
+ 		waitForNextFrame: 1 timeout: 2000;
+ 		getFrameForCamera: 1 into: form bits.
+ 	 form displayAt: Sensor cursorPoint]] ensure: [CameraInterface closeCamera: 1]
+ 
  Copyright (c) 2009 Massachusetts Institute of Technology
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.!
- THE SOFTWARE.
- !

Item was added:
+ ----- Method: CameraInterface class>>cameraGetSemaphore: (in category 'camera ops') -----
+ cameraGetSemaphore: cameraNum
+ 	"Answer the external semaphore index through which to signal that a frame is available.
+ 	 Fail if cameraNum has not had a semaphore index set, or is otherwise invalid.
+ 	 Answer nil on failure for convenience."
+ 	<primitive: 'primGetCameraSemaphore' module: 'CameraPlugin' error: ec>
+ 	^nil!

Item was changed:
  ----- Method: CameraInterface class>>closeCamera: (in category 'camera ops') -----
  closeCamera: cameraNum
+ 	"Close the camera. Do nothing if it was not open. Unregister any associated semaphore."
- 	"Close the camera. Do nothing if it was not open."
  
+ 	(self cameraGetSemaphore: cameraNum) ifNotNil:
+ 		[:semaphoreIndex|
+ 		Smalltalk unregisterExternalObject: (Smalltalk externalObjectAt: semaphoreIndex ifAbsent: nil)].
+ 	self primitiveCloseCamera: cameraNum!
- 	<primitive: 'primCloseCamera' module: 'CameraPlugin'>
- !

Item was added:
+ ----- Method: CameraInterface class>>primitiveCloseCamera: (in category 'private-primitives') -----
+ primitiveCloseCamera: cameraNum
+ 	"Close the camera. Do nothing if it was not open except answering nil."
+ 
+ 	<primitive: 'primCloseCamera' module: 'CameraPlugin'>
+ 	^nil!

Item was added:
+ ----- Method: CameraInterface class>>trySetSemaphoreForCamera: (in category 'utilities') -----
+ trySetSemaphoreForCamera: camNum
+ 	"Attempt to set a semaphore to be signalled when a frame is available.
+ 	 Fail silently.  Use e.g. waitForCameraStart: or waitForNextFrame: to
+ 	 access the semaphore if available."
+ 	| semaphore |
+ 	Smalltalk registerExternalObject: (semaphore := Semaphore new).
+ 	[CameraInterface camera: camNum setSemaphore: semaphore]
+ 		on: Error
+ 		do: [:err|
+ 			Smalltalk unregisterExternalObject: semaphore]!

Item was changed:
+ ----- Method: CameraInterface class>>waitForCameraStart: (in category 'utilities') -----
- ----- Method: CameraInterface class>>waitForCameraStart: (in category 'camera ops') -----
  waitForCameraStart: camNum
+ 	"Wait for the camera to get it's first frame (indicated by a non-zero frame extent. Timeout after two seconds."
+ 	"self waitForCameraStart: 1"
- 	"Wait for the camera to get it's first frame (indicated by a non-zero frame extent. Timeout after a few seconds."
- 	"self waitForCameraStart"
  
  	| startTime |
+ 	(self cameraGetSemaphore: camNum) ifNotNil:
+ 		[:semaphoreIndex|
+ 		(Smalltalk externalObjectAt: semaphoreIndex ifAbsent: [self error: 'seriously?!!?!!?!!?']) wait.
+ 		^self].
+ 	startTime := Time utcMicrosecondClock.
+ 	[(self packedFrameExtent: camNum) > 0 ifTrue: [^ self].
+ 	 (Time utcMicrosecondClock - startTime) < 2000000] whileTrue:
+ 		[(Delay forMilliseconds: 50) wait]!
- 	startTime := Time millisecondClockValue.
- 	[(Time millisecondClockValue - startTime) < 2000] whileTrue: [
- 		(self packedFrameExtent: camNum) > 0 ifTrue: [^ self].
- 		(Delay forMilliseconds: 50) wait].!

Item was added:
+ ----- Method: CameraInterface class>>waitForNextFrame:timeout: (in category 'utilities') -----
+ waitForNextFrame: camNum timeout: timeoutms
+ 	"Wait for the camera to get it's first frame (indicated by a non-zero frame extent. Timeout after two seconds."
+ 	"self waitForNextFrame: 1 timeout: 2000"
+ 
+ 	| now timeoutusecs |
+ 	(self cameraGetSemaphore: camNum) ifNotNil:
+ 		[:semaphoreIndex|
+ 		(Smalltalk externalObjectAt: semaphoreIndex ifAbsent: [self error: 'seriously?!!?!!?!!?']) waitTimeoutMSecs: timeoutms.
+ 		^self].
+ 	now := Time utcMicrosecondClock.
+ 	timeoutusecs := timeoutms * 1000.
+ 	[(self camera: camNum getParam: 1) > 0 ifTrue: [^self].
+ 	 (Time utcMicrosecondClock - now) < timeoutusecs] whileTrue:
+ 		[(Delay forMilliseconds: 50) wait]!



More information about the Squeak-dev mailing list