Skip to content

Instantly share code, notes, and snippets.

@millxing
Created June 5, 2019 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save millxing/e4091a293ad2c226c152087cc0ce28b4 to your computer and use it in GitHub Desktop.
Save millxing/e4091a293ad2c226c152087cc0ce28b4 to your computer and use it in GitHub Desktop.
simple matlab script to communicate with a monome grid usingf serial port functions
% get a list of devices connected to serial ports
% create a serial port object (s) -- on my computer it was the 6th device
a = seriallist;
s = serial(a{6});
s.Terminator = 'CR';
% open serial port object
fopen(s)
% FREAD: 3 bytes for the input buffer
% byte 1 = type of event of detected (33 is button press, 32 is button release)
% byte 2 = column of event
% byte 3 = row of event
% FWRITE: 4 bytes to the grid to control LEDs
% byte 1 = type of event of detected (33 is button press, 32 is button release)
% byte 2 = column of button
% byte 3 = row of button
% byte 4 = intensity of LED (0-15)
% apologies for the infinite loop - use Ctrl-Break to exit
% be sure to use fclose(s) after exiting the loop
while 1==1
% read input buffer
A = fread(s,3);
% if an event if detected
if ~isempty(A)
% turn on led
if A(1)==33
fwrite(s,[24 A(2) A(3) 15]);
% display column and row of button pressed
disp([A(2) A(3)]);
end
% turn off led
if A(1)==32
fwrite(s,[24 A(2) A(3) 0]);
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment