def __init__(self, robot, screen_config, freq=2.0): # we have higher frequency to allow better read of buttons pypot.utils.StoppableLoopThread.__init__(self, freq) self.robot = robot self.serial = i2c(port=0, address=0x3c) self.display = ssd1327(self.serial, rotate=2, mode="1") # load screens with open(screen_config) as f: self.screens = json.load(f) self.currscreen = 'status' self.screen = self.screen_from_config(self.screens[self.currscreen]) # battery voltage reading # self.robot.volt = 0 # current battery # self.robot.volt_per = 0 # current battery percentage # self.volt_samples = 0 # we will do a running average # self.SAMPLES_LIMIT = 10 # number of samples for the average # # voltage history try: with open(self._history_file,'r') as f: # time on battery, last voltage tobstr, hvoltstr = f.readline().split(",") self.robot.tob = float(tobstr) self.last_volt = float(hvoltstr) except IOError: # file does not exist self.robot.tob = 0 self.last_volt = 0 self.last_read = time.time() # we will update the history in etc/battery.dat not at every update() cycle self.history_update = 0
def set_address(self, address): super(terrariumOLED, self).set_address(address) try: address = i2c(port=int(self.bus), address=int('0x' + str(self.address), 16)) if self.get_type() == terrariumOLEDSSD1306.TYPE: self.device = ssd1306(address) elif self.get_type() == terrariumOLEDSSD1309.TYPE: self.device = ssd1309(address) elif self.get_type() == terrariumOLEDSSD1322.TYPE: self.device = ssd1322(address) elif self.get_type() == terrariumOLEDSSD1325.TYPE: self.device = ssd1325(address) elif self.get_type() == terrariumOLEDSSD1327.TYPE: self.device = ssd1327(address) elif self.get_type() == terrariumOLEDSSD1331.TYPE: self.device = ssd1331(address) elif self.get_type() == terrariumOLEDSSD1351.TYPE: self.device = ssd1351(address) elif self.get_type() == terrariumOLEDSH1106.TYPE: self.device = sh1106(address) except DeviceNotFoundError as ex: print( '%s - WARNING - terrariumDisplay - %s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S,%f')[:23], ex)) self.device = None self.init()
def test_show(): """ SSD1327 OLED screen content can be displayed. """ device = ssd1327(serial) serial.reset_mock() device.show() serial.command.assert_called_once_with(175)
def test_init_128x128(): """ SSD1327 OLED with a 128 x 128 resolution works correctly. """ ssd1327(serial) serial.command.assert_has_calls([ call(174, 160, 83, 161, 0, 162, 0, 164, 168, 127), call(184, 1, 17, 34, 50, 67, 84, 101, 118), call(179, 0, 171, 1, 177, 241, 188, 8, 190, 7, 213, 98, 182, 15), call(129, 127), call(21, 0, 63, 117, 0, 127), call(175) ]) # Next 4096 are all data: zero's to clear the RAM # (4096 = 128 * 128 / 2) serial.data.assert_called_once_with([0] * (128 * 128 // 2))
def setupOled(): global oled, canvas, draw, font oled = ssd1327(spi, width=OLED_WIDTH, height=OLED_HEIGHT, framebuffer='full_frame') canvas = Image.new('RGB', oled.size) draw = ImageDraw.Draw(canvas)
def test_hide(): """ SSD1327 OLED screen content can be hidden. """ device = ssd1327(serial) serial.reset_mock() device.hide() serial.command.assert_called_once_with(174)
def test_init_128x128(): """ SSD1327 OLED with a 128 x 128 resolution works correctly. """ ssd1327(serial) serial.command.assert_has_calls([ call(174, 160, 83, 161, 0, 162, 0, 164, 168, 127, 184, 1, 17, 34, 50, 67, 84, 101, 118, 179, 0, 171, 1, 177, 241, 188, 8, 190, 7, 213, 98, 182, 15), call(129, 127), call(21, 0, 127, 117, 0, 127), call(175) ]) # Next 4096 are all data: zero's to clear the RAM # (4096 = 128 * 128 / 2) serial.data.assert_called_once_with([0] * (128 * 128 // 2))
def test_monochrome_display(): """ SSD1327 OLED screen can draw and display a monochrome image. """ device = ssd1327(serial, mode="1") serial.reset_mock() # Use the same drawing primitives as the demo with canvas(device) as draw: primitives(device, draw) # Initial command to reset the display serial.command.assert_called_once_with(21, 0, 63, 117, 0, 127) # Next 4096 bytes are data representing the drawn image serial.data.assert_called_once_with(get_json_data('demo_ssd1327_monochrome'))
def test_monochrome_display(): """ SSD1327 OLED screen can draw and display a monochrome image. """ device = ssd1327(serial, mode="1") serial.reset_mock() # Use the same drawing primitives as the demo with canvas(device) as draw: primitives(device, draw) # Initial command to reset the display serial.command.assert_called_once_with(21, 0, 127, 117, 0, 127) # Next 4096 bytes are data representing the drawn image serial.data.assert_called_once_with(get_json_data('demo_ssd1327_monochrome'))
def test_monochrome_display(): """ SSD1327 OLED screen can draw and display a monochrome image. """ device = ssd1327(serial, mode="1", framebuffer=full_frame()) serial.reset_mock() # Use the same drawing primitives as the demo with canvas(device) as draw: primitives(device, draw) # Initial command to reset the display serial.command.assert_called_once_with(21, 0, 63, 117, 0, 127) # To regenerate test data, uncomment the following (remember not to commit though) # ================================================================================ # from baseline_data import save_reference_data # save_reference_data("demo_ssd1327_monochrome", serial.data.call_args.args[0]) # Next 4096 bytes are data representing the drawn image serial.data.assert_called_once_with( get_reference_data('demo_ssd1327_monochrome'))
#!/usr/bin/python3 import rospy # from node_control.msg import Peripheral, Arm, Sensor, Joystick, Array from std_msgs.msg import Int16, String, Bool from mcgreen_control.msg import Array from luma.core.interface.serial import i2c, spi from luma.core.render import canvas from luma.oled.device import ssd1327 from PIL import ImageFont, ImageDraw from time import sleep import textwrap serial = spi(device=0, port=0) device = ssd1327(serial) font_size = 14 font_name = "FreeMono.ttf" font = ImageFont.truetype(font_name, font_size) class Screen: MODE_TOPIC = "/mode_feedback" GAME_TOPIC = "/current_game" UPPER_TOPIC = "/upper_safety" EXPRESSION_TOPIC = "/dot_matrix" LOWER_TOPIC = "/lower_safety" SAFETY_TOPIC = "/safety_feedback" def __init__(self): self.mode_sub = rospy.Subscriber(self.MODE_TOPIC, Int16, self.mode_set) self.game_sub = rospy.Subscriber(self.GAME_TOPIC, String, self.game_set) self.upper_sub = rospy.Subscriber(self.UPPER_TOPIC, Array, self.upper_set)
def test_framebuffer_override(): """ Reproduce https://github.com/rm-hull/luma.examples/issues/95 """ ssd1327(serial, mode="1", framebuffer="diff_to_previous")
def __init__(self, robot): self.robot = robot self.serial = i2c(port=0, address=0x3c) self.display = ssd1327(self.serial, rotate=2, mode="1")
# Get Config Setting and initialize the compatible OLED device # Compatible devices -> SSD1306, SSD1309, SSD1322, SSD1325, SSD1327, SSD1331, SSD1351 and SH1106 DSP_SET = displayConfig["DisplayType"] rot = displayConfig["Rotation"] disp = None if DSP_SET == "SSD1306": disp = ssd1306(serial, rotate=rot) elif DSP_SET == "SSD1309": disp = ssd1309(serial, rotate=rot) elif DSP_SET == "SSD1322": disp = ssd1322(serial, rotate=rot) elif DSP_SET == "SSD1325": disp = ssd1325(serial, rotate=rot) elif DSP_SET == "SSD1327": disp = ssd1327(serial, rotate=rot) elif DSP_SET == "SSD1331": disp = ssd1331(serial, rotate=rot) elif DSP_SET == "SSD1351": disp = ssd1351(serial, rotate=rot) elif DSP_SET == "SH1106": disp = sh1106(serial, rotate=rot) # disp = sh1106(serial, rotate=2) # disp = ssd1306(serial, rotate=0) def getLargeFont(): return ImageFont.truetype(workDir + "/Fonts/Georgia Bold.ttf", 12)
def plugin_init(self, enableplugin=None): plugin.PluginProto.plugin_init(self, enableplugin) if self.enabled: i2cport = -1 try: for i in range(0, 2): if gpios.HWPorts.is_i2c_usable( i) and gpios.HWPorts.is_i2c_enabled(i): i2cport = i break except: i2cport = -1 if i2cport > -1: if self.interval > 2: nextr = self.interval - 2 else: nextr = self.interval self.initialized = False serialdev = None self.taskdevicepluginconfig[1] = int( float(self.taskdevicepluginconfig[1])) if self.taskdevicepluginconfig[1] != 0: # i2c address serialdev = i2c(port=i2cport, address=self.taskdevicepluginconfig[1]) else: return self.initialized self.device = None try: if "x" in str(self.taskdevicepluginconfig[3]): resstr = str(self.taskdevicepluginconfig[3]).split('x') self.width = int(resstr[0]) self.height = int(resstr[1]) else: self.width = None self.height = None except: self.width = None self.height = None if str(self.taskdevicepluginconfig[0]) != "0" and str( self.taskdevicepluginconfig[0]).strip( ) != "": # display type try: if str(self.taskdevicepluginconfig[0]) == "ssd1306": from luma.oled.device import ssd1306 if self.height is None: self.device = ssd1306( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1306( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "sh1106": from luma.oled.device import sh1106 if self.height is None: self.device = sh1106( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = sh1106( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "ssd1309": from luma.oled.device import ssd1309 if self.height is None: self.device = ssd1309( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1309( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "ssd1331": from luma.oled.device import ssd1331 if self.height is None: self.device = ssd1331( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1331( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "ssd1351": from luma.oled.device import ssd1351 if self.height is None: self.device = ssd1351( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1351( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "ssd1322": from luma.oled.device import ssd1322 if self.height is None: self.device = ssd1322( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1322( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "ssd1325": from luma.oled.device import ssd1325 if self.height is None: self.device = ssd1325( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1325( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "ssd1327": from luma.oled.device import ssd1327 if self.height is None: self.device = ssd1327( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1327( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True except Exception as e: misc.addLog(rpieGlobals.LOG_LEVEL_ERROR, "OLED can not be initialized! " + str(e)) self.enabled = False self.device = None return False if self.device is not None: try: lc = int(self.taskdevicepluginconfig[4]) except: lc = 1 if lc < 1: lc = 1 elif lc > 4: lc = 4 try: defh = 10 if self.height != 64: # correct y coords defh = int(defh * (self.height / 64)) self.hfont = ImageFont.truetype( 'img/UbuntuMono-R.ttf', defh) lineheight = 11 if lc == 1: lineheight = 24 self.ypos = [20, 0, 0, 0] elif lc == 2: lineheight = 16 self.ypos = [15, 34, 0, 0] elif lc == 3: lineheight = 12 self.ypos = [13, 25, 37, 0] elif lc == 4: lineheight = 10 self.ypos = [12, 22, 32, 42] if self.height != 64: # correct y coords for p in range(len(self.ypos)): self.ypos[p] = int(self.ypos[p] * (self.height / 64)) lineheight = int(lineheight * (self.height / 64)) self.ufont = ImageFont.truetype( 'img/UbuntuMono-R.ttf', lineheight) # use size except Exception as e: print(e) try: self.device.show() except: pass if self.interval > 2: nextr = self.interval - 2 else: nextr = 0 self._lastdataservetime = rpieTime.millis() - (nextr * 1000) try: self.dispimage = Image.new( '1', (self.device.width, self.device.height), "black") self.conty1 = 12 if self.height != 64: # correct y coords self.conty1 = int(self.conty1 * (self.height / 64)) self.conty2 = self.device.height - self.conty1 self.textbuffer = [] self.actualpage = 0 self.lastlineindex = self.P36_Nlines for l in reversed(range(self.P36_Nlines)): if (str(self.lines[l]).strip() != "") and (str( self.lines[l]).strip() != "0"): self.lastlineindex = l break try: self.pages = math.ceil( (self.lastlineindex + 1) / int(self.taskdevicepluginconfig[4])) except: self.pages = 0 except Exception as e: self.initialized = False try: cont = int(self.taskdevicepluginconfig[6]) except: cont = 0 if cont > 0: self.device.contrast(cont) draw = ImageDraw.Draw(self.dispimage) maxcols = int(self.taskdevicepluginconfig[7] ) # auto decrease font size if needed if maxcols < 1: maxcols = 1 tstr = "X" * maxcols try: sw = draw.textsize(tstr, self.ufont)[0] except: sw = self.device.width while (sw > self.device.width): lineheight -= 1 self.ufont = ImageFont.truetype( 'img/UbuntuMono-R.ttf', lineheight) sw = draw.textsize(tstr, self.ufont)[0] self.writeinprogress = 0 else: self.initialized = False else: self.__del__()
def main(argv): # Check and get arguments try: options, remainder = getopt.getopt(argv, '', ['help', 'interface=', 'i2c-port=', 'i2c-address=', 'display=', 'display-width=', 'display-height=', 'display-theme=', 'follow=', 'refresh=', 'latitude=', 'longitude=']) except getopt.GetoptError: l.usage() sys.exit(2) for opt, arg in options: if opt == '--help': l.usage() sys.exit() elif opt in ('--interface'): if arg not in ['i2c', 'spi']: print('Unknown interface type (choose between \'i2c\' and \'spi\')') sys.exit() s.interface = arg elif opt in ('--i2c-port'): s.i2c_port = int(arg) elif opt in ('--i2c-address'): s.i2c_address = int(arg, 16) elif opt in ('--display'): if arg not in ['sh1106', 'ssd1306', 'ssd1327', 'ssd1351', 'st7735']: print('Unknown display type (choose between \'sh1106\', \'ssd1306\', \'ssd1327\', \'ssd1351\' and \'st7735\')') sys.exit() s.display = arg elif opt in ('--display-width'): s.display_width = int(arg) elif opt in ('--display-height'): s.display_height = int(arg) elif opt in ('--follow'): if arg in ['RRF', 'TECHNIQUE', 'INTERNATIONAL', 'LOCAL', 'BAVARDAGE', 'FON']: s.room_current = arg else: tmp = l.scan(arg) if tmp is False: s.room_current = 'RRF' else: s.room_current = tmp s.callsign = arg s.scan = True elif opt in ('--refresh'): s.refresh = float(arg) elif opt in ('--latitude'): s.latitude = float(arg) elif opt in ('--longitude'): s.longitude = float(arg) elif opt in ('--display-theme'): s.display_theme = arg # Set serial if s.interface == 'i2c': serial = i2c(port=s.i2c_port, address=s.i2c_address) if s.display == 'sh1106': s.device = sh1106(serial, width=s.display_width, height=s.display_height, rotate=0) elif s.display == 'ssd1306': s.device = ssd1306(serial, width=s.display_width, height=s.display_height, rotate=0) elif s.display == 'ssd1327': s.device = ssd1327(serial, width=s.display_width, height=s.display_height, rotate=0, mode='RGB') else: serial = spi(device=0, port=0) if s.display == 'ssd1351': s.device = ssd1351(serial, width=s.display_width, height=s.display_height, rotate=1, mode='RGB', bgr=True) elif s.display == 'st7735': s.device = st7735(serial, width=s.display_width, height=s.display_height, rotate=3, mode='RGB') init_message = [] # Let's go init_message.append('RRFDisplay ' + s.version) init_message.append('') init_message.append('88 et 73 de F4HWN') init_message.append('') d.display_init(init_message) # Lecture du fichier de theme init_message.append('Chargement Theme') d.display_init(init_message) s.theme = cp.ConfigParser() s.theme.read('./themes/' + s.display_theme) # Lecture initiale de la progation et du cluster init_message.append('Requete Propagation') d.display_init(init_message) l.get_solar() init_message.append('Requete Cluster') d.display_init(init_message) l.get_cluster() init_message.append('Let\'s go') d.display_init(init_message) # Boucle principale s.timestamp_start = time.time() rrf_data = '' rrf_data_old = '' #print s.scan #print s.callsign #print s.room_current while(True): chrono_start = time.time() tmp = datetime.datetime.now() s.day = tmp.strftime('%Y-%m-%d') s.now = tmp.strftime('%H:%M:%S') s.hour = int(tmp.strftime('%H')) s.minute = int(s.now[3:-3]) s.seconde = int(s.now[-2:]) if s.seconde % 15 == 0 and s.scan == True: # On scan tmp = l.scan(s.callsign) if tmp is not False: #print s.now, tmp s.room_current = tmp if s.minute == 0: # Update solar propagation l.get_solar() if s.minute % 4 == 0: # Update cluster l.get_cluster() url = s.room[s.room_current]['url'] # Requete HTTP vers le flux json du salon produit par le RRFDisplay try: r = requests.get(url, verify=False, timeout=0.5) except requests.exceptions.ConnectionError as errc: #print ('Error Connecting:', errc) pass except requests.exceptions.Timeout as errt: #print ('Timeout Error:', errt) pass # Controle de la validité du flux json try: rrf_data = r.json() except: pass if rrf_data != '' and rrf_data != rrf_data_old: # Si le flux est valide rrf_data_old = rrf_data data_abstract = rrf_data['abstract'][0] data_activity = rrf_data['activity'] data_transmit = rrf_data['transmit'][0] data_last = rrf_data['last'] data_all = rrf_data['all'] s.message[1] = l.sanitize_call(data_last[0]['Indicatif']) s.message[2] = l.sanitize_call(data_last[1]['Indicatif']) s.message[3] = l.sanitize_call(data_last[2]['Indicatif']) if s.device.height == 128: # Only if place... try: data_elsewhere = rrf_data['elsewhere'][0] i = 0 s.transmit_elsewhere = False for data in rrf_data['elsewhere'][6]: if data in ['RRF', 'TECHNIQUE', 'INTERNATIONAL', 'LOCAL', 'BAVARDAGE', 'FON']: tmp = rrf_data['elsewhere'][6][data] if tmp != 0: s.transmit_elsewhere = True s.raptor[i] = l.convert_second_to_time(tmp) + '/' + data[:3] + '/' + l.sanitize_call(rrf_data['elsewhere'][1][data]) + '/' + str(rrf_data['elsewhere'][5][data]) else: s.raptor[i] = l.convert_second_to_time(tmp) + '/' + data[:3] + '/' + l.convert_time_to_string(rrf_data['elsewhere'][3][data]) + '/' + str(rrf_data['elsewhere'][5][data]) i += 1 except: pass if data_transmit['Indicatif'] != '': if s.transmit is False: # Wake up screen... s.transmit = l.wake_up_screen(s.device, s.display, s.transmit) s.call_current = l.sanitize_call(data_transmit['Indicatif']) s.call_type = data_transmit['Type'] s.call_description = data_transmit['Description'] s.call_tone = data_transmit['Tone'] s.call_locator = data_transmit['Locator'] s.call_sysop = data_transmit['Sysop'] s.call_prenom = data_transmit['Prenom'] s.call_latitude = data_transmit['Latitude'] s.call_longitude = data_transmit['Longitude'] s.duration = data_transmit['TOT'] else: if s.transmit is True: # Sleep screen... s.transmit = l.wake_up_screen(s.device, s.display, s.transmit) # Load Histogram for q in range(0, 24): s.qso_hour[q] = data_activity[q]['TX'] # Load Last limit = len(rrf_data['last']) s.call = [''] * 10 s.call_time = [''] * 10 for q in range(0, limit): s.call[q] = l.sanitize_call(rrf_data['last'][q]['Indicatif']) s.call_time[q] = rrf_data['last'][q]['Heure'] # Load Best limit = len(rrf_data['all']) s.best = [''] * 10 s.best_time = [0] * 10 for q in range(0, limit): s.best[q] = l.sanitize_call(rrf_data['all'][q]['Indicatif']) s.best_time[q] = l.convert_time_to_second(rrf_data['all'][q]['Durée']) if(s.seconde < 10): # TX today s.message[0] = 'TX total ' + str(data_abstract['TX total']) elif(s.seconde < 20): # Active node s.message[0] = 'Links actifs ' + str(data_abstract['Links actifs']) elif(s.seconde < 30): # Online node s.message[0] = 'Links total ' + str(data_abstract['Links connectés']) elif(s.seconde < 40): # Total emission tmp = l.convert_time_to_string(data_abstract['Emission cumulée']) if 'h' in tmp: tmp = tmp[0:6] s.message[0] = 'BF total ' + tmp elif(s.seconde < 50): # Last TX s.message[0] = 'Dernier ' + data_last[0]['Heure'] elif(s.seconde < 60): # Scan if s.scan is True: s.message[0] = 'Suivi de ' + s.callsign else: s.message[0] = 'Salon ' + s.room_current[:3] # Print screen if s.device.height == 128: d.display_128() else: d.display_64() chrono_stop = time.time() chrono_time = chrono_stop - chrono_start if chrono_time < s.refresh: sleep = s.refresh - chrono_time else: sleep = 0 #print "Temps d'execution : %.2f %.2f secondes" % (chrono_time, sleep) #sys.stdout.flush() time.sleep(sleep)
def test_framebuffer_override(): """ Reproduce https://github.com/rm-hull/luma.examples/issues/95 """ ssd1327(serial, mode="1", framebuffer=diff_to_previous())
def plugin_init(self, enableplugin=None): plugin.PluginProto.plugin_init(self, enableplugin) if self.enabled: try: i2cl = self.i2c except: i2cl = -1 try: i2cport = gpios.HWPorts.geti2clist() if i2cl == -1: i2cl = int(i2cport[0]) except: i2cport = [] if len(i2cport) > 0 and i2cl > -1: if self.interval > 2: nextr = self.interval - 2 else: nextr = self.interval self.initialized = False serialdev = None self.taskdevicepluginconfig[1] = int( float(self.taskdevicepluginconfig[1])) if self.taskdevicepluginconfig[1] != 0: # i2c address serialdev = i2c(port=i2cl, address=self.taskdevicepluginconfig[1]) else: return self.initialized self.device = None try: if "x" in str(self.taskdevicepluginconfig[3]): resstr = str(self.taskdevicepluginconfig[3]).split('x') self.width = int(resstr[0]) self.height = int(resstr[1]) else: self.width = None self.height = None except: self.width = None self.height = None if str(self.taskdevicepluginconfig[0]) != "0" and str( self.taskdevicepluginconfig[0]).strip( ) != "": # display type try: if str(self.taskdevicepluginconfig[0]) == "ssd1306": from luma.oled.device import ssd1306 if self.height is None: self.device = ssd1306( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1306( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "sh1106": from luma.oled.device import sh1106 if self.height is None: self.device = sh1106( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = sh1106( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "ssd1309": from luma.oled.device import ssd1309 if self.height is None: self.device = ssd1309( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1309( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "ssd1331": from luma.oled.device import ssd1331 if self.height is None: self.device = ssd1331( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1331( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "ssd1351": from luma.oled.device import ssd1351 if self.height is None: self.device = ssd1351( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1351( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "ssd1322": from luma.oled.device import ssd1322 if self.height is None: self.device = ssd1322( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1322( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "ssd1325": from luma.oled.device import ssd1325 if self.height is None: self.device = ssd1325( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1325( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True elif str(self.taskdevicepluginconfig[0]) == "ssd1327": from luma.oled.device import ssd1327 if self.height is None: self.device = ssd1327( serialdev, rotate=int( float(self.taskdevicepluginconfig[2]))) else: self.device = ssd1327( serialdev, rotate=int( float(self.taskdevicepluginconfig[2])), width=self.width, height=self.height) self.initialized = True except Exception as e: misc.addLog(rpieGlobals.LOG_LEVEL_ERROR, "OLED can not be initialized! " + str(e)) self.enabled = False self.device = None return False if self.device is not None: try: lc = int(self.taskdevicepluginconfig[4]) except: lc = self.P23_Nlines if lc < 1: lc = self.P23_Nlines lineheight = int( self.device.height / lc) # lineheight = int(self.device.height / lc)+1 self.ufont = ImageFont.truetype('img/UbuntuMono-R.ttf', lineheight) try: self.device.show() except: pass with canvas(self.device) as draw: maxcols = int(self.taskdevicepluginconfig[5]) if maxcols < 1: maxcols = 1 tstr = "X" * maxcols try: sw = draw.textsize(tstr, self.ufont)[0] except: sw = self.device.width while (sw > self.device.width): lineheight -= 1 self.ufont = ImageFont.truetype( 'img/UbuntuMono-R.ttf', lineheight) sw = draw.textsize(tstr, self.ufont)[0] self.charwidth, self.lineheight = draw.textsize( "X", self.ufont) if lc in [2, 4, 6, 8]: self.lineheight += 1 if self.interval > 2: nextr = self.interval - 2 else: nextr = 0 self._lastdataservetime = rpieTime.millis() - (nextr * 1000) self.dispimage = Image.new( '1', (self.device.width, self.device.height), "black") else: self.initialized = False
def connect_oled_i2c_spi(app, cfg): """connect to oled I2c SPI""" try: # OLED DISPLAY 2 SETUP app.devices = cfg.get('OLED DISPLAY 2 SETUP', 'oled_devices').strip('"') app.i2c_or_spi = cfg.get('OLED DISPLAY 2 SETUP', 'oled_i2c_or_spi').strip('"') app.spi_gpio_dc_pin = int(cfg.get('OLED DISPLAY 2 SETUP', 'oled_spi_gpio_dc_pin').strip('"')) app.spi_gpio_rst_pin = int(cfg.get('OLED DISPLAY 2 SETUP', 'oled_spi_gpio_rst_pin')) app.port_address = cfg.get('OLED DISPLAY 2 SETUP', 'oled_port_address').strip('"') app.spi_device_number = int(cfg.get('OLED DISPLAY 2 SETUP', 'oled_spi_device_number')) app.port = int(cfg.get('OLED DISPLAY 2 SETUP', 'oled_port').strip('"')) app.color_mode = cfg.get('OLED DISPLAY 2 SETUP', 'oled_color_mode').strip('"') app.screen_width = int(cfg.get('OLED DISPLAY 2 SETUP', 'oled_width').strip('"')) app.screen_height = int(cfg.get('OLED DISPLAY 2 SETUP', 'oled_height').strip('"')) app.rotate_screen = int(cfg.get('OLED DISPLAY 2 SETUP', 'oled_rotate').strip('"')) # Logo / States Images app.showlogo = cfg.get('OLED DISPLAY 2 TEXT', 'oled_showlogo').strip('"') app.logos = cfg.get('OLED DISPLAY 2 TEXT', 'oled_logos').strip('"') app.logo_path = cfg.get('OLED DISPLAY 2 TEXT', 'oled_logo_path').strip('"') app.states_pictures = cfg.get('OLED DISPLAY 2 TEXT', 'oled_states_pictures').strip('"') app.state_picture_path = cfg.get('OLED DISPLAY 2 TEXT', 'oled_state_picture_path').strip('"') # Text 1, Counter, Date-Time app.font_1 = cfg.get('OLED DISPLAY 2 TEXT', 'oled_font_1').strip('"') app.counter_1 = cfg.get('OLED DISPLAY 2 TEXT', 'oled_counter_type1').strip('"') app.text1_color = cfg.get('OLED DISPLAY 2 TEXT', 'oled_text1_color').strip('"') app.text_1 = cfg.get('OLED DISPLAY 2 TEXT', 'oled_text_1').strip('"') app.size_1 = int(cfg.get('OLED DISPLAY 2 TEXT', 'oled_size_1').strip('"')) app.right_1 = int(cfg.get('OLED DISPLAY 2 TEXT', 'oled_text1_right').strip('"')) app.down_1 = int(cfg.get('OLED DISPLAY 2 TEXT', 'oled_text1_down').strip('"')) # Text 2, Counter, Date-Time app.font_2 = cfg.get('OLED DISPLAY 2 TEXT', 'oled_font_2').strip('"') app.counter_2 = cfg.get('OLED DISPLAY 2 TEXT', 'oled_counter_type2').strip('"') app.text2_color = cfg.get('OLED DISPLAY 2 TEXT', 'oled_text2_color').strip('"') app.text_2 = cfg.get('OLED DISPLAY 2 TEXT', 'oled_text_2').strip('"') app.size_2 = int(cfg.get('OLED DISPLAY 2 TEXT', 'oled_size_2').strip('"')) app.right_2 = int(cfg.get('OLED DISPLAY 2 TEXT', 'oled_text2_right').strip('"')) app.down_2 = int(cfg.get('OLED DISPLAY 2 TEXT', 'oled_text2_down').strip('"')) # Text 3, Counter, Date-Time app.font_3 = cfg.get('OLED DISPLAY 2 TEXT', 'oled_font_3').strip('"') app.counter_3 = cfg.get('OLED DISPLAY 2 TEXT', 'oled_counter_type3').strip('"') app.text3_color = cfg.get('OLED DISPLAY 2 TEXT', 'oled_text3_color').strip('"') app.text_3 = cfg.get('OLED DISPLAY 2 TEXT', 'oled_text_3').strip('"') app.size_3 = int(cfg.get('OLED DISPLAY 2 TEXT', 'oled_size_3').strip('"')) app.right_3 = int(cfg.get('OLED DISPLAY 2 TEXT', 'oled_text3_right').strip('"')) app.down_3 = int(cfg.get('OLED DISPLAY 2 TEXT', 'oled_text3_down').strip('"')) # Text 4, Counter, Date-Time app.font_4 = cfg.get('OLED DISPLAY 2 TEXT', 'oled_font_4').strip('"') app.counter_4 = cfg.get('OLED DISPLAY 2 TEXT', 'oled_counter_type4').strip('"') app.text4_color = cfg.get('OLED DISPLAY 2 TEXT', 'oled_text4_color').strip('"') app.text_4 = cfg.get('OLED DISPLAY 2 TEXT', 'oled_text_4').strip('"') app.size_4 = int(cfg.get('OLED DISPLAY 2 TEXT', 'oled_size_4').strip('"')) app.right_4 = int(cfg.get('OLED DISPLAY 2 TEXT', 'oled_text4_right').strip('"')) app.down_4 = int(cfg.get('OLED DISPLAY 2 TEXT', 'oled_text4_down').strip('"')) except OSError: pass try: # Choose I2c or SPI connection i = app.i2c_or_spi.split() if "SPI" in i: app.serial = spi(device=app.spi_device_number, port=app.port, gpio_DC=app.spi_gpio_dc_pin, gpio_RST=app.spi_gpio_rst_pin) elif "I2c" in i: app.serial = i2c(port=app.port, address=app.port_address) except OSError: pass try: # Connect to screen d = app.devices.split() if "sh1106" in d: app.device = sh1106(app.serial, rotate=app.rotate_screen, width=app.screen_width, height=app.screen_height) elif "ssd1306" in d: app.device = ssd1306(app.serial, rotate=app.rotate_screen, width=app.screen_width, height=app.screen_height) elif "ssd1309" in d: app.device = ssd1309(app.serial, rotate=app.rotate_screen, width=app.screen_width, height=app.screen_height) elif "ssd1322" in d: app.device = ssd1322(app.serial, rotate=app.rotate_screen, width=app.screen_width, height=app.screen_height) elif "ssd1325" in d: app.device = ssd1325(app.serial, rotate=app.rotate_screen, width=app.screen_width, height=app.screen_height) elif "ssd1327" in d: app.device = ssd1327(app.serial, rotate=app.rotate_screen, width=app.screen_width, height=app.screen_height) elif "ssd1331" in d: app.device = ssd1331(app.serial, rotate=app.rotate_screen, width=app.screen_width, height=app.screen_height) elif "ssd1351" in d: app.device = ssd1351(app.serial, rotate=app.rotate_screen, width=app.screen_width, height=app.screen_height) elif "ssd1362" in d: app.device = ssd1362(app.serial, rotate=app.rotate_screen, width=app.screen_width, height=app.screen_height) except OSError: pass
def main(argv): # Check and get arguments try: options, remainder = getopt.getopt(argv, '', [ 'help', 'interface=', 'i2c-port=', 'i2c-address=', 'display=', 'display-width=', 'display-height=' ]) except getopt.GetoptError: l.usage() sys.exit(2) for opt, arg in options: if opt == '--help': l.usage() sys.exit() elif opt in ('--interface'): if arg not in ['i2c', 'spi']: print 'Unknown interface type (choose between \'i2c\' and \'spi\')' sys.exit() s.interface = arg elif opt in ('--i2c-port'): s.i2c_port = int(arg) elif opt in ('--i2c-address'): s.i2c_address = int(arg, 16) elif opt in ('--display'): if arg not in [ 'sh1106', 'ssd1306', 'ssd1327', 'ssd1351', 'st7735' ]: print 'Unknown display type (choose between \'sh1106\', \'ssd1306\', \'ssd1327\', \'ssd1351\' and \'st7735\')' sys.exit() s.display = arg elif opt in ('--display-width'): s.display_width = int(arg) elif opt in ('--display-height'): s.display_height = int(arg) # Set serial if s.interface == 'i2c': serial = i2c(port=s.i2c_port, address=s.i2c_address) if s.display == 'sh1106': s.device = sh1106(serial, width=s.display_width, height=s.display_height, rotate=0) elif s.display == 'ssd1306': s.device = ssd1306(serial, width=s.display_width, height=s.display_height, rotate=0) elif s.display == 'ssd1327': s.device = ssd1327(serial, width=s.display_width, height=s.display_height, rotate=0, mode='RGB') else: serial = spi(device=0, port=0) if s.display == 'ssd1351': s.device = ssd1351(serial, width=s.display_width, height=s.display_height, rotate=1, mode='RGB', bgr=True) elif s.display == 'st7735': s.device = st7735(serial, width=s.display_width, height=s.display_height, rotate=3, mode='RGB') while True: print 'Start' l.scroll_message("Il etait une fois tout petit chaton.") print 'Stop'
def init(self, args): global disp, image, image_hor, image_ver, draw driver = args['driver'] interface = args['interface'] self.mode = '1' self.bits = 1 if interface == 'I2C': if driver == 'SSD1306_128_64': disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST, i2c_bus=args['i2c_bus'], i2c_address=args['i2c_address']) elif driver == 'SSD1306_128_32': disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, i2c_bus=args['i2c_bus'], i2c_address=args['i2c_address']) elif driver == 'ssd1309_128_64': self.backend = 'luma.oled' disp = ssd1309(i2c(port=args['i2c_bus'], address=args['i2c_address'])) #elif driver == 'ssd1322_256_64': # self.backend = 'luma.oled' # disp = ssd1322(i2c(port=args['i2c_bus'], address=args['i2c_address'])) #elif driver == 'ssd1325_128_64': # self.backend = 'luma.oled' # disp = ssd1325(i2c(port=args['i2c_bus'], address=args['i2c_address'])) elif driver == 'SSD1327_128_128': self.backend = 'luma.oled' self.mode = 'RGB' self.bits = 8 disp = ssd1327(i2c(port=args['i2c_bus'], address=args['i2c_address'])) #elif driver == 'SSD1331_96_64': # # self.backend = 'luma.oled' # self.bits = 16 # disp = ssd1331(i2c(port=args['i2c_bus'], address=args['i2c_address'])) #elif driver == 'SSD1351_128_96': # # self.backend = 'luma.oled' # self.bits = 16 # disp = ssd1351(i2c(port=args['i2c_bus'], address=args['i2c_address'])) elif driver == 'SSH1106_128_64': self.backend = 'luma.oled' disp = ssh1106(i2c(port=args['i2c_bus'], address=args['i2c_address'])) elif interface == 'SPI': if driver == 'SSD1306_128_64': disp = Adafruit_SSD1306.SSD1306_128_64(rst=args['spi_rst_pin'], dc=args['spi_dc_pin'], spi=SPI.SpiDev(args['spi_port'], args['spi_device']), max_speed_hz=args['spi_hz']) elif driver == 'SSD1306_128_32': disp = Adafruit_SSD1306.SSD1306_128_32(rst=args['spi_rst_pin'], dc=args['spi_dc_pin'], spi=SPI.SpiDev(args['spi_port'], args['spi_device']), max_speed_hz=args['spi_hz']) elif driver == 'ssd1309_128_64': self.backend = 'luma.oled' disp = ssd1309(spi(device=args['spi_device'], port=args['spi_port'])) elif driver == 'ssd1322_256_64': self.backend = 'luma.oled' disp = ssd1322(spi(device=args['spi_device'], port=args['spi_port'])) elif driver == 'ssd1325_128_64': self.backend = 'luma.oled' disp = ssd1325(spi(device=args['spi_device'], port=args['spi_port'])) elif driver == 'SSD1327_128_128': self.backend = 'luma.oled' self.mode = 'RGB' self.bits = 8 disp = ssd1327(spi(device=args['spi_device'], port=args['spi_port'])) elif driver == 'SSD1331_96_64': # self.backend = 'luma.oled' self.bits = 16 disp = ssd1331(spi(device=args['spi_device'], port=args['spi_port'])) elif driver == 'SSD1351_128_96': # self.backend = 'luma.oled' self.bits = 16 disp = ssd1351(spi(device=args['spi_device'], port=args['spi_port'])) elif driver == 'SSH1106_128_64': self.backend = 'luma.oled' disp = ssh1106(spi(device=args['spi_device'], port=args['spi_port'])) self.maxValue = 1 if 'RGB' == self.mode: self.maxValue = 255 if 'adafruit' == self.backend: disp.begin() self.rotate({}) width = self.getOutputWidth() height = self.getOutputHeight() #image_hor = Image.new('1', (width, height)) #image_ver = Image.new('1', (width, width)) image = Image.new(self.mode, (width, width)) images['default'] = image; # Get drawing object to draw on image. draw = ImageDraw.Draw(image) #start() """f = open("out.txt", "a")