Code Reference¶
Arduino Code¶
SerialComms¶
-
getSerialInput
()¶ This function reads the data from the serial connection and returns it as a string.
This is used later to update the values
-
getSepIndex
(String input, char separator)¶ Returns the index of the separator character in a string.
-
UpdateGlobals
(String input)¶ This is a big ugly function which compares the input string to the names of variables that I have stored in memory; This is very much not the C way to do things...
I think this could be a hash table. I haven’t learned enough about hash table implementation yet and I know this works, so for the moment: If it ain’t broke...
sensors¶
-
senseLick
()¶ - check to see if the lick sensor has moved:
- if the sensor was in a low state, allow the calling of a rising edge.
- check if the sensor is above threshold
- report if the state of lickOn has change
single_port_setup¶
-
runTrial
()¶ returns 0 if the stimulus was applied returns 1 if a timeout is required until next trial
local variables and initialisation of the trial
t_init
is initialised such thatt_since
returns 0 at the start of the trial, and increases from there.trial_phase0 while the trial has not started
- update the time
- check for licks
- trigger the recording by putting
recTrig = HIGH
-
Habituation
()¶ - Determine the appropriate stimulus
- set active port
- counts number of sequential licks
- Uses the C ternary operator, which has the form:
A = boolean ? assignment if true : assignment if false;
the active port count gets incremented to a maximum of ten, while the non-active port count gets decremented to a minimum of zero. This is an important caveat as the counts are made with unsigned variables, meaning that one less than zero is actually 255!
- Uses the C ternary operator, which has the form:
States¶
-
ActiveDelay
(unsigned long wait, bool break_on_lick)¶ needs to be documented
-
deliver_reward
(bool water)¶ Open the water port on port for a duration defined by waterVol
-
punish
(int del)¶ plays the punishment tone / buzzer
-
Timeout
(unsigned long wait, int depth)¶ runs a time out
-
preTrial
()¶ - while the trial has not started
- update the time
- check for licks
- trigger the recording by putting recTrig -> HIGH
-
count_responses(int duration, bool lickTrig) {
Counts the number of hits on the lick sensor over duration of milliseconds.
-
TrialStimulus
()¶ runs the trial stimulus
-
conditional_tone
(int frequency, int duration)¶ wrapper function so I don’t need to put a billion if statements
Python Code¶
Reads the characters in the buffer and modifies the program parameters accordingly
Audio¶
-
band_limited_noise
(min_freq, max_freq, samples=1024, samplerate=1)¶ Generates noise within a particular band of frequencies
-
fftnoise
(f)¶ filter used in band_limited_noise
config_loader¶
-
write_out_config
(params, ID)¶ writes a subset of parameters to an ini file which can then be used to recover variable values.
-
ConfigSectionMap
(section, Config)¶ Helper function for reading configuration objects to a dictionary
-
restore_old_config
(ID)¶ reads the old ini file. checks to see if the current ID is in the ini sections restores the varible values in the global namespace (Hacky solution)
data_directories¶
-
timenow
()¶ provides the current time string in the form HH:MM:SS
Returns: the current time in the form HH:MM:SS Return type: str
-
today
()¶ provides today’s date as a string in the form YYMMDD
Returns: the date as a string in the form YYMMDD Return type: str
-
create_datapath
(DATADIR = "", date = today())¶ make a path to save the data based on today’s date
-
create_logfile
(DATADIR = "", date = today(), port = None, ID = None)¶ make a logfile to save communications, based on today’s date
serial_wrapper¶
-
timenow
() provides the current time string in the form HH:MM:SS
-
init_serialport
(port, logfile = None, ID = None)¶ Open communications with the arduino; quits the program if no communications are found on port.
If there are communications the script waits 500 ms then reads all incoming lines from the Serial port. These two lines include the arduino code version and a string that says the arduino is online
-
Serial_monitor
(ser, logfile, show = True, ID = None, verbose = None)¶ Reads from the serial port one line at time.
-
Continuous_monitor_arduino
(ser, end_trial_msg = "- Status Ready", sep = ':', debug_flags = (("#", "t#", "- ")), ID = None, verbose = None, logfile = None)¶ continously loops through messages from the serial monitor until the end_trial_msg is recieved. messages that are not preceded by debug_flags are stored in a dictionary which is returned by this function.
-
update_bbox
(ser, params, logfile, trial_df = {}, ID = None, verbose = None)¶ Communicates the contents of the dict params through the serial communications port.
data is sent in the form: parmas[key] = value –> key:value
trail_df dictionary is updated to include the parameters received from the arduino
numerical¶
Previously held functions to help with numerical handling, these have mostly been replaced, now contains a function for general conversion of strings to numbers
-
num
(s)¶ First attempts to convert string s to an integer. If that gives a ValueError then it attempts to return s as a float. Finally if s cannot be converted to a float or an int, the string is returned unchanged.