Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Refactor photodiodeSpot and photodiodeGhostBox #337

Merged
merged 15 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
},
plugins: ["react"],
rules: {
"no-unused-vars": "warn",
"react/prop-types": "off", // TODO #223: These should be added so the rule can be removed
"import/order": "warn",
},
Expand Down
216 changes: 109 additions & 107 deletions package-lock.json

Large diffs are not rendered by default.

56 changes: 31 additions & 25 deletions src/lib/markup/photodiode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,40 @@ import { config } from "../../config/main";
import { div, span } from "./tags";

/**
* Displays a box in the bottom right corner of the screen with the id "photodiode-spot"
* The box is only visible if config.USE_PHOTODIODE is true
* Markup for a box in the bottom right corner of the screen and a photodiode spot inside the ghost box
*
* Note the box will only be visible if USE_PHOTODIODE is true
* Note that this trial is only available when running in Electron
*/
function photodiodeGhostBox() {
const spot = span("", { id: "photodiode-spot", class: "photodiode-spot" });
return div(spot, {
id: "photodiode-box",
class: config.USE_PHOTODIODE ? "photodiode-box visible" : "photodiode-box invisible",
});
}
const photodiodeGhostBox = div(span("", { id: "photodiode-spot", class: "photodiode-spot" }), {
id: "photodiode-box",
// TODO #355: Conditional check should be at the task level
class: config.USE_PHOTODIODE ? "photodiode-box visible" : "photodiode-box invisible",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is config guaranteed to be defined when this is run? It always makes me a little nervous to put conditional stuff at the top level.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, but I agree I'd like to change it at some point.

});

/**
* Repeatedly flashes a spot inside the photodiodeGhostBox and communicates with the USB port
* Conditionally flashes a spot inside the photodiodeGhostBox and sends event codes to the serial port
*
* Note that this trial is only available when running in Electron
*
* @param {number} taskCode The code to be sent to the USB port (Electron only)
* Note that this function must be executed inside the "on_load" callback of a trial
* @param {number} taskCode The unique code for the given trial on which this function executes
*/
function photodiodeSpot(taskCode) {
function pdSpotEncode(taskCode) {
if (!config.USE_ELECTRON) {
throw new Error("photodiodeSpot trial is only available when running inside Electron");
}

// Pulse the spot color from black to white
if (config.USE_PHOTODIODE) {
const blinkTime = 40; // TODO #333: Get blink time from config.json (40ms is the default)
let numBlinks = taskCode; // TODO #354: Encode numBlinks in the event marker config
repeatPulseFor(blinkTime, numBlinks);
window.electronAPI.photodiodeTrigger(taskCode);
}

/**
* Pulses the photodiode spot from black (on) to white (off) and runs a callback function
* @param {number} ms The amount of time to flash the photodiode spot
* @param {function} callback A callback function to execute after the flash
*/
// TODO #331: Single photodiode color, pulse between visible and invisible here
function pulseFor(ms, callback) {
$(".photodiode-spot").css({ "background-color": "black" });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's jQuery in this???

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah! I'm afraid to touch it 😨

Expand All @@ -36,7 +46,11 @@ function photodiodeSpot(taskCode) {
}, ms);
}

// Repeat pulseFor i times
/**
* Recursive function that executes the pulseFor function i times
* @param {number} ms The amount of time to flash the photodiode spot and wait before recursion
* @param {number} i The number of times to repeat
*/
function repeatPulseFor(ms, i) {
if (i > 0) {
pulseFor(ms, () => {
Expand All @@ -46,14 +60,6 @@ function photodiodeSpot(taskCode) {
});
}
}

if (config.USE_PHOTODIODE) {
// TODO #333: Pass blinkTime as config setting
const blinkTime = 40;

repeatPulseFor(blinkTime, taskCode);
window.electronAPI.photodiodeTrigger(taskCode);
}
}

export { photodiodeGhostBox, photodiodeSpot };
export { photodiodeGhostBox, pdSpotEncode };
1 change: 1 addition & 0 deletions src/timelines/honeycombBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function createHoneycombBlock(jsPsych) {
duration: fixationSettings.randomize_duration
? jsPsych.randomization.sampleWithoutReplacement(fixationSettings.durations, 1)[0]
: fixationSettings.default_duration,
// TODO 280: Fixation will be recorded as "task: fixation" (data object, see below)
});

/**
Expand Down
1 change: 1 addition & 0 deletions src/timelines/honeycombTimeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
preloadTrial,
welcomeTrial,
} from "../trials/honeycombTrials";

import { createHoneycombBlock } from "./honeycombBlock";
import { preamble } from "./preamble";

Expand Down
4 changes: 2 additions & 2 deletions src/trials/camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function cameraStart(jsPsych) {
{
// Helps participant center themselves inside the camera
type: htmlButtonResponse,
stimulus: baseStimulus(markup, true) + photodiodeGhostBox(),
stimulus: baseStimulus(markup, true) + photodiodeGhostBox,
choices: [language.prompts.continue.button],
response_ends_trial: true,
on_start: () => {
Expand Down Expand Up @@ -91,7 +91,7 @@ function cameraEnd(jsPsych, duration) {

return {
type: htmlKeyboardResponse,
stimulus: baseStimulus(recordingEndMarkup, true) + photodiodeGhostBox(),
stimulus: baseStimulus(recordingEndMarkup, true) + photodiodeGhostBox,
trial_duration: duration,
on_start: () => {
// Complete the camera recording
Expand Down
2 changes: 1 addition & 1 deletion src/trials/holdUpMarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function holdUpMarker() {

return {
type: htmlButtonResponse,
stimulus: baseStimulus(eventMarkerMarkup, true) + photodiodeGhostBox(),
stimulus: baseStimulus(eventMarkerMarkup, true) + photodiodeGhostBox,
prompt: [p(language.trials.holdUpMarker)],
choices: [language.prompts.continue.button],
on_start: async () => {
Expand Down
8 changes: 4 additions & 4 deletions src/trials/startCode.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import htmlKeyboardResponse from "@jspsych/plugin-html-keyboard-response";

import { audioCodes, eventCodes, language } from "../config/main";
import { photodiodeSpot, photodiodeGhostBox } from "../lib/markup/photodiode";
import { pdSpotEncode, photodiodeGhostBox } from "../lib/markup/photodiode";
import { baseStimulus } from "../lib/markup/stimuli";
import { h1 } from "../lib/markup/tags";
import { beep } from "../lib/utils";
Expand All @@ -10,11 +10,11 @@ function startCode() {
const startCodeMarkup = h1(language.prompts.settingUp);
return {
type: htmlKeyboardResponse,
stimulus: baseStimulus(startCodeMarkup, true) + photodiodeGhostBox(),
stimulus: baseStimulus(startCodeMarkup, true) + photodiodeGhostBox,
trial_duration: 2000,
on_load: () => {
// Displays the photodiode spot and plays an audible beep
photodiodeSpot(eventCodes.open_task);
// Displays the photodiode spot and plays an audible beep when the trial first loads
pdSpotEncode(eventCodes.open_task);
beep(audioCodes);
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/trials/welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function showWelcome() {
const welcomeMarkup = h1(language.trials.welcome);
return {
type: htmlKeyboardResponse,
stimulus: baseStimulus(welcomeMarkup, true) + photodiodeGhostBox(),
stimulus: baseStimulus(welcomeMarkup, true) + photodiodeGhostBox,
prompt: language.prompts.continue.prompt,
response_ends_trial: true,
};
Expand Down