Skip to content

Commit

Permalink
Merge pull request #20 from pimoroni/sphinx-docs
Browse files Browse the repository at this point in the history
Code QA & Test Fixes
  • Loading branch information
Gadgetoid committed Jan 30, 2019
2 parents cddf989 + f1d9e3f commit eacf3e2
Show file tree
Hide file tree
Showing 13 changed files with 589 additions and 31 deletions.
1 change: 0 additions & 1 deletion library/.coverage

This file was deleted.

69 changes: 42 additions & 27 deletions library/inky/eeprom.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Inky display-type EEPROM tools."""

import datetime
import struct
Expand All @@ -8,65 +9,76 @@
EEP_ADRESS = 0x50
EEP_WP = 12


class EPDType:
valid_colors = [None, "black", "red", "yellow"]
"""Class to represent EPD EEPROM structure."""

valid_colors = [None, 'black', 'red', 'yellow']

def __init__(self, width, height, color, pcb_variant, display_variant, write_time=None):
"""Initialise new EEPROM data structure."""
self.width = width
self.height = height
self.height = height
self.color = color
if type(color) == str:
self.set_color(color)
self.pcb_variant = pcb_variant
self.pcb_variant = pcb_variant
self.display_variant = display_variant
self.eeprom_write_time = str(datetime.datetime.now()) if write_time is None else write_time

def __repr__(self):
"""Return string representation of EEPROM data structure."""
return """Display: {}x{}
Color: {}
PCB Variant: {}
Display Variant: {}
Time: {}""".format(self.width,
self.height,
self.get_color(),
self.pcb_variant / 10.0,
self.display_variant,
self.eeprom_write_time)
self.height,
self.get_color(),
self.pcb_variant / 10.0,
self.display_variant,
self.eeprom_write_time)

@classmethod
def from_bytes(class_object, data):
"""Initialise new EEPROM data structure from a bytes-like object or list."""
data = bytearray(data)
data = struct.unpack("<HHBBB22p", data)
data = struct.unpack('<HHBBB22p', data)
return class_object(*data)

def update_eeprom_write_time(self):
"""Update the stored write time."""
self.eeprom_write_time = str(datetime.datetime.now())

def encode(self):
return struct.pack("<HHBBB22p",
self.width,
self.height,
self.color,
self.pcb_variant,
self.display_variant,
str(datetime.datetime.now()))
"""Return a bytearray representing the EEPROM data structure."""
return struct.pack('<HHBBB22p',
self.width,
self.height,
self.color,
self.pcb_variant,
self.display_variant,
str(datetime.datetime.now()))

def to_list(self):
return [ord(c) for x in self.encode()]

"""Return a list of bytes representing the EEPROM data structure."""
return [ord(c) for c in self.encode()]

def set_color(self, color):
"""Set the stored colour value."""
try:
self.color = self.valid_colors.index(color)
except KeyError:
raise ValueError("Invalid colour: {}".format(color))
raise ValueError('Invalid colour: {}'.format(color))

def get_color(self):
"""Get the stored colour value."""
try:
return self.valid_colors[self.color]
except KeyError:
return None


# Normal Yellow wHAT
yellow_what_1_E = EPDType(400, 300, color='yellow', pcb_variant=12, display_variant=2)

Expand All @@ -81,18 +93,21 @@ def get_color(self):


def read_eeprom():
"""Return a class representing EEPROM contents, or none."""
try:
i2c = smbus.SMBus(1)
i2c.write_i2c_block_data(EEP_ADRESS, 0x00, [0x00])
return EPDType.from_bytes(i2c.read_i2c_block_data(0x50,0,29))
i2c.write_i2c_block_data(EEP_ADRESS, 0x00, [0x00])
return EPDType.from_bytes(i2c.read_i2c_block_data(0x50, 0, 29))
except IOError:
return None


def main(args):
print(read_eeprom())
"""EEPROM Test Function."""
print(read_eeprom())
return 0


if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))

2 changes: 1 addition & 1 deletion library/inky/inky.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(self, resolution=(400, 300), colour='black', cs_pin=CS0_PIN, dc_pin

if self.eeprom is not None:
if self.eeprom.width != self.width or self.eeprom.height != self.height:
raise ValueError("Supplied width/height do not match Inky: {}x{}".format(self.eeprom.width, self.eeprom.height))
raise ValueError('Supplied width/height do not match Inky: {}x{}'.format(self.eeprom.width, self.eeprom.height))
if self.eeprom.display_variant == 1 and self.eeprom.get_color() == 'red':
self.lut = 'red_ht'

Expand Down
3 changes: 2 additions & 1 deletion library/tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def mockery():
sys.modules['RPi'] = mock.Mock()
sys.modules['RPi.GPIO'] = mock.Mock()
sys.modules['spidev'] = mock.Mock()
sys.modules['smbus'] = MockSMBus(1)
sys.modules['smbus'] = mock.Mock()
sys.modules['smbus'].SMBus = MockSMBus


def test_init_phat_black():
Expand Down
2 changes: 1 addition & 1 deletion library/tests/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def __init__(self, i2c_bus):

def write_i2c_block_data(self, i2c_address, register, values):
"""Write a block of i2c data bytes."""
raise IOError("Pretending there's no EEPROM to talk to.")
# self.regs[register:register + len(values)] = values
pass

def read_i2c_block_data(self, i2c_address, register, length):
"""Read a block of i2c data bytes."""
Expand Down
53 changes: 53 additions & 0 deletions sphinx/_static/custom.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.rst-content a, .rst-content a:focus {
color:#13c0d7;
}
.rst-content a:visited, .rst-content a:active {
color:#87319a;
}
.rst-content .highlighted {
background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAJElEQVQIW2P8//9/PSMjYyMDEmAEsdElwILoEnBBZAkUQZgEABMWE4Kzp1KUAAAAAElFTkSuQmCC),rgba(246,167,4,0.2);
margin:0 -6px;
}
.wy-side-nav-search {
background:#333333;
}
.wy-nav-side {
background:#444444;
}
.wy-menu-vertical a {
color:#cccccc
}
.wy-menu-vertical p.caption {
background: #333333;
color: #6d6d6d;
}
.rst-content dl:not(.docutils) dt {
background:#e7fafd;
border-top:solid 3px #13c0d7;
color:rgba(0,0,0,0.5);
}
.rst-content .viewcode-link, .rst-content .viewcode-back {
color:#00b09b;
}
code.literal {
color:#e63c2e;
}


.rst-content #at-a-glance {
margin-bottom:24px;
}
.rst-content #at-a-glance blockquote {
margin-left:0;
}
.rst-content #at-a-glance dl:not(.docutils) dt {
border:none;
background:#f0f0f0;
}
.rst-content #at-a-glance dl:not(.docutils) dd,
.rst-content #at-a-glance dl:not(.docutils) dd dl:not(.docutils) dd {
display:none;
}
.rst-content #at-a-glance dl:not(.docutils) {
margin-bottom:0;
}
Empty file.
43 changes: 43 additions & 0 deletions sphinx/_templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{% extends "!layout.html" %}
{% block extrahead %}
<link rel="stylesheet" href="{{ pathto("_static/custom.css", True) }}" type="text/css">
{% endblock %}
{% block footer %}
<script type="text/javascript">
var menuItems = $('.wy-menu-vertical').find('a');

var scrollItems = menuItems.map(function(){
var item = $($(this).attr('href'));
if (item.length) { return item; }
});

$(window).on('scroll',function(){
var fromTop = $(this).scrollTop()

var cur = scrollItems.map(function(){
if ($(this).offset().top < fromTop+30) return this;
});

cur = cur[cur.length-1];
var id = cur && cur.length ? cur[0].id : "";

var target = $('#'+id);
target.attr('id','');
window.location.hash = '#'+id;
target.attr('id',id);

menuItems
.parent().removeClass("current")
.end().filter("[href='#"+id+"']").parent().addClass("current");
});

$(window).on('hashchange',function(){

var hash = window.location.hash.replace('#','');

menuItems
.parent().removeClass("current")
.end().filter("[href='#"+hash+"']").parent().addClass("current");
})
</script>
{% endblock %}

0 comments on commit eacf3e2

Please sign in to comment.