Permalink
Browse files

documentation

  • Loading branch information...
1 parent 4a11466 commit d4379e9e61a66c8e8041d095d7b616d618a098f7 ponty committed Dec 18, 2013
Showing with 254 additions and 26 deletions.
  1. +47 −0 codegen/README.md
  2. +3 −0 examples/dump.py
  3. +3 −0 examples/highfreqpwm.py
  4. +10 −0 examples/reset.py
  5. +40 −10 nanpy/arduinocore.py
  6. +49 −9 nanpy/arduinopin.py
  7. +10 −0 nanpy/define.py
  8. +22 −2 nanpy/pwm.py
  9. +39 −0 nanpy/resgister.py
  10. +23 −5 nanpy/watchdog.py
  11. +8 −0 tests/README.md
View
@@ -0,0 +1,47 @@
+Code generation
+================
+
+C code is generated for AVR type and registers.
+All information is parsed from AVR Libc header files.
+
+dependencies:
+
+ - path.py
+ - AVR Libc (http://www.nongnu.org/avr-libc/)
+
+ sudo apt-get install python-pip avr-libc
+ sudo pip install path.py
+
+running from project directory:
+
+ python codegen/__init__.py
+
+generated files:
+
+ - firmware/Nanpy/generated_mcu.h
+ - firmware/Nanpy/generated_avr_registers.h
+
+
+generated_mcu.h
+-----------------
+
+This is a list of all known AVR types, "MCU" is defined to the type name.
+Example:
+
+ #ifdef __AVR_ATmega328P__
+ # ifdef MCU
+ # error "MCU is already defined"
+ # endif
+ # define MCU "__AVR_ATmega328P__"
+ #endif
+
+generated_avr_registers.h
+---------------------------
+This is a list of all known register names.
+Example:
+
+ #ifdef DDRB
+ DEFINE(DDRB)
+ #else
+ MISSING(DDRB)
+ #endif
View
@@ -1,3 +1,6 @@
+'''Dump all possible values from the AVR
+'''
+
from dicts.sorteddict import SortedDict
from entrypoint2 import entrypoint
from nanpy.arduino import Arduino
@@ -1,3 +1,6 @@
+'''High frequency PWM mode demo
+'''
+
from nanpy.arduinopin import ArduinoPin
FREQ = 10007
View
@@ -1,3 +1,13 @@
+"""soft reset demo."""
+
from nanpy.watchdog import Watchdog
+from nanpy.arduino import Arduino
+
+
+def print_millis():
+ print ('uptime: %s sec' % (Arduino.millis() / 1000.0))
+print_millis()
+print ('soft reset')
Watchdog.soft_reset()
+print_millis()
View
@@ -5,70 +5,100 @@
class ArduinoCore(object):
+ """Access to Arduino functions which are not part of the public API.
+
+ There are some additional funtions implemented in C
+
+ """
+
@classmethod
@memoized
@returns(int)
@arduinoclassmethod
def digitalPinToBitMask(cls, pin):
- pass
+ """nonpublic Arduino function."""
@classmethod
@memoized
@returns(int)
@arduinoclassmethod
def digitalPinToPort(cls, pin):
- pass
+ """nonpublic Arduino function."""
@classmethod
@memoized
@returns(int)
@arduinoclassmethod
def portModeRegister(cls, pin):
- pass
+ """nonpublic Arduino function."""
@classmethod
@memoized
@returns(int)
@arduinoclassmethod
def digitalPinToTimer(cls, pin):
- pass
+ """nonpublic Arduino function."""
@classmethod
@memoized
@returns(int)
@arduinoclassmethod
def analogInPinToBit(cls, pin):
- pass
+ """nonpublic Arduino function."""
@classmethod
@memoized
@returns(int)
@arduinoclassmethod
def portOutputRegister(cls, pin):
- pass
+ """nonpublic Arduino function."""
@classmethod
@memoized
@returns(int)
@arduinoclassmethod
def portInputRegister(cls, pin):
- pass
+ """nonpublic Arduino function."""
@classmethod
@memoized
@returns(int)
@arduinoclassmethod
def totalPinCount(cls):
- pass
+ """total pin count.
+
+ The C implementation is a hack.
+
+ """
@classmethod
@returns(lambda x: int(x) / 1000.0)
@arduinoclassmethod
def readVcc(cls):
- """VCC in Volt."""
+ """VCC in Volt. This works on Arduinos with a 328 or 168 only.
+
+ Source: http://code.google.com/p/tinkerit/wiki/SecretVoltmeter
+
+ How it works
+
+ The Arduino 328 and 168 have a built in precision voltage reference of 1.1V.
+ This is used sometimes for precision measurement,
+ although for Arduino it usually makes more sense to measure against Vcc,
+ the positive power rail.
+ The chip has an internal switch that selects which pin the analogue to digital converter reads.
+ That switch has a few leftover connections, so the chip designers wired them up to useful signals.
+ One of those signals is that 1.1V reference.
+ So if you measure how large the known 1.1V reference is in comparison to Vcc,
+ you can back-calculate what Vcc is with a little algebra. That is how this works.
+
+ """
@classmethod
def model(cls):
- """"""
+ """The model of the AVR.
+
+ Example: ATmega328P
+
+ """
s = Define.asDict().get('MCU')
return s.strip('_').split('_')[-1]
View
@@ -1,6 +1,4 @@
from nanpy.arduino import Arduino
-from nanpy.arduinoboard import arduinoclassmethod
-from nanpy.arduinoboard import returns
from nanpy.arduinocore import ArduinoCore
from nanpy.define import Define
from nanpy.memo import memoized
@@ -40,36 +38,51 @@ def name2int(pin_name, A0):
class ArduinoPin(object):
+ '''Object-oriented representation of an Arduino pin
+
+ Examples:
+
+ # they do the same
+ ArduinoPin(0).value
+ ArduinoPin('D0').value
+ ArduinoPin('D0').read_value()
+ '''
@classmethod
@memoized
def count_analog(cls):
+ """Count of analog pins."""
return len(cls.analog_names())
@classmethod
@memoized
def count_digital(cls):
+ """Count of digital pins."""
return len(cls.digital_names())
@classmethod
@memoized
def count(cls):
+ """Count of all pins."""
return cls.count_analog() + cls.count_digital()
@classmethod
@memoized
def digital_names(cls):
+ """List of digital pin names."""
A0 = Define.asDict()['A0']
return ['D%s' % x for x in range(0, A0)]
@classmethod
@memoized
def analog_names(cls):
+ """List of analog pin names."""
A0 = Define.asDict()['A0']
return (
['A%s' % (x - A0) for x in range(A0, ArduinoCore.totalPinCount())]
)
def __init__(self, name):
+ """name can be int or string."""
self.A0 = Define.asDict()['A0']
self.nr = name2int(name, self.A0)
if self.nr >= ArduinoCore.totalPinCount():
@@ -79,6 +92,8 @@ def __init__(self, name):
@property
@memoized
def pwm(self):
+ '''Object-oriented representation of the pin PWM functionality
+ '''
return ArduinoPwmPin(self.nr)
@property
@@ -91,11 +106,15 @@ def is_analog(self):
@property
def avr_port(self):
+ '''AVR port name (example: "B")
+ '''
x = ArduinoCore.digitalPinToPort(self.nr)
return chr(ord('A') + x - 1)
@property
def avr_bit(self):
+ '''AVR bit name (example: "2")
+ '''
bitmask = ArduinoCore.digitalPinToBitMask(self.nr)
i = 0
while bitmask != 1:
@@ -105,10 +124,19 @@ def avr_bit(self):
@property
def avr_pin(self):
+ '''AVR pin name (example: "PB2")
+ '''
return 'P%s%s' % (self.avr_port, self.avr_bit)
@property
def name(self):
+ """Arduino pin name.
+
+ D -> digital
+ A -> analog
+ (examples: "D2", "A0")
+
+ """
if self.is_digital:
return 'D%s' % self.nr
else:
@@ -122,6 +150,7 @@ def nr_analog(self):
@property
def programming_function(self):
+ """programming function (MISO, MOSI, SCK or SS)"""
d = Define.asDict()
if self.nr == d['MISO']:
return 'MISO'
@@ -133,21 +162,35 @@ def programming_function(self):
return 'SS'
def reset(self):
+ '''reset to the pin default state: INPUT, no pullup
+ '''
self.write_mode(INPUT)
self.write_pullup(LOW)
if self.pwm.available:
self.pwm.reset()
def write_pullup(self, value):
+ """set pullup (0/1)"""
self.write_mode(INPUT)
return self.write_digital_value(value)
def read_digital_value(self, direction=None):
+ """read digital value (0/1)
+
+ direction can be set, if 'direction' parameter exists
+
+ """
if direction is not None:
self.write_mode(direction)
return Arduino.digitalRead(self.nr)
def write_digital_value(self, value, direction=None):
+ """write digital value (0/1)
+
+ direction can be set, if 'direction' parameter exists, and it is
+ not INPUT
+
+ """
if direction == INPUT:
raise ValueError('write_digital_value() to INPUT??')
if direction is not None:
@@ -158,21 +201,18 @@ def write_digital_value(self, value, direction=None):
digital_value = property(read_digital_value, write_digital_value)
def read_analog_value(self):
+ '''read analog value (0-1023)
+ '''
if not self.is_analog:
return None
return Arduino.analogRead(self.nr)
analog_value = property(read_analog_value)
def read_mode(self):
+ """read mode (0/1)"""
return Arduino.pinModeRead(self.nr)
def write_mode(self, value):
+ """write mode (0/1)"""
return Arduino.pinMode(self.nr, value)
mode = property(read_mode, write_mode)
-
-# def read_pwm_value(self):
-# return
-#
-# def write_pwm_value(self, value):
-# return Arduino.analogWrite(self.nr, value)
-# pwm_value = property(read_pwm_value, write_pwm_value)
View
@@ -11,6 +11,15 @@ def auto_convert(x):
class Define(object):
+ """Access to firmware constants.
+
+ Examples:
+
+ Define.asDict().get('F_CPU') # AVR frequency
+ Define.asDict().get('__DATE__') # firmware upload date
+
+ """
+
@classmethod
@memoized
@returns(int)
@@ -34,6 +43,7 @@ def value(cls, index):
@classmethod
@memoized
def asDict(cls):
+ """return all constants and their values in a dictionary."""
d = dict()
for i in range(cls.count()):
d[cls.name(i)] = cls.value(i)
Oops, something went wrong.

0 comments on commit d4379e9

Please sign in to comment.