1+ from smile .common import Experiment , Log , Wait , Func , UntilDone , \
2+ Label , Loop , If , Elif , Else , KeyPress , Ref , \
3+ Parallel , Slider , Serial , UpdateWidget , Debug , Meanwhile , While , Subroutine
4+ from smile .video import Rectangle , TextInput , Button , ButtonPress
5+ from smile .mouse import MouseCursor
6+ from smile .startup import (
7+ INFO_WIDTH ,
8+ INFO_HEIGHT ,
9+ INFO_OUTLINE_COLOR ,
10+ INFO_COLOR ,
11+ INFO_FONT_SIZE ,
12+ INFO_BUTTON_HEIGHT ,
13+ INFO_BUTTON_WIDTH ,
14+ TEXT_INPUT_WIDTH ,
15+ TEXT_INPUT_HEIGHT
16+ )
17+ from smile .scale import scale as s
18+ from hashlib import blake2b
19+
20+ import config as CogBatt_config
21+
22+ def _validate_code (exp ):
23+ worker_id = exp ._subject
24+ code = exp .get_var ('_code' )
25+ expected_code = blake2b (worker_id .encode (), digest_size = 4 , salt = CogBatt_config .API_SALT .encode ()).hexdigest ()[:4 ]
26+ Debug (code = code , expected_code = expected_code , invalid = code != expected_code )
27+ exp .set_var ('code_invalid' , code != expected_code )
28+
29+ @Subroutine
30+ def InputSubject (self ):
31+ with Parallel ():
32+ with Parallel (blocking = False ):
33+ MouseCursor ()
34+ recOut = Rectangle (width = s (INFO_WIDTH ) + s (20 ),
35+ height = s (INFO_HEIGHT ) + s (20 ),
36+ color = INFO_OUTLINE_COLOR )
37+ recin = Rectangle (width = s (INFO_WIDTH ),
38+ height = s (INFO_HEIGHT ),
39+ color = INFO_COLOR )
40+ lbl = Label (text = CogBatt_config .EXP_NAME , center_x = recin .center_x ,
41+ top = recin .top - s (10 ),
42+ font_size = s (INFO_FONT_SIZE ))
43+ idIn = TextInput (width = s (TEXT_INPUT_WIDTH ),
44+ height = s (TEXT_INPUT_HEIGHT ),
45+ font_size = s (INFO_FONT_SIZE ),
46+ center_x = recin .center_x ,
47+ top = lbl .bottom - s (20 ),
48+ multiline = False ,
49+ text = "" ,
50+ disabled = False ,
51+ hint_text = "Prolific Worker ID" ,
52+ write_tab = False )
53+ codeIn = TextInput (width = s (TEXT_INPUT_WIDTH ),
54+ height = s (TEXT_INPUT_HEIGHT ),
55+ font_size = s (INFO_FONT_SIZE ),
56+ center_x = recin .center_x ,
57+ top = lbl .bottom - s (80 ),
58+ multiline = False ,
59+ text = "" ,
60+ disabled = False ,
61+ hint_text = "4 digit task code" ,
62+ write_tab = False )
63+ bc = Button (text = "Continue" , font_size = s (INFO_FONT_SIZE ),
64+ height = s (INFO_BUTTON_HEIGHT ),
65+ width = s (INFO_BUTTON_WIDTH ),
66+ right = recin .right - s (20 ),
67+ bottom = recin .bottom + s (20 ),
68+ name = "C" ,
69+ background_normal = "" ,
70+ background_color = INFO_OUTLINE_COLOR ,
71+ disabled = True )
72+ with Serial ():
73+ with While (
74+ (Ref .object (codeIn .text ).__len__ () < 4 )
75+ or (Ref (str , idIn .text ) == '' )
76+ ):
77+ Wait (0.1 )
78+ bc .disabled = False
79+
80+ bp = ButtonPress (buttons = [bc ])
81+ with If (
82+ (bp .pressed == "C" )
83+ ):
84+ Func (self .exp ._change_smile_subj , Ref .object (idIn .text ).lower ().strip ())
85+ Func (self .exp .set_var , '_code' , Ref .object (codeIn .text ).lower ().strip ())
86+ Func (_validate_code , Ref .object (self .exp ))
0 commit comments