2.3.6.1.2. Bar graph with LEDs
2.3.6.1.2.1. Description
This example shows how to make a bar graph by controlling the Red Pitaya on-board LEDs. The number of LEDs that will be turned ON, corresponds to the value of variable p.
2.3.6.1.2.3. SCPI Code Examples
2.3.6.1.2.3.1. Code - MATLAB®
The code is written in MATLAB. In the code, we use SCPI commands and TCP client communication. Copy the code from below into the MATLAB editor, save the project, and hit the “Run” button. Change p from 0-100.
IP = '192.168.178.56'; % Input IP of your Red Pitaya...
port = 5000;
RP = tcpclient(IP, port);
%% Open connection with your Red Pitaya
RP.ByteOrder = "big-endian";
configureTerminator(RP,'CR/LF');
%% Define value p from 0 - 100 %
p = 67; % Set value of p
if p >=(100/7)
writeline(RP,'DIG:PIN LED1,1')
else
writeline(RP,'DIG:PIN LED1,0')
end
if p >=(100/7)*2
writeline(RP,'DIG:PIN LED2,1')
else
writeline(RP,'DIG:PIN LED2,0')
end
if p >=(100/7)*3
writeline(RP,'DIG:PIN LED3,1')
else
writeline(RP,'DIG:PIN LED3,0')
end
if p >=(100/7)*4
writeline(RP,'DIG:PIN LED4,1')
else
writeline(RP,'DIG:PIN LED4,0')
end
if p >=(100/7)*5
writeline(RP,'DIG:PIN LED5,1')
else
writeline(RP,'DIG:PIN LED5,0')
end
if p >=(100/7)*6
writeline(RP,'DIG:PIN LED6,1')
else
writeline(RP,'DIG:PIN LED6,0')
end
if p >=(100/7)*7
writeline(RP,'DIG:PIN LED7,1')
else
writeline(RP,'DIG:PIN LED7,0')
end
clear RP;
2.3.6.1.2.3.2. Code - Python
#!/usr/bin/env python3
import sys
import redpitaya_scpi as scpi
IP = 'rp-f066c8.local'
rp_s = scpi.scpi(IP)
if (len(sys.argv) > 2):
percent = int(sys.argv[2])
else:
percent = 50
print ("Bar showing "+str(percent)+"%")
for i in range(8):
if (percent > (i * (100.0/8))):
rp_s.tx_txt('DIG:PIN LED' + str(i) + ',' + str(1))
else:
rp_s.tx_txt('DIG:PIN LED' + str(i) + ',' + str(0))
rp_s.close()
2.3.6.1.2.4. API Code Examples
Note
The API code examples don’t require the use of the SCPI server. Instead, the code should be compiled and executed on the Red Pitaya itself (inside Linux OS). Instructions on how to compile the code and other useful information are here.
2.3.6.1.2.4.1. Code - C API
/* Red Pitaya C API example LED Bar graph */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "rp.h"
int main (int argc, char **argv) {
float percent;
// percentage can be provided as an argument
if (argc > 1) {
percent = atof(argv[1]);
} else {
percent = 50.0;
}
printf("Bar showing %.1f%%\n", percent);
// Initialization of API
if (rp_Init() != RP_OK) {
fprintf(stderr, "Red Pitaya API init failed!\n");
return EXIT_FAILURE;
}
// Turning on leds based on parameter percent
for (int i=0; i<8; i++) {
if (percent > (i*(100.0/8))) {
rp_DpinSetState(i+RP_LED0, RP_HIGH);
} else {
rp_DpinSetState(i+RP_LED0, RP_LOW);
}
}
// Releasing resources
rp_Release();
return EXIT_SUCCESS;
}
2.3.6.1.2.4.2. Code - Python API
#!/usr/bin/python3
import time
import rp
percent = 50 # Percentage of LED bar turned ON
is_integer = True
# Initialize the interface
rp.rp_Init()
#####! Choose one of two methods, comment the other !#####
#! METHOD 1: Interacting with Registers direclty
led = 0
led_array = [0b00000001, 0b00000010, 0b00000100, 0b00001000, 0b00010000, 0b00100000, 0b01000000, 0b10000000]
while 1:
led = 0
percent = input("Enter LED bar percentage: ")
try:
# Try to convert input to integer
int(percent)
except ValueError:
is_integer = False # set flag to false if the conversion fails
else:
is_integer = True
percent = int(percent) # convert input string to integer
if is_integer: # If input is integer
if not 0 <= percent <= 100: # In case of not defined percentage display default value
percent = 50
print (f"Bar showing {percent}%")
for i in range(8): # Calculate LED percentage
if percent > (i+1)*(100.0/9):
led += led_array[i] # Sum the bits together to get the final register value
rp.rp_LEDSetState(led)
else:
print("Invalid input")
time.sleep(0.2)
#! METHOD 2: Using Macros
led_array = [rp.RP_LED0, rp.RP_LED1, rp.RP_LED2, rp.RP_LED3, rp.RP_LED4, rp.RP_LED5, rp.RP_LED6, rp.RP_LED7]
while 1:
percent = input("Enter LED bar percentage: ")
try:
# Try to convert input to integer
int(percent)
except ValueError:
is_integer = False # set flag to false if the conversion fails
else:
is_integer = True
percent = int(percent) # convert input string to integer
if is_integer: # If input is integer
if not 0 <= percent <= 100: # In case of not defined percentage display default value
percent = 50
print (f"Bar showing {percent}%")
for i in range(8): # Calculate LED percentage
if percent > (i+1)*(100.0/9):
rp.rp_DpinSetState(led_array[i],rp.RP_HIGH)
else:
rp.rp_DpinSetState(led_array[i],rp.RP_LOW)
else:
print("Invalid input")
time.sleep(0.2)
# Release resources
rp.rp_Release()