-
Notifications
You must be signed in to change notification settings - Fork 6
/
i2c2oled.sh
257 lines (236 loc) · 9.74 KB
/
i2c2oled.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/bin/bash
#
# File: "media/fat/i2c2oled/i2c2oled.sh"
#
# Just for fun ;-)
#
# 2021-04-18 by venice
# License GPL v3
#
# Using DE10-Nano's i2c Bus and Commands showing the MiSTer Logo on an connected SSD1306 OLED Display
#
# The SSD1306 is organized in eight 8-Pixel-High Pages (=Rows, 0..7) and 128 Columns (0..127).
# I you write an Data Byte you address an 8 Pixel high Column in one Page.
# Commands start with 0x00, Data with 0x40 (as far as I know)
#
# Initial Base for the Script taken from here:
# https://stackoverflow.com/questions/42980922/which-commands-do-i-have-to-use-ssd1306-over-i%C2%B2c
#
# Use Gimp or ImageMagick to convert the original to X-PixMap (XPM) and change " " (Space) to "1" and "." (Dot) to "0" for easier handling
# See examples what to modify additionally
# The String Array has 64 Lines with 128 Chars
# Put your X-PixMap files in /media/fat/i2c2oled_pix with extension "pix"
#
# 2021-04-28
# -Adding Basic Support for an 8x8 Pixel Font taken from https://github.com/greiman/SdFat under the MIT License
# Use modded ASCII functions from here https://gist.github.com/jmmitchell/c82b03e3fc2dc0dcad6c95224e42c453
# -Cosmetic changes
#
# 2021-04-29/30
# -Adding Font-Based Animation "pressplay" and "loading"
# The PIX's "pressplay.pix" and "loading.pix" are needed.
#
# 2021-05-01
# -Adding "Warp-5" Scrolling :-)
# The PIX "ncc1701.pix" is needed.
# -Using "font_width" instead of fixed value.
#
# 2021-05-15
# -Adding OLED Address Detection
# If Device is not found the Script ends with Error Code 1
# Use code from https://raspberrypi.stackexchange.com/questions/26818/check-for-i2c-device-presence
#
# 2021-05-17
# -Adding an "CONTRAST" variable so you can set your contrast value
#
# 2021-12-27
# -Adding "ROTATE" option and code from "MickGyver"
#
# 2021-12-29
# -Split Daemon Script into Daemon, User and System INI Files
# -New Option "ANIMATION" in User INI
# Set to -1 for Random Animation, 0 for NO Animation, 1 for PressPlay, 2..5 for "Loading" Variations
#
# 2021-12-30
# -New Loading Icons (iload3/iload4)
#
# 2022-01-01 (Happy new Year)
# -New Option "ANIMATION" in User INI
# Set to "yes" (default) for the short "Display-Blackout" before a Picture change
# -Cosmetics
#
# 2022-01-10
# -Cosmetics
# -Add Private Picture Folder pripath="/media/fat/i2c2oled/pri"
#
# 2022-02-27
# -Add Support for SSD1309 (2,42") Display (most of these Displays need to be modded from SPI to I2C)
# New Option SSD1309="yes/no"
# -Copied values with default settings to i2c2oled-system.ini
# -User INI i2c2oled-user.ini got higher priority to overrride default setting in i2c2oled-system.ini
# -Add Contrast function
#
# 2022-04-09
# -Make the client quiet (inotifywait -q)
#
# 2022-05-15
# -Make the client more quiet (inotifywait -qq)
# -Switch Page Adressing from Horizontal Mode to Page Mode (Needed for Support of the SSH1106 Display)
# -Add Support for OneColor Pictures
# Create the two folders /media/fat/i2c2oled/Pix_Onecolor and/or /media/fat/i2c2oled/PRI_Onecolor and add "OneColor" Pictures here.
#
# 2022-05-19
# -New Option SSH1106="yes/no"
# Add Support for SSH1106#
#
# 2022-05-20
# -New Option "INVERTHEADER="yes/no"
# Inverts the first 16 Pixel Lines, useful for OneColor Displays
#
# 2022-07-10
# -Github User ahmadexp add Support for an AD7414/AD7415 i2c Temperature Sensor mounted to RTC1.3
#
# 2022-08-25
# -Add Option USE_RANDOM_ALT="yes/no" for randomly chosen alternative PixMap
# -Show Startup Text only once each System Start (generate and check file "/tmp/i2c2oledinitdone")
#
# 2022-09-02..18
# -Add Screeensaver
#
#
# Load INI files
. /media/fat/i2c2oled/i2c2oled-system.ini
. /media/fat/i2c2oled/i2c2oled-user.ini
# ************************** Main Program **********************************
# Lookup for i2c Device
dbug "i2c2oled Hardware check."
#echo -e "\n\n${fyellow}i2c2oled Hardware check.${freset}"
mapfile -t i2cdata < <(i2cdetect -y ${i2cbus})
for i in $(seq 1 ${#i2cdata[@]}); do
i2cline=(${i2cdata[$i]})
echo ${i2cline[@]:1} | grep -q ${oledid}
if [ $? -eq 0 ]; then
#echo -e "${fgreen}OLED at 0x${oledid} found, proceed...${freset}"
dbug "OLED at 0x${oledid} found, proceed..."
oledfound="true"
fi
done
if [ "${oledfound}" = "false" ]; then
#echo -e "${fred}OLED at 0x${oledid} not found, end here!${freset}"
dbug "OLED at 0x${oledid} not found, end here!"
exit 1
fi
if [ ! -e ${initfile} ]; then
display_off # Switch Display off
init_display # Send INIT Commands
if [ "${debug}" = "true" ]; then
display_on # Debug
flushscreen # Fill the Screen completly
sleep 0.5 # Small sleep
fi
set_contrast ${CONTRAST} # Set Contrast
if [ "${debug}" = "true" ]; then
sleep 0.5 # Small sleep
fi
clearscreen # Clear the Screen completly
display_on # Switch Display on
cfont=${#font[@]} # Debugging get count font array members
dbug $cfont # Debugging
set_cursor 31 0 # Set Cursor at Page (Row) 0 to the 32th Pixel (Column)
showtext "i2c2oled" # Some Text for the Display
set_cursor 19 3 # Set Cursor at Page (Row) 3 to the 20th Pixel (Column)
showtext "MiSTer FPGA" # Some Text for the Display
set_cursor 19 5 # Set Cursor at Page (Row) 5 to the 20th Pixel (Column)
showtext "by Sorgelig" # Some Text for the Display
sleep ${SLIDETIME} # Wait a moment
touch ${initfile} # Init done
fi
if [ "${SHOW_TEMP}" = "yes" ]; then #initialize temperature sensor to default
init_temperature_default_config
fi
# reset_cursor
while true; do # main loop
if [ -r ${corenamefile} ]; then # proceed if file exists and is readable (-r)
newcore=$(cat ${corenamefile}) # get CORENAME
dbug "Read CORENAME: ${newcore}" # some debug output
if [ "${newcore}" != "${oldcore}" ]; then # proceed only if Core has changed
dbug "Send ${newcore} to i2c-${i2cbus}" # some debug output
if [[ ${newcore} != "MENU" && ${newcore} != "000-UPDATE" ]]; then # If Corename not "MENU" and not "Update"
dbug "${ANIMATION}"
if (( ${ANIMATION} == -1 )); then #
anirandom=$[$RANDOM%5+1] # Generate an Random Number between 0 and Modulo_Faktor-1, +1
else
anirandom=${ANIMATION} # ..or use the anmation type from User-INI
fi
dbug "${anirandom}"
if (( ${anirandom} == 1 )); then
pressplay # Run "pressplay" Animation
elif (( ${anirandom} == 2 )); then
loading 1 # Run "loading" Animation
elif (( ${anirandom} == 3 )); then
loading 2 # Run "loading" Animation
elif (( ${anirandom} == 4 )); then
loading 3 # Run "loading" Animation
elif (( ${anirandom} == 5 )); then
loading 4 # Run "loading" Animation
fi
fi # end if
if [ "${BLACKOUT}" = "yes" ]; then
display_off
fi
if [ ${newcore} = "THEEASTEREGG" ]; then
manythanksto
else
showpix ${newcore} # The "Magic"
fi
if [ "${BLACKOUT}" = "yes" ]; then
display_on
fi
SCREENSAVER_COUNTER=0 # Reset ScreenSaver Counter
#echo "SCREENSAVER_COUNTER: ${SCREENSAVER_COUNTER}" # debug
oldcore=${newcore} # Update oldcore Variable
fi # endif Core check
if [ "${SHOW_TEMP}" = "yes" ]; then
inotifywait -qq -t $SHOW_TEMP_INTERVAL -e modify "${corenamefile}" | show_temperature # show temperature while waiting for the core change event
elif [ "${SCREENSAVER}" = "yes" ]; then # Screensaver Active?
inotifywait -qq -t ${SCREENSAVER_INTERVAL} -e modify "${corenamefile}" # Wait for Corechange but with "SCREENSAVER_INTERVAL" Seconds as Timeout
SCREENSAVER_COUNTER=$((SCREENSAVER_COUNTER+1)) # Count up Screensaver Counter
if [[ ${SCREENSAVER_COUNTER} -ge 99 ]]; then # Check for "overflow"
SCREENSAVER_COUNTER=${SCREENSAVER_START}
fi
#echo "SCREENSAVER_COUNTER: ${SCREENSAVER_COUNTER}" # debug
if [[ ${SCREENSAVER_COUNTER} -ge ${SCREENSAVER_START} ]]; then # Screensaver Reday to Start ?
if [ "${BLACKOUT}" = "yes" ]; then # "Blackout" ?
display_off
fi
clearscreen # Clear the Screen for the Screensaver-Text
if [ $(( ${SCREENSAVER_COUNTER} % ${SCREENSAVER_SCREENS} )) -eq 0 ]; then # Get Text for Screensaver-Text
textout=$(date '+%R') # Show Time
elif [ $(( ${SCREENSAVER_COUNTER} % ${SCREENSAVER_SCREENS})) -eq 1 ]; then
textout=$(date '+%a %b %d') # Show Date
elif [ $(( ${SCREENSAVER_COUNTER} % ${SCREENSAVER_SCREENS})) -eq 2 ]; then
textout="i2c2oled" # Show Text "i2c2oled"
elif [ $(( ${SCREENSAVER_COUNTER} % ${SCREENSAVER_SCREENS} )) -eq 3 ]; then
textout="MiSTer FPGA" # Show Text "MiSTer FPGA"
elif [ $(( ${SCREENSAVER_COUNTER} % ${SCREENSAVER_SCREENS} )) -eq 4 ]; then
textout=${newcore} # Show CORENAME
fi
textoutlen=${#textout} # Get Text length
#echo "textout: ${textout}" # debug
#echo "textlen: ${textoutlen}" # debug
xpos=$((0 + $RANDOM % (127-8*${textoutlen}))) # 0..127-textlength*8
ypos=$((0 + $RANDOM % 7)) # 0..7
set_cursor ${xpos} ${ypos}
showtext "${textout}" # "" needed here or text after "space" is lost
if [ "${BLACKOUT}" = "yes" ]; then # "Blackout" ?
display_on
fi
fi
else
inotifywait -qq -e modify "${corenamefile}"
fi
else # CORENAME file not found
dbug "File ${corenamefile} not found!" # some debug output
fi # end if /tmp/CORENAME check
done # end while
# ************************** End Main Program *******************************