Skip to content

Commit

Permalink
Initial commit: files copied from a-star repository and renamed to re…
Browse files Browse the repository at this point in the history
…move "Prime"
  • Loading branch information
kevin-pololu committed Aug 6, 2015
0 parents commit 0d4cd52
Show file tree
Hide file tree
Showing 28 changed files with 4,498 additions and 0 deletions.
1 change: 1 addition & 0 deletions AStar32U4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This file is left empty intentionally.
131 changes: 131 additions & 0 deletions AStar32U4.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright Pololu Corporation. For more information, see http://www.pololu.com/

/*! \file AStar32U4Prime.h
*
* This is the main header file for the %AStar32U4Prime library.
* It includes all the other header files provided by the library.
*/

#pragma once

#include <FastGPIO.h>
#include <Pushbutton.h>
#include <AStar32U4PrimeLCD.h>
#include <AStar32U4PrimeBuzzer.h>
#include <AStar32U4PrimeButtons.h>
#include <avr/io.h>
#include <stdint.h>

/*! \brief Turns the red user LED (RX) on or off.
@param on 1 to turn on the LED, 0 to turn it off.
The red user LED is on pin 17, which is also known as PB0, SS, and RXLED. The
Arduino core code uses this LED to indicate when it receives data over USB, so
it might be hard to control this LED when USB is connected. */
inline void ledRed(bool on)
{
FastGPIO::Pin<17>::setOutput(!on);
}

/*! \brief Turns the yellow user LED on pin 13 on or off.
@param on 1 to turn on the LED, 0 to turn it off. */
inline void ledYellow(bool on)
{
FastGPIO::Pin<13>::setOutput(on);
}

/*! \brief Turns the green user LED (TX) on or off.
@param on 1 to turn on the LED, 0 to turn it off.
The green user LED is pin PD5, which is also known as TXLED. The Arduino core
code uses this LED to indicate when it receives data over USB, so it might be
hard to control this LED when USB is connected. */
inline void ledGreen(bool on)
{
FastGPIO::Pin<IO_D5>::setOutput(!on);
}

/*! \brief Returns true if USB power is detected.
This function returns true if power is detected on the board's USB port and
returns false otherwise. It uses the ATmega32U4's VBUS line, which is directly
connected to the power pin of the USB connector.
\sa A method for detecting whether the board's virtual COM port is open:
http://arduino.cc/en/Serial/IfSerial */
inline bool usbPowerPresent()
{
return USBSTA >> VBUS & 1;
}

/*! \brief Reads the battery voltage for an A-Star 32U4 Prime LV and returns it
in millivolts.
This function performs an analog reading and uses it to compute the voltage on
the A-Star 32U4 Prime LV's VIN pin in millivolts. This only works if the
specified pin (A1 by default) has been connected to BATLEV.
This function is only meant to be run on the A-Star 32U4 Prime LV (the blue
board) and will give incorrect results on other versions.
@param pin The pin number to read. This argument is passed on to analogRead.
The default value is `A1`.
\sa readBatteryMillivoltsSV() */
inline uint16_t readBatteryMillivoltsLV(uint8_t pin = A1)
{
const uint8_t sampleCount = 8;
uint16_t sum = 0;
for (uint8_t i = 0; i < sampleCount; i++)
{
sum += analogRead(pin);
}

// VBAT = 3 * millivolt reading = 3 * raw * 5000/1024
// = raw * 1875 / 128
// The correction number below makes it so that we round to the nearest
// whole number instead of always rounding down.
const uint16_t correction = 64 * sampleCount - 1;
return ((uint32_t)sum * 1875 + correction) / (128 * sampleCount);
}

/*! \brief Reads the battery voltage for an A-Star 32U4 Prime SV and returns it
in millivolts.
This function performs an analog reading and uses it to compute the voltage on
the A-Star 32U4 Prime SV's VIN pin in millivolts. This only works if the
specified pin (A1 by default) has been connected to BATLEV.
This function is only meant to be run on the A-Star 32U4 Prime SV (the green
board) and will give incorrect results on other versions.
@param pin The pin number to read. This argument is passed on to analogRead.
The default value is `A1`.
\sa readBatteryMillivoltsLV() */
inline uint16_t readBatteryMillivoltsSV(uint8_t pin = A1)
{
const uint8_t sampleCount = 8;
uint16_t sum = 0;
for (uint8_t i = 0; i < sampleCount; i++)
{
sum += analogRead(pin);
}

// VBAT = 8 * millivolt reading = 8 * raw * 5000/1024
// = raw * 625 / 16
// The correction number below makes it so that we round to the nearest
// whole number instead of always rounding down.
const uint16_t correction = 8 * sampleCount - 1;
return ((uint32_t)sum * 625 + correction) / (16 * sampleCount);
}

/*! \deprecated This function is deprecated and is only here for backwards
compatibility. We recommend using readBatteryMillivoltsLV() instead. */
inline uint16_t readBatteryMillivolts(uint8_t pin = A1)
{
return readBatteryMillivoltsLV(pin);
}
99 changes: 99 additions & 0 deletions AStar32U4Buttons.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright Pololu Corporation. For more information, see http://www.pololu.com/

/** \file AStar32U4PrimeButtons.h */

#pragma once

#include <Pushbutton.h>
#include <FastGPIO.h>
#include <USBPause.h>
#include <SPIPause.h>
#include <util/delay.h>

/*! The pin number for the pin connected to button A on the A-Star 32U4
* Prime. */
#define A_STAR_32U4_PRIME_BUTTON_A 14

/*! The pin number for the pin connected to button B on the A-Star 32U4 Prime.
* Note that this is not an official Arduino pin number so it cannot be used
* with functions like digitalRead, but it can be used with the FastGPIO
* library. */
#define A_STAR_32U4_PRIME_BUTTON_B IO_D5

/*! The pin number for the pin connected to button C on the A-Star 32U4
* Prime. */
#define A_STAR_32U4_PRIME_BUTTON_C 17

/*! \brief Interfaces with button A on the A-Star 32U4 Prime.
*
* The pin used for button A is also used for reading the DO pin on the microSD
* card. If the chip select (CS) pin for the microSD card is low (active), you
* cannot read button A because the signal from the microSD card will override
* the signal from the button. Therefore, the CS pin needs to be high whenever
* functions in this class are called. The CS pin is high by default and the
* Arduino's SD library leaves CS high when the microSD card is not being used,
* so most users will not need to worry about that. */
class AStar32U4PrimeButtonA : public Pushbutton
{
public:
AStar32U4PrimeButtonA() : Pushbutton(A_STAR_32U4_PRIME_BUTTON_A)
{
}
};

/*! \brief Interfaces with button B on the A-Star 32U4 Prime.
*
* The pin used for button B is also used for the TX LED.
*
* This class temporarily disables USB interrupts because the Arduino core code
* has USB interrupts enabled that sometimes write to the pin this button is on.
*
* This class temporarily sets the pin to be an input without a pull-up
* resistor. The pull-up resistor is not needed because of the resistors on the
* board. */
class AStar32U4PrimeButtonB : public PushbuttonBase
{
public:
virtual bool isPressed()
{
// These objects take care of disabling USB interrupts temporarily
// and restoring the pin to its previous state at the end.
USBPause usbPause;
FastGPIO::PinLoan<A_STAR_32U4_PRIME_BUTTON_B> loan;

FastGPIO::Pin<A_STAR_32U4_PRIME_BUTTON_B>::setInput();
_delay_us(3);
return !FastGPIO::Pin<A_STAR_32U4_PRIME_BUTTON_B>::isInputHigh();
}
};

/*! \brief Interfaces with button C on the A-Star 32U4 Prime.
*
* The pin used for button C is also used for the RX LED.
*
* This class temporarily disables USB interrupts because the Arduino core code
* has USB interrupts enabled that sometimes write to the pin this button is on.
*
* This class temporarily disables the AVR's hardware SPI module because it
* might have been enabled by the Arduino's SD library, and the pin for button C
* is the SPI SS line. If it is an input and it reads low, the state of the SPI
* module will be changed.
*
* This class temporarily sets the pin to be an input without a pull-up
* resistor. The pull-up resistor is not needed because of the resistors on the
* board.
*/
class AStar32U4PrimeButtonC : public PushbuttonBase
{
public:
virtual bool isPressed()
{
SPIPause spiPause;
USBPause usbPause;
FastGPIO::PinLoan<A_STAR_32U4_PRIME_BUTTON_C> loan;

FastGPIO::Pin<A_STAR_32U4_PRIME_BUTTON_C>::setInput();
_delay_us(3);
return !FastGPIO::Pin<A_STAR_32U4_PRIME_BUTTON_C>::isInputHigh();
}
};
29 changes: 29 additions & 0 deletions AStar32U4Buzzer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright Pololu Corporation. For more information, see http://www.pololu.com/

/*! \file AStar32U4PrimeBuzzer.h */

#pragma once

#include <PololuBuzzer.h>

/*! \brief Main class for interfacing with the A-Star 32U4 Prime Buzzer.
*
* This class uses Timer 4 and pin 6 (PD7/OC4D) to play beeps and melodies on
* the A-Star 32U4 Prime Buzzer.
*
* Note durations are timed using a timer overflow interrupt
* (`TIMER4_OVF`), which will briefly interrupt execution of your
* main program at the frequency of the sound being played. In most cases, the
* interrupt-handling routine is very short (several microseconds). However,
* when playing a sequence of notes in `PLAY_AUTOMATIC` mode (the default mode)
* with the `play()` command, this interrupt takes much longer than normal
* (perhaps several hundred microseconds) every time it starts a new note. It is
* important to take this into account when writing timing-critical code.
*/
class AStar32U4PrimeBuzzer : public PololuBuzzer
{
// This is a trivial subclass of PololuBuzzer; it exists because we wanted
// the A-Star class names to be consistent and we didn't just use a typedef
// to define it because that would make the Doxygen documentation harder to
// understand.
};
109 changes: 109 additions & 0 deletions AStar32U4LCD.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright Pololu Corporation. For more information, see http://www.pololu.com/

/*! \file AStar32U4PrimeLCD.h */

#pragma once

#include <PololuHD44780.h>
#include <FastGPIO.h>
#include <USBPause.h>
#include <SPIPause.h>

/*! \brief Main class for interfacing with the A-Star 32U4 Prime LCD.
*
* This class interfaces with an HD44780-compatible LCD connected to the LCD
* connector on the A-Star 32U4 Prime.
*
* This library is similar to the Arduino
* [LiquidCrystal library](http://arduino.cc/en/Reference/LiquidCrystal),
* but it has some extra features needed on the A-Star 32U4 Prime:
*
* * This class disables USB interrupts temporarily while writing to the LCD so
* that the USB interrupts will not change the RXLED and TXLED pins, which
* double as LCD data lines.
* * This class disables the AVR's hardware SPI module temporarily so that
* it can use the PB3/MISO/DB4 pin as an output to control the LCD.
* * This class restores the RS, DB4, DB5, DB6, and DB7 pins to their previous
* states when it is done using them so that those pins can also be used for
* other purposes such as controlling LEDs.
*
* This class assumes that you are using the default pin mapping that you would
* get by simply connecting an LCD to the 2x7 LCD connector on the A-Star 32U4
* Prime:
*
* * RS = Pin 7 (PE6)
* * E = Pin 8 (PB4)
* * DB4 = Pin 14 (PB3/MISO)
* * DB5 = Pin 17 (PB0/SS/RXLED)
* * DB6 = Pin 13 (PC7)
* * DB7 = PD5/TXLED (which has no official pin number)
*
* If you want to control your LCD with different pins, you can edit the line
* labeled "Pin assignments" in AStar32U4PrimeLCD.h or you can make your own new
* LCD class in your sketch by copying the code for the AStar32U4PrimeLCD class,
* which is not very long.
*
* This class inherits from the Arduino Print class, so you can call the
* `print()` function on it with a variety of arguments. See the
* [Arduino print() documentation](http://arduino.cc/en/Serial/Print)
* for more information.
*
* For detailed information about HD44780 LCD interface, including what
* characters can be displayed, see the
* [HD44780 datasheet](http://www.pololu.com/file/0J72/HD44780.pdf).
*/
class AStar32U4PrimeLCD : public PololuHD44780Base
{
// Pin assignments. Change this if you want to remap your pins.
static const uint8_t rs = 7, e = 8, db4 = 14, db5 = 17, db6 = 13, db7 = IO_D5;

public:

virtual void initPins()
{
FastGPIO::Pin<e>::setOutputLow();
}

virtual void send(uint8_t data, bool rsValue, bool only4bits)
{
// Temporarily disable the AVR's hardware SPI module; if it is enabled
// then MISO is forced to be an input, and the Arduino SD library
// generally leaves it enabled.
SPIPause spiPause;

// Temporarily disable USB interrupts because they write some pins
// we are using as LCD pins.
USBPause usbPause;

// Save the state of the RS and data pins. The state automatically
// gets restored before this function returns.
FastGPIO::PinLoan<rs> loanRS;
FastGPIO::PinLoan<db4> loanDB4;
FastGPIO::PinLoan<db5> loanDB5;
FastGPIO::PinLoan<db6> loanDB6;
FastGPIO::PinLoan<db7> loanDB7;

// Drive the RS pin high or low.
FastGPIO::Pin<rs>::setOutput(rsValue);

// Send the data.
if (!only4bits) { sendNibble(data >> 4); }
sendNibble(data & 0x0F);
}

private:

void sendNibble(uint8_t data)
{
FastGPIO::Pin<db4>::setOutput(data >> 0 & 1);
FastGPIO::Pin<db5>::setOutput(data >> 1 & 1);
FastGPIO::Pin<db6>::setOutput(data >> 2 & 1);
FastGPIO::Pin<db7>::setOutput(data >> 3 & 1);

FastGPIO::Pin<e>::setOutputHigh();
_delay_us(1); // Must be at least 450 ns.
FastGPIO::Pin<e>::setOutputLow();
_delay_us(1); // Must be at least 550 ns.
}
};

11 changes: 11 additions & 0 deletions Doxyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# Doxygen configuration file for generating documentation.
PROJECT_NAME = "AStar32U4Prime library"
OUTPUT_DIRECTORY = docs
INLINE_INHERITED_MEMB = YES
INPUT = .
USE_MDFILE_AS_MAINPAGE = README.md
RECURSIVE = YES
SOURCE_BROWSER = YES
USE_MATHJAX = YES
GENERATE_LATEX = NO
Loading

0 comments on commit 0d4cd52

Please sign in to comment.