4tronix / PiconZero

Initial commit
13 stars 11 forks source link

logging, init & i2c mock fallback #7

Open WayneKeenan opened 7 years ago

WayneKeenan commented 7 years ago

Hi, this is more of a query and perhaps start of a discussion.

I've embedded a modified version of the piconzero library in PiCraftZero.

I've included a diff below to highlight the details but it's not intended as a PR or patch, it's just for illustration purposes. Also, my version has fallen behind the GitHub one.

In summary the tweaks are:

It would be nice to get these into the piconzero library in some way instead of users having to import from the namespace my modified version exists in.

The diff:

shinytoo:local.projects wayne$ diff -r PiconZero/Python/piconzero.py   ~/local.projects/PiCraft/python/src/picraftzero/thirdparty/piconzero.py 
4c4,13
< import smbus, time
---
> import time
> 
> import logging
> logger = logging.getLogger(__name__)
> init_done =False
> try:
>     import smbus
> except ImportError:
>     logger.warning("Falling back to mock SMBus. This library requires python smbus")
>     from picraftzero.thirdparty.mocks.raspiberrypi.rpidevmocks import Mock_smbusModule as smbus
19d27
< INPERIOD0 = 21
24a33
> SHOW_ERRORS = False
36,37c45,46
<             if (DEBUG):
<                 print("Error in getRevision(), retrying")
---
>             if (SHOW_ERRORS):
>                 logger.excpetion("Error in getRevision(), retrying")
52,53c61,62
<                 if (DEBUG):
<                     print("Error in setMotor(), retrying")
---
>                 if (SHOW_ERRORS):
>                     logger.exception("Error in setMotor(), retrying")
86,88c95,97
<                 if (DEBUG):
<                     print("Error in readChannel(), retrying")
< 
---
>                 if (SHOW_ERRORS):
>                     logger.excpetion("Error in readChannel(), retrying")
>                 
90c99
< 
---
>     
101,102c110,111
<                 if (DEBUG):
<                     print("Error in setOutputConfig(), retrying")
---
>                 if (SHOW_ERRORS):
>                     logger.exception("Error in setOutputConfig(), retrying")
107,110c116,119
< # 0: Digital, 1: Analog, 2: DS18B20, 4: DutyCycle 5: Pulse Width
< def setInputConfig (channel, value, pullup = False, period = 2000):
<     if (channel >= 0 and channel <= 3 and value >= 0 and value <= 5):
<         if (value == 0 and pullup == True):
---
> # 0: Digital, 1: Analog
> def setInputConfig (channel, value, pullup = False):
>     if (channel>=0 and channel <=3 and value>=0 and value<=3):
>         if (value==0 and pullup==True):
115,116d123
<                 if (value == 4 or value == 5):
<                     bus.write_word_data (pzaddr, INPERIOD0 + channel, period)
119,120c126,127
<                 if (DEBUG):
<                     print("Error in setInputConfig(), retrying")
---
>                 if (SHOW_ERRORS):
>                     logger.exception("Error in setInputConfig(), retrying")
137,138c144,145
<                 if (DEBUG):
<                     print("Error in setOutput(), retrying")
---
>                 if (SHOW_ERRORS):
>                     logger.exception("Error in setOutput(), retrying")
150,151c157,158
<             if (DEBUG):
<                 print("Error in setPixel(), retrying")
---
>             if (SHOW_ERRORS):
>                 logger.exception("Error in setPixel(), retrying")
160,161c167,168
<             if (DEBUG):
<                 print("Error in setAllPixels(), retrying")
---
>             if (SHOW_ERRORS):
>                 logger.exception("Error in setAllPixels(), retrying")
169,171c176,178
<             if (DEBUG):
<                 print("Error in updatePixels(), retrying")
< 
---
>             if (SHOW_ERRORS):
>                 logger.exception("Error in updatePixels(), retrying")
>                         
182,183c189,190
<             if (DEBUG):
<                 print("Error in setBrightness(), retrying")
---
>             if (SHOW_ERRORS):
>                 logger.exception("Error in setBrightness(), retrying")
188c195
< def init (debug=False):
---
> def init (debug=False, show_errors=False):
189a197,200
>     SHOW_ERRORS = show_errors
>     global init_done
>     if init_done:
>         return
192a204
>             init_done = True
195,196c207
<             if (DEBUG):
<                 print("Error in init(), retrying")
---
>             logger.exception("Error in init(), retrying")
197a209
> 
199c211
<         print("Debug is", DEBUG)
---
>         print ("Debug is", DEBUG)
204c216
< def cleanup ():
---
> def cleanup():
210,211c222
<             if (DEBUG):
<                 print("Error in cleanup(), retrying")
---
>             logger.exception("Error in cleanup(), retrying")
213a225
> 
WayneKeenan commented 7 years ago

For the i2c fallback nobody would want piconzero to depend on picraftzero.thirdparty.mocks.raspiberrypi.rpidevmocks or the rpidevmocks PiCraftZero uses.

Perhaps piconzero could do it's default behaviour unless an optional i2c_provider option was passed to the constructors/init or a module global?