Advertisement
mrjonny2

Ports.h

Mar 26th, 2012
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.74 KB | None | 0 0
  1. // Ports library definitions
  2. // 2009-02-13 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
  3. // $Id: Ports.h 5993 2010-09-03 22:18:09Z jcw $
  4.  
  5. #ifndef Ports_h
  6. #define Ports_h
  7.  
  8. #include <Arduino.h>
  9. #include <stdint.h>
  10.  
  11. class Port {
  12. protected:
  13.     uint8_t portNum;
  14.  
  15.     inline uint8_t digiPin() const
  16.         { return portNum ? portNum + 3 : 18; }
  17.     inline uint8_t digiPin2() const
  18.         { return portNum ? portNum + 13 : 19; }
  19.     inline uint8_t anaPin() const
  20.         { return portNum - 1; }
  21. public:
  22.     inline Port (uint8_t num) : portNum (num) {}
  23.  
  24.     // DIO pin
  25.     inline void mode(uint8_t value) const
  26.         { pinMode(digiPin(), value); }
  27.     inline uint8_t digiRead() const
  28.         { return digitalRead(digiPin()); }
  29.     inline void digiWrite(uint8_t value) const
  30.         { return digitalWrite(digiPin(), value); }
  31.     inline void anaWrite(uint8_t val) const
  32.         { analogWrite(digiPin(), val); }
  33.     inline uint32_t pulse(uint8_t state, uint32_t timeout =1000000L) const
  34.         { return pulseIn(digiPin(), state, timeout); }
  35.    
  36.     // AIO pin
  37.     inline void mode2(uint8_t value) const
  38.         { pinMode(digiPin2(), value); }
  39.     inline uint16_t anaRead() const
  40.         { return analogRead(anaPin()); }        
  41.     inline uint8_t digiRead2() const
  42.         { return digitalRead(digiPin2()); }
  43.     inline void digiWrite2(uint8_t value) const
  44.         { return digitalWrite(digiPin2(), value); }
  45.     inline uint32_t pulse2(uint8_t state, uint32_t timeout =1000000L) const
  46.         { return pulseIn(digiPin2(), state, timeout); }
  47.        
  48.     // IRQ pin (INT1, shared across all ports)
  49.     static void mode3(uint8_t value)
  50.         { pinMode(3, value); }
  51.     static uint8_t digiRead3()
  52.         { return digitalRead(3); }
  53.     static void digiWrite3(uint8_t value)
  54.         { return digitalWrite(3, value); }
  55.        
  56.     // both pins: data on DIO, clock on AIO
  57.     inline void shift(uint8_t bitOrder, uint8_t value) const
  58.         { shiftOut(digiPin(), digiPin2(), bitOrder, value); }
  59.     uint16_t shiftRead(uint8_t bitOrder, uint8_t count =8) const;
  60.     void shiftWrite(uint8_t bitOrder, uint16_t value, uint8_t count =8) const;
  61. };
  62.  
  63. class RemoteNode {
  64. public:
  65.     typedef struct {
  66.         uint8_t flags, modes, digiIO, anaOut[2];
  67.         uint16_t anaIn[4]; // only bits 0..11 used
  68.     } Data;
  69.  
  70.     RemoteNode (char id, uint8_t band, uint8_t group =0);
  71.    
  72.     void poll(uint16_t msecs);
  73.  
  74.     friend class RemoteHandler;
  75.     friend class RemotePort;
  76. private:
  77.     uint8_t nid;
  78.     uint32_t lastPoll;
  79.     Data data;
  80. };
  81.  
  82. class RemoteHandler {
  83. public:
  84.     static void setup(uint8_t id, uint8_t band, uint8_t group =0);
  85.     static uint8_t poll(RemoteNode& node, uint8_t send);
  86. };
  87.  
  88. class RemotePort : protected Port {
  89.     RemoteNode& node;
  90.  
  91.     inline uint8_t pinBit() const
  92.         { return portNum - 1; }
  93.     inline uint8_t pinBit2() const
  94.         { return portNum + 3; }
  95. public:
  96.     RemotePort (RemoteNode& remote, uint8_t num) : Port (num), node (remote) {}
  97.  
  98.     void mode(uint8_t value) const;
  99.     uint8_t digiRead() const;
  100.     void digiWrite(uint8_t value) const;
  101.     void anaWrite(uint8_t val) const;
  102.    
  103.     void mode2(uint8_t value) const;    
  104.     uint16_t anaRead() const;
  105.     uint8_t digiRead2() const;
  106.     void digiWrite2(uint8_t value) const;    
  107. };
  108.  
  109. class PortI2C : public Port {
  110.     uint8_t uswait;
  111.    
  112.     inline void hold() const
  113.         { delayMicroseconds(uswait); }
  114.     inline void sdaOut(uint8_t value) const
  115.         { mode(!value); digiWrite(value); }
  116.     inline uint8_t sdaIn() const
  117.         { return digiRead(); }
  118.     inline void sclHi() const
  119.         { hold(); digiWrite2(1); }
  120.     inline void sclLo() const
  121.         { hold(); digiWrite2(0); }
  122. public:
  123.     enum { KHZMAX = 1, KHZ400 = 2, KHZ100 = 9 };
  124.    
  125.     PortI2C (uint8_t num, uint8_t rate =KHZMAX);
  126.    
  127.     uint8_t start(uint8_t addr) const;
  128.     void stop() const;
  129.     uint8_t write(uint8_t data) const;
  130.     uint8_t read(uint8_t last) const;
  131. };
  132.  
  133. class DeviceI2C {
  134.     const PortI2C& port;
  135.     uint8_t addr;
  136.    
  137. public:
  138.     DeviceI2C(const PortI2C& p, uint8_t me) : port (p), addr (me << 1) {}
  139.    
  140.     bool isPresent() const;
  141.    
  142.     uint8_t send() const
  143.         { return port.start(addr); }
  144.     uint8_t receive() const
  145.         { return port.start(addr | 1); }
  146.     void stop() const
  147.         { port.stop(); }
  148.     uint8_t write(uint8_t data) const
  149.         { return port.write(data); }
  150.     uint8_t read(uint8_t last) const
  151.         { return port.read(last); }
  152.        
  153.     uint8_t setAddress(uint8_t me)
  154.         { addr = me << 1; }
  155. };
  156.  
  157. // The millisecond timer can be used for timeouts up to 60000 milliseconds.
  158. // Setting the timeout to zero disables the timer.
  159. //
  160. // for periodic timeouts, poll the timer object with "if (timer.poll(123)) ..."
  161. // for one-shot timeouts, call "timer.set(123)" and poll as "if (timer.poll())"
  162.  
  163. class MilliTimer {
  164.     word next;
  165.     byte armed;
  166. public:
  167.     MilliTimer () : armed (0) {}
  168.    
  169.     byte poll(word ms =0);
  170.     word remaining() const;
  171.     byte idle() const { return !armed; }
  172.     void set(word ms);
  173. };
  174.  
  175. // interface for the Blink Plug - see http://jeelabs.org/bp1
  176.  
  177. class BlinkPlug : public Port {
  178.     MilliTimer debounce;
  179.     byte leds, lastState, checkFlags;
  180. public:
  181.     enum { ALL_OFF, ON1, OFF1, ON2, OFF2, SOME_ON }; // returned by buttonCheck
  182.    
  183.     BlinkPlug (byte port)
  184.         : Port (port), leds (0), lastState (0), checkFlags (0) {}
  185.    
  186.     void ledOn(byte mask);
  187.     void ledOff(byte mask);
  188.     byte state();
  189.     byte pushed(); // deprecated, don't use in combination with buttonCheck
  190.     byte buttonCheck();
  191. };
  192.  
  193. // interface for the Memory Plug - see http://jeelabs.org/mp1
  194.  
  195. class MemoryPlug : public DeviceI2C {
  196.     uint32_t nextSave;
  197. public:
  198.     MemoryPlug (PortI2C& port)
  199.         : DeviceI2C (port, 0x50), nextSave (0) {}
  200.  
  201.     void load(word page, void* buf, byte offset =0, int count =256);
  202.     void save(word page, const void* buf, byte offset =0, int count =256);
  203. };
  204.  
  205. class MemoryStream {
  206.     MemoryPlug& dev;
  207.     word start, curr;
  208.     char step;
  209.     byte buffer[256], pos;
  210. public:
  211.     MemoryStream (MemoryPlug& plug, word page =0, char dir =1)
  212.             : dev (plug), start (page), curr (page), step (dir), pos (0) {}
  213.    
  214.     long position(byte writing) const;
  215.     byte get();
  216.     void put(byte data);
  217.     word flush();
  218.     void reset();
  219. };
  220.  
  221. // interface for the UART Plug - see http://jeelabs.org/up1
  222.  
  223. class UartPlug : public Print {
  224.     DeviceI2C dev;
  225.     // avoid per-byte access, fill entire buffer instead to reduce I2C overhead
  226.     byte rxbuf[20], in, out;
  227.  
  228.     void regSet (byte reg, byte value);
  229.     void regRead (byte reg);
  230.    
  231. public:
  232.     UartPlug (PortI2C& port, byte addr)
  233.         : dev (port, addr), in (0), out (0) {}
  234.        
  235.     void begin(long);
  236.     byte available();
  237.     int read();
  238.     void flush();
  239.     virtual size_t write(byte);
  240. };
  241.  
  242. // interface for the Dimmer Plug - see http://jeelabs.org/dp1
  243.  
  244. class DimmerPlug : public DeviceI2C {
  245. public:
  246.     enum {
  247.         MODE1, MODE2,
  248.         PWM0, PWM1, PWM2, PWM3, PWM4, PWM5, PWM6, PWM7,
  249.         PWM8, PWM9, PWM10, PWM11, PWM12, PWM13, PWM14, PWM15,
  250.         GRPPWM, GRPFREQ,
  251.         LEDOUT0, LEDOUT1, LEDOUT2, LEDOUT3,
  252.         SUBADR1, SUBADR2, SUBADR3, ALLCALLADR,
  253.     };
  254.  
  255.     DimmerPlug (PortI2C& port, byte addr)
  256.         : DeviceI2C (port, addr) {}
  257.        
  258.     void setReg(byte reg, byte value) const;
  259.     byte getReg(byte reg) const;
  260. };
  261.  
  262. // interface for the Lux Plug - see http://jeelabs.org/xp1
  263.  
  264. class LuxPlug : public DeviceI2C {
  265.     union { byte b[4]; word w[2]; } data;
  266. public:
  267.     enum {
  268.         CONTROL, TIMING,
  269.         THRESHLOWLOW, THRESHLOWHIGH, THRESHHIGHLOW, THRESHHIGHHIGH, INTERRUPT,
  270.         ID = 0xA,
  271.         DATA0LOW = 0xC, DATA0HIGH, DATA1LOW, DATA1HIGH,
  272.     };
  273.  
  274.     LuxPlug (PortI2C& port, byte addr) : DeviceI2C (port, addr) {}
  275.    
  276.     void begin() {
  277.         send();
  278.         write(0xC0 | CONTROL);
  279.         write(3); // power up
  280.         stop();
  281.     }
  282.    
  283.     const word* getData();
  284.  
  285.     word calcLux(byte iGain =0, byte tInt =2) const;
  286. };
  287.  
  288. // interface for the Gravity Plug - see http://jeelabs.org/gp1
  289.  
  290. class GravityPlug : public DeviceI2C {
  291.     union { byte b[6]; int w[3]; } data;
  292. public:
  293.     GravityPlug (PortI2C& port) : DeviceI2C (port, 0x38) {}
  294.    
  295.     void begin() {}
  296.    
  297.     const int* getAxes();
  298. };
  299.  
  300. // interface for the Heading Plug - see http://jeelabs.org/hp1
  301. // FIXME - this HDPM01 interface class isn't working properly yet!
  302.  
  303. class HeadingPlug : public PortI2C {
  304.     DeviceI2C eeprom, adc, compass;
  305.     Port aux;
  306.     // keep following fields in order:
  307.     word C1, C2, C3, C4, C5, C6, C7;
  308.     byte A, B, C, D;
  309.  
  310.     byte eepromByte(byte reg) const;
  311.     void getConstants();
  312.     word adcValue(byte press) const;
  313.     void setReset(byte reset) const;
  314.  
  315. public:
  316.     HeadingPlug (int num)
  317.         : PortI2C (num), eeprom (*this, 0x50), adc (*this, 0x77),
  318.           compass (*this, 0x30), aux (5-num) {}
  319.    
  320.     void begin();
  321.     void pressure(int& temp, int& pres) const;
  322.     void heading(int& xaxis, int& yaxis) const;
  323. };
  324.  
  325. // interface for the Input Plug - see http://jeelabs.org/ip1
  326.  
  327. class InputPlug : public Port {
  328.     uint8_t slow;
  329. public:
  330.     InputPlug (uint8_t num, uint8_t fix =0) : Port (num), slow (fix) {}
  331.    
  332.     void select(uint8_t channel);
  333. };
  334.  
  335. // low-power utilities
  336.  
  337. class Sleepy {
  338. public:
  339.     // start the watchdog timer (or disable it if mode < 0)
  340.     static void watchdogInterrupts (char mode);
  341.    
  342.     // enter low-power mode, wake up with watchdog, INT0/1, or pin-change
  343.     static void powerDown ();
  344.    
  345.     // spend some time in low-power mode, the timing is only approximate
  346.     static void loseSomeTime (word msecs);
  347. };
  348.  
  349. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement