Skip to content

Commit

Permalink
Add support for PWM
Browse files Browse the repository at this point in the history
  • Loading branch information
fenichelar committed Mar 9, 2016
1 parent 52063f9 commit 62969e5
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 41 deletions.
8 changes: 7 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ Contributing
===
## Style
- **Indent**: Tabs
- **Spacing**: See existing pages
- **One Line Conditionals**: Never
- **Comments**: Use Doxygen compatible comments [https://www.stack.nl/~dimitri/doxygen/manual/docblocks.html](https://www.stack.nl/~dimitri/doxygen/manual/docblocks.html)
- **General**: The goal is to be as clear as possible. Shorter code is not always better.

## Documentation
- Do not update documentation files! In other words, don't make pull requests for `gh-pages` branch which contains everything in the `/extra/documentation/html` directory, they will be ignored.
- Documentation is built using Doxygen
- Do not update documentation files! In other words, don't make pull requests for `gh-pages` branch which contains everything in the `/extra/documentation` directory, they will be ignored.
- Do add documentation to code in the form of Doxygen compatible comments.

## Issues
- Report issues [here](https://github.com/fenichelar/Pin/issues)
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ myPin.setHigh();
myPin.setLow();
~~~~~~~~~~~~~

## PWM
Set duty cycle to ~50% (not all pins support PWM)
~~~~~~~~~~~~~{.cpp}
myPin.setDutyCycle(127);
~~~~~~~~~~~~~

## Get Pin Info
Get mode (INPUT/OUTPUT)
~~~~~~~~~~~~~{.cpp}
Expand All @@ -73,7 +79,7 @@ myPin.toggleState();

## Simultaneous Operations on Multiple Pins

All Pins in array must use the same DDR, PORT, and PIN registers. Look at the corresponding file in the boards directory to determine what registers each pin uses on a given board. Because this library is built for speed, the array of pins is not automatically checked to be valid for simultaneous operations. An invalid array will produce unexpected results without error, therefore it is highly recommended that the array be validated using the `checkPinGroup` function during setup.
All Pins in array must use the same DDR, PORT, and PIN registers. Look at the Arduino documentation for your board to determine what registers each pin uses. Because this library is built for speed, the array of pins is not automatically checked to be valid for simultaneous operations. An invalid array will produce unexpected results without error, therefore it is highly recommended that the array be validated using the `checkPinGroup()` function during setup.

Import Pin library with support for simultaneous operations
~~~~~~~~~~~~~{.cpp}
Expand Down
6 changes: 5 additions & 1 deletion examples/Pin-Group/Pin-Group.ino
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <PinGroup.h> // Include Pin library group functions

// The Pins used in this array must all be on the same DDR and PORT registers
// Look at the coresponding file in the boards directory to determine what register each pin is on
// Look at the Arduino documentation for your board to determine what registers each pin uses
Pin myPinGroup[] = {2,3,5}; // Create array of Pin objects for digital pins labelled 2,3,5 on the Arduino Uno or Mega (not valid for Leonardo)

/**
Expand All @@ -35,6 +35,10 @@ void loop() {

delay(200); // Wait 200 milliseconds

setOutputHigh(myPinGroup); // Simultaneously set array of Pins to output high

delay(200); // Wait 200 milliseconds

setInput(myPinGroup); // Simultaneously set array of Pins to input mode

delay(200); // Wait 200 milliseconds
Expand Down
25 changes: 25 additions & 0 deletions examples/Pin-PWM/Pin-PWM.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
@file Pin-PWM.ino
@author Alec Fenichel
@brief Pin PWM example
@details Use PWM with the Pin library
*/

#include <Pin.h> // Include Pin library

Pin myPin = Pin(5); // Create Pin object for digital pin labelled 5 on any of the supported boards

/**
Called at start
*/
void setup() {
myPin.setOutput(); // Set Pin to output mode
// The Pin must support PWM
myPin.setDutyCycle(127); // Set Pin duty cycle to 127 (~50%)
}

/**
Called continously after setup
*/
void loop() {
}
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ setHigh KEYWORD2
setLow KEYWORD2
setOutputHigh KEYWORD2
setOutputLow KEYWORD2
setDutyCycle KEYWORD2

toggleMode KEYWORD2
toggleState KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Pin
version=2.2.3
version=2.3.0
author=Alec Fenichel <[email protected]>
maintainer=Alec Fenichel <[email protected]>
sentence=An easy to use Arduino library for fast digital I/O using port manipulation.
Expand Down
44 changes: 15 additions & 29 deletions src/Pin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@

#include "Pin.h"

/**
Struct for storing register addresses and offset
*/
struct pinMapping {
volatile uint8_t* pin; ///< Address of the PIN register, used to read pin if the pin is set as an input
volatile uint8_t* port; ///< Address of the PORT register, used to set output if the pin is set as an output or set pull-up resistor if the pin is set as an input
volatile uint8_t* ddr; ///< Address of the DDR register, used to define data direction
const uint8_t offset; ///< Bit mask for specific pin inside register
const uint8_t timer; ///< Timer used for specific pin
};


// ################################# Constructors #################################

Expand All @@ -39,11 +28,7 @@ Pin::Pin(uint8_t number) {
*/
Pin::Pin(uint8_t number, bool analog) {
if (analog) {
#ifdef Board_H
init(number + ANALOGOFFSET);
#else
init(analogInputToDigitalPin(number));
#endif
init(analogInputToDigitalPin(number));
} else {
init(number);
}
Expand All @@ -56,19 +41,11 @@ Pin::Pin(uint8_t number, bool analog) {
*/
void Pin::init(uint8_t number) {
_number = number;
#ifdef Board_H
_offset = pinMappings[_number].offset;
_timer = pinMappings[_number].timer;
_PIN = pinMappings[_number].pin;
_PORT = pinMappings[_number].port;
_DDR = pinMappings[_number].ddr;
#else
_offset = digitalPinToBitMask(_number);
_timer = digitalPinToTimer(_number);
_PIN = portInputRegister(digitalPinToPort(_number));
_PORT = portOutputRegister(digitalPinToPort(_number));
_DDR = portModeRegister(digitalPinToPort(_number));
#endif
_offset = digitalPinToBitMask(_number);
_timer = digitalPinToTimer(_number);
_PIN = portInputRegister(digitalPinToPort(_number));
_PORT = portOutputRegister(digitalPinToPort(_number));
_DDR = portModeRegister(digitalPinToPort(_number));
}


Expand Down Expand Up @@ -300,6 +277,15 @@ void Pin::setOutputLow() {
PORT_LOW;
}

/**
Set the PWM duty cycle
@param value the duty cycle (0-255)
*/
void Pin::setDutyCycle(int value) {
analogWrite(_number,value);
}

// #################### Toggle ####################

/**
Expand Down
6 changes: 2 additions & 4 deletions src/Pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#include "Arduino.h"


#ifndef Pin_H
#define Pin_H
#pragma once


// ################################# Defines #################################
Expand Down Expand Up @@ -65,6 +64,7 @@ class Pin {
void setLow();
void setOutputHigh();
void setOutputLow();
void setDutyCycle(int value);
// Toggle
void toggleMode();
void toggleState();
Expand All @@ -80,5 +80,3 @@ class Pin {
volatile uint8_t* _PORT;
volatile uint8_t* _DDR;
};

#endif
5 changes: 1 addition & 4 deletions src/PinGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#include "Pin.h"


#ifndef PinGroup_H
#define PinGroup_H
#pragma once


// ################################# Defines #################################
Expand Down Expand Up @@ -206,5 +205,3 @@ bool checkPinGroup(Pin (&pins)[N]) {

return true;
}

#endif

0 comments on commit 62969e5

Please sign in to comment.