1 /*
2  * Author: Stan Gifford <stan@gifford.id.au>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #pragma once
26 
27 #include <mraa/i2c.h>
28 
29 #define MAX_BUFFER_LENGTH 6
30 
31 #define SERVO_MIN       0
32 #define SERVO_MAX       180
33 #define SERVO_FREQ      60
34 #define PCA9685_SUBADR1 0x2
35 #define PCA9685_SUBADR2 0x3
36 #define PCA9685_SUBADR3 0x4
37 
38 #define PCA9685_MODE1    0x0
39 #define PCA9685_PRESCALE 0xFE
40 
41 #define LED0_ON_L  0x6
42 #define LED0_ON_H  0x7
43 #define LED0_OFF_L 0x8
44 #define LED0_OFF_H 0x9
45 
46 #define ALLLED_ON_L  0xFA
47 #define ALLLED_ON_H  0xFB
48 #define ALLLED_OFF_L 0xFC
49 #define ALLLED_OFF_H 0xFD
50 
51 #define PCA9685_MODE1_REG       0x00
52 #define PCA9685_PRESCALE_REG    0xFE
53 #define LED0_REG                0x06
54 
55 namespace upm {
56 
57  /**
58   * @brief Adafruit PCA9685-based Servo Shield library
59   * @defgroup adafruitss libupm-adafruitss
60   * @ingroup adafruit i2c servos
61   */
62 
63  /**
64   * @library adafruitss
65   * @sensor adafruitss
66   * @comname Adafruit Servo Shield
67   * @type servos
68   * @man adafruit
69   * @web http://www.adafruit.com/product/1411
70   * @con i2c
71   *
72   * @brief API for the Adafruit Servo Shield
73   *
74   * UPM library for the PCA9685-based Adafruit 16-channel servo shield. If 3
75   * or more GWS servos are attached, results could be unpredictable. Adafruit
76   * Industries recommend installing a capacitor on the board, which should
77   * alleviate the issue. Sizing depends on servos and their number.
78   *
79   * @image html adafruitss.jpg
80   * @snippet adafruitss.cxx Interesting
81   */
82 
83   class adafruitss {
84   public:
85     /**
86      * Creates an adafruitss object
87      *
88      * @param bus Number of the used I2C bus
89      * @param i2c_address Address of the servo shield on the I2C bus
90      */
91     adafruitss(int bus, int i2c_address);
92     int update(void);
93     /**
94      * Sets the frequency of the servos
95      *
96      * @param freq Frequency at which the servos operate
97      */
98     void setPWMFreq(float freq);
99     /**
100      * Moves one of the servos to a specified angle
101      *
102      * @param port Port of the servo on the shield (servo number)
103      * @param servo_type Can be 0 = standard (1ms to 2ms), 1 = extended
104      * (0.6ms to 2.4ms), or 2 = extended (0.8ms to 2.2ms)
105      * @param degrees Angle to set the servo to
106      */
107     void servo(uint8_t port, uint8_t servo_type, float degrees);
108 
109   private:
110 
111     int pca9685_addr;
112     mraa_i2c_context m_i2c;
113     uint8_t m_rx_tx_buf[MAX_BUFFER_LENGTH];
114     float _duration_1ms;
115 };
116 
117 }
118