more notes for a bad memory - - -
Using an Arduino Mega with colour LCD (UTFT) display as a dumb i2c Raspberry Pi slave
Objective
Use an Arduino Mega to drive a dumb LCD display as the i2c slave of a Raspberry Pi i2c master.
Create and manipulate the strings in the Pi and send them to any x/y coordinates on the 320 x 480 LCD
This display is from Banggood
A C code program on the Pi sends out strings of bytes of chosen colour to an x/y coordinate on the LCD
The Arduino Mega receives the bytes and drives the display.
Command 2 is used for the strings
Command 3 clears the screen - - More to follow (change font etc)
String handling in C is impossible to remember if you only program every few months.
So I use Blassic basic to feed strings to the C code program.
See http://blassic.net/ . . . download the Raspberry Pi version of Blassic for /usr/sbin here)
Within a Blassic program
you can run bits of C code and Bash scripts, read and write to files.
- and move from strings to ints to floats to bytes to ASCII characters etc. with trivial ease.
Python could do it - but I know basic and you can follow the program meaning in plain english!!
The code comments explain the detail.
This code is my current backup and will be cleaned and upgraded at random times - but what is here works.
Pi-send_characters_to_MegaLCD_a.c
// /home/graham/UTFT_display/Pi-send_characters_to_MegaLCD_a.c // Send any length text string to x/y coordinate on Mega LCD // to compile use gcc Pi-send_characters_to_MegaLCD_a.c -o Pi-send_characters_to_MegaLCD_a // /home/graham/UTFT_display/Pi-Ard_send_9_characters_to_MegaLCD_a 5 2 0 0 1 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 - fill the top line (red)
int i2c; int main(int argc, char *argv[]) // --------------------------------------------------------the arduino i2c address // ----------------------------------------------------------the command byte for (i=1; i <= (argc - 3); i++){ printf("I2C: Connecting\n"); // use /dev/i2c-0 for Pi model B, /dev/i2c-1 for B+ and 2 printf("I2C: acquiring i2c address 0x%x\n", address); rc=ioctl(i2c,I2C_SLAVE,address); /* set the i2c chip address */ write(i2c, data, (argc-2)); //send (z+1) bytes on the i2c bus // As we are not talking to direct hardware but a microcontroller we // Now wait else you could crash the arduino by sending requests too fast (not tested) close(i2c); |
// MegSlaveLcd-text-2 // /home/graham/UTFT_display/Pi-send_characters_to_MegaLCD_a 5 2 0 0 1 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 - fill the top line (red) #include <Wire.h> #include <UTFT.h> int number = 0; word combined1 = 0; //- - word or double needed here - int fails (why??) //=====================================================================setup START myGLCD.InitLCD(); // initialize i2c as slave //---------------------------------------------------------------------------------loop START Flash_LED_13(); //show we are alive myGLCD.fillCircle(20,20,15); myGLCD.setColor(255, 110, 110); myGLCD.setColor(0, 255, 0); myGLCD.setColor(150, 150, 255); } if (command==2) { // **************************************************************************command = 2 if (cc == 1) { Serial.println (send_to_Pi); myGLCD.setColor(red, green, blue); for (int i=0; i <= (z-1); i++){ command=0; if (command==4) { // **************************************************************************command = 4 - set background color } //--------------------------------------------------------------------------------------loop END //______________________________________________________________________________________________ receive data START // §§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§§ what is the command byte? //++++++++++++++++++++++++++++++++++ data[0]=3 - clear the screen combined3 = word(data[6],data[5]); //_________________________________________________________ send data to Pi START - not needed for Mega display yet //=============================================================== functions below this line // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FUNCTION Flash_LED_13 |
#!/usr/sbin/blassic ' cut & paste to test NB! text string cannot contain spaces, _ (underline) is used in the input and it gets changed to a space DIM a$(30) i2c_address$ = PROGRAMARG$(1) colour$ = PROGRAMARG$(5) ' colour code 1=red 2=green 3=blue a$ = PROGRAMARG$(6) ' line of text to print on LCD - - - - _ will be spaces in the LCD string xx$ = STR$((VAL(xx$))/2) ' arguments xx and yy must be typed as even numbers up to 510 ' process any length of string into bytes PRINT "NumberOfCharacters = ", NumberOfCharacters aa$ = "" ' create the string of bytes separated by spaces to feed a string into Raspi C code program Pi-send_characters_to_MegaLCD_a screen_C_driver$ = "/home/graham/UTFT_display/Pi-send_characters_to_MegaLCD_a " + i2c_address$ + " " + command$ + " " + xx$ + " " + yy$ + " " + cc$ + aa$ SYSTEM |
Footnote!
I want to use the above with LM75 i2c thermometers - I found this script (thanks) -
#!/bin/bash while true; do i2cget -y 1 0x4F 0x00 w | awk '{printf("%.1f\n", (a=( \ (("0x"substr($1,5,2)substr($1,3,1))*0.0625)+0.1) \ )>128?a-256:a)}' sleep 5 done |
awk is magic to me - but it works - even for negative temperatures and seems to be able to resolve to 0.1 degrees.
Remove the \n in {printf("%.1f\n" to just output the number without the return
Test with some real data
- - - - - it's a bit cold this evening!
Use the awk magic to get LM75 i2c thermometer data (via long i2c line between house and greenhouse)
#!/bin/bash |
#!/bin/bash # /home/graham/housekeeping/lm75_0x48_once.sh i2cget -y 1 0x48 0x00 w | awk '{printf("%.1f", (a=( \ (("0x"substr($1,5,2)substr($1,3,1))*0.0625)+0.1) \ )>128?a-256:a)}' |
The Blassic control program drops down to system to get the data
then sends commands via C code to the Mega with LCD
#!/usr/sbin/blassic ' /home/graham/housekeeping/LM75ForMegaLCD.bas ' drop to system to run lm75_0x48_once.sh and lm75_0x4F_once.sh LABEL StartAgain SHELL "echo -n `/home/graham/housekeeping/lm75_0x48_once.sh`>/var/www/ramdisk/LM75_48.txt" SHELL "echo -n `/home/graham/housekeeping/lm75_0x4F_once.sh`>/var/www/ramdisk/LM75_4F.txt" PRINT LM75_48$ SendtoMega1$ = "/home/graham/housekeeping/stringsformegaLCD.bas 5 2 0 0 green Small_Ghs_=_" + LM75_48$ SendtoMega2$ = "/home/graham/housekeeping/stringsformegaLCD.bas 5 2 0 50 red Outside_=_" + LM75_4F$ GOTO StartAgain '----------------------------------------------------------------------------------------Subroutines SYSTEM |
Please email me if you want to swap notes