| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from time import * |
| 8 |
from string import * |
| 9 |
|
| 10 |
from nemo.pdi import * |
| 11 |
from nemo.experiment import GenericDataPoint |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
buttonBox = 0 |
| 18 |
display = 0 |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
dt = 0 |
| 24 |
dataSet = None |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class ExampleClass: |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
def __init__(self, aDisplayHandle, aDataType): |
| 43 |
self.displayHandle = aDisplayHandle |
| 44 |
self.dataType = aDataType |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
self.rectangle1 = self.createRectangle(40, 35, 20, 30) |
| 49 |
self.rectangle1.name = self.rectangle1.name + '-1' |
| 50 |
self.rectangle1Showing = 0 |
| 51 |
|
| 52 |
self.rectangle2 = self.createRectangle(62, 35, 10, 20) |
| 53 |
self.rectangle2.name = self.rectangle2.name + '-2' |
| 54 |
self.rectangle2Showing = 0 |
| 55 |
|
| 56 |
self.circle = self.createCircle() |
| 57 |
self.circleShowing = 0 |
| 58 |
|
| 59 |
self.image = self.createImage() |
| 60 |
self.imageShowing = 0 |
| 61 |
|
| 62 |
self.line = self.createLine() |
| 63 |
self.lineShowing = 0 |
| 64 |
|
| 65 |
self.text = self.createText() |
| 66 |
|
| 67 |
|
| 68 |
self.running = 0 |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
def createRectangle(self, x, y, w, h): |
| 73 |
rectangle = GraphicFactory.createGraphic(GraphicFactory.FILL_RECTANGLE) |
| 74 |
|
| 75 |
rectangle.name = 'rectangle example' |
| 76 |
rectangle.description = 'a solid red rectangle' |
| 77 |
|
| 78 |
|
| 79 |
rectangle.red = 255 |
| 80 |
rectangle.green = 0 |
| 81 |
rectangle.blue = 0 |
| 82 |
|
| 83 |
|
| 84 |
rectangle.x = str(x) |
| 85 |
rectangle.y = str(y) |
| 86 |
rectangle.w = str(w) |
| 87 |
rectangle.h = str(h) |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
return rectangle |
| 93 |
|
| 94 |
|
| 95 |
def createCircle(self): |
| 96 |
circle = GraphicFactory.createGraphic(GraphicFactory.FILL_CIRCLE) |
| 97 |
|
| 98 |
circle.name = 'circle example' |
| 99 |
circle.description = 'a solid blue circle' |
| 100 |
|
| 101 |
|
| 102 |
circle.red = 0 |
| 103 |
circle.green = 0 |
| 104 |
circle.blue = 255 |
| 105 |
|
| 106 |
|
| 107 |
circle.x = '45' |
| 108 |
circle.y = '0' |
| 109 |
circle.diameter = '10' |
| 110 |
|
| 111 |
|
| 112 |
return circle |
| 113 |
|
| 114 |
|
| 115 |
def createText(self): |
| 116 |
text = GraphicFactory.createGraphic(GraphicFactory.DRAW_STRING) |
| 117 |
|
| 118 |
text.name = 'text example' |
| 119 |
text.description = 'sample text' |
| 120 |
|
| 121 |
|
| 122 |
text.red = 255 |
| 123 |
text.green = 255 |
| 124 |
text.blue = 255 |
| 125 |
text.alpha = 50 # This is for transparency |
| 126 |
|
| 127 |
|
| 128 |
text.x = '5' |
| 129 |
text.y = '88' |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
text.fontName = 'Arial' |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
text.fontStyle = text.BOLD | text.ITALIC |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
text.fontWidth = '90' |
| 142 |
text.fontHeight = '10' |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
text.alignment = text.ALIGN_CENTER |
| 150 |
|
| 151 |
|
| 152 |
text.text = 'enter graphic key or Q to quit' |
| 153 |
|
| 154 |
|
| 155 |
return text |
| 156 |
|
| 157 |
|
| 158 |
def createLine(self): |
| 159 |
line = GraphicFactory.createGraphic(GraphicFactory.DRAW_LINE) |
| 160 |
|
| 161 |
line.name = 'line example' |
| 162 |
line.description = 'an orange line' |
| 163 |
|
| 164 |
|
| 165 |
line.red = 255 |
| 166 |
line.green = 128 |
| 167 |
line.blue = 0 |
| 168 |
|
| 169 |
|
| 170 |
line.x1 = '32' |
| 171 |
line.y1 = '17' |
| 172 |
line.x2 = '25' |
| 173 |
line.y2 = '17' |
| 174 |
|
| 175 |
|
| 176 |
return line |
| 177 |
|
| 178 |
|
| 179 |
def createImage(self): |
| 180 |
image = GraphicFactory.createGraphic(GraphicFactory.DRAW_IMAGE) |
| 181 |
|
| 182 |
image.name = 'image example' |
| 183 |
image.description = 'a self-contained image' |
| 184 |
|
| 185 |
|
| 186 |
image.x = '75' |
| 187 |
image.y = '75' |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
image.w = '25' |
| 194 |
image.h = '25' |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
image.imageBytes = task.getResource('myImage.jpg') |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
image.isResource = 1 |
| 209 |
image.imageName = 'myImage.jpg' |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
return image |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
def logDataPoint(self, aDataPoint): |
| 220 |
|
| 221 |
|
| 222 |
if (self.dataLogger <> None): |
| 223 |
self.dataLogger.logDataPoint(aDataPoint) |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
def run(self): |
| 228 |
|
| 229 |
task.startRun('My Run Name') |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
self.dataLogger = task.startDataSet(self.dataType.getName(), 0) |
| 234 |
|
| 235 |
|
| 236 |
self.running = 1 |
| 237 |
|
| 238 |
|
| 239 |
self.displayHandle.showGraphics(0) |
| 240 |
|
| 241 |
|
| 242 |
self.displayHandle.addGraphic(0, self.text) |
| 243 |
|
| 244 |
|
| 245 |
def finish(self): |
| 246 |
|
| 247 |
self.running = 0 |
| 248 |
|
| 249 |
|
| 250 |
task.endDataSet(self.dataLogger) |
| 251 |
|
| 252 |
|
| 253 |
task.endRun() |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
self.dataLogger = None |
| 258 |
|
| 259 |
|
| 260 |
self.displayHandle.clearGraphics(0) |
| 261 |
|
| 262 |
|
| 263 |
sleep(7) |
| 264 |
|
| 265 |
|
| 266 |
task.endExperiment() |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
def handleDisplayEvent(self, anEvent): |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
client = anEvent.getClient() |
| 277 |
aTime = anEvent.getTime() |
| 278 |
type = anEvent.getType() |
| 279 |
|
| 280 |
|
| 281 |
graphicsList = "" |
| 282 |
graphics = anEvent.getGraphics() |
| 283 |
for graphicData in graphics: |
| 284 |
graphicsList = graphicsList + graphicData + " " |
| 285 |
|
| 286 |
graphicsList = graphicsList.strip() |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
typeText = "" |
| 291 |
if (type == GraphicalDisplay.HIDE): |
| 292 |
typeText = "HIDE" |
| 293 |
elif (type == GraphicalDisplay.SHOW): |
| 294 |
typeText = "SHOW" |
| 295 |
elif (type == GraphicalDisplay.DRAW): |
| 296 |
typeText = "ADD GRAPHIC" |
| 297 |
elif (type == GraphicalDisplay.CLEAR_GRAPHICS): |
| 298 |
typeText = "CLEAR ALL" |
| 299 |
elif (type == GraphicalDisplay.ADD_GRAPHICS): |
| 300 |
typeText = "ADD GRAPHICS" |
| 301 |
elif (type == GraphicalDisplay.CREATE_GROUP): |
| 302 |
typeText = "ADD GRAPHIC GROUP" |
| 303 |
|
| 304 |
|
| 305 |
gdp = GenericDataPoint() |
| 306 |
|
| 307 |
|
| 308 |
gdp.dt = self.dataType |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
gdp.setField("time", str(aTime)) |
| 314 |
gdp.setField("event", str(typeText) + ":{" + graphicsList + "}") |
| 315 |
|
| 316 |
|
| 317 |
self.logDataPoint(gdp) |
| 318 |
|
| 319 |
|
| 320 |
def handleButtonEvent(self, anEvent): |
| 321 |
|
| 322 |
if (not self.running): |
| 323 |
return |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
client = anEvent.getClient() |
| 330 |
pressTime = anEvent.getTime() |
| 331 |
key = anEvent.getData() |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
if (len(key) == 0): |
| 336 |
return |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
if (not key[0] in (letters+digits+'`~!@#$%^&*()-=_+{}[]\\|;:\'\",<.>/?')): |
| 341 |
return |
| 342 |
|
| 343 |
|
| 344 |
if (key == 'Q' or key == 'q'): |
| 345 |
|
| 346 |
self.finish() |
| 347 |
elif (key == 'C' or key == 'c'): |
| 348 |
|
| 349 |
if (self.circleShowing): |
| 350 |
self.displayHandle.removeGraphic(0, self.circle.name) |
| 351 |
else: |
| 352 |
self.displayHandle.addGraphic(0, self.circle) |
| 353 |
self.circleShowing = not self.circleShowing |
| 354 |
elif (key == 'L' or key == 'l'): |
| 355 |
|
| 356 |
if (self.lineShowing): |
| 357 |
self.displayHandle.removeGraphic(0, self.line.name) |
| 358 |
else: |
| 359 |
self.displayHandle.addGraphic(0, self.line) |
| 360 |
self.lineShowing = not self.lineShowing |
| 361 |
elif (key == '1'): |
| 362 |
|
| 363 |
if (self.rectangle1Showing): |
| 364 |
self.displayHandle.removeGraphic(0, self.rectangle1.name) |
| 365 |
else: |
| 366 |
self.displayHandle.addGraphic(0, self.rectangle1) |
| 367 |
self.rectangle1Showing = not self.rectangle1Showing |
| 368 |
elif (key == '2'): |
| 369 |
|
| 370 |
if (self.rectangle2Showing): |
| 371 |
self.displayHandle.removeGraphic(0, self.rectangle2.name) |
| 372 |
else: |
| 373 |
self.displayHandle.addGraphic(0, self.rectangle2) |
| 374 |
self.rectangle2Showing = not self.rectangle2Showing |
| 375 |
elif (key == 'I' or key == 'i'): |
| 376 |
|
| 377 |
if (self.imageShowing): |
| 378 |
self.displayHandle.removeGraphic(0, self.image.name) |
| 379 |
else: |
| 380 |
self.displayHandle.addGraphic(0, self.image) |
| 381 |
self.imageShowing = not self.imageShowing |
| 382 |
elif (key == '0'): |
| 383 |
|
| 384 |
self.displayHandle.clearGraphics(0) |
| 385 |
self.displayHandle.addGraphic(0, self.text) |
| 386 |
self.lineShowing = 0 |
| 387 |
self.imageShowing = 0 |
| 388 |
self.circleShowing = 0 |
| 389 |
self.rectangle1Showing = 0 |
| 390 |
self.rectangle2Showing = 0 |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
def handleScannerEvent(self, anEvent): |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
client = event.getClient() |
| 400 |
aTime = event.getTime() |
| 401 |
aType = event.getType() |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
typeText = "" |
| 406 |
if (aType == Scanner.START_SCAN): |
| 407 |
typeText = "STARTED SCANNER" |
| 408 |
elif (aType == Scanner.STOP_SCAN): |
| 409 |
typeText = "STOPPED SCANNER" |
| 410 |
elif (aType == Scanner.GET_IMAGES): |
| 411 |
typeText = "IMAGES" |
| 412 |
else: |
| 413 |
typeText = "UNKNOWN TYPE" |
| 414 |
|
| 415 |
|
| 416 |
gdp = GenericDataPoint() |
| 417 |
gdp.dt = self.dataType |
| 418 |
gdp.setField("time", str(aTime)) |
| 419 |
gdp.setField("event", str(typeText)) |
| 420 |
|
| 421 |
|
| 422 |
self.logDataPoint(gdp) |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
def fromDisplay(event): |
| 436 |
global exampleClassInstance |
| 437 |
|
| 438 |
exampleClassInstance.handleDisplayEvent(event) |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
def fromButtonBox(event): |
| 445 |
global exampleClassInstance |
| 446 |
|
| 447 |
exampleClassInstance.handleButtonEvent(event) |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
def fromScanner(event): |
| 454 |
global exampleClassInstance |
| 455 |
|
| 456 |
exampleClassInstance.handleScannerEvent(event) |
| 457 |
|
| 458 |
|
| 459 |
def start(): |
| 460 |
|
| 461 |
global exampleClassInstance |
| 462 |
|
| 463 |
|
| 464 |
task.sendResource(0, 'myImage.jpg') |
| 465 |
|
| 466 |
|
| 467 |
exampleClassInstance.run() |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
task.setExptSize(1) |
| 483 |
|
| 484 |
|
| 485 |
task.setExperimentName("Example Experiment") |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
task.setStartFunction(start) |
| 493 |
|
| 494 |
|
| 495 |
dataType = task.getDataType('example_data_type') |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
displayHandle = task.addDevice("Display", "nemo.pdi.GraphicalDisplay", fromDisplay) |
| 510 |
scanner = task.addDevice("Scanner", "nemo.pdi.Scanner", fromScanner) |
| 511 |
buttonBox = task.addDevice("ButtonBox", "nemo.pdi.ButtonBox", fromButtonBox) |
| 512 |
|
| 513 |
|
| 514 |
exampleClassInstance = ExampleClass(displayHandle, dataType) |