AStar32U4 library
AStar32U4.h
Go to the documentation of this file.
1 // Copyright Pololu Corporation. For more information, see http://www.pololu.com/
2 
9 #pragma once
10 
11 #include <FastGPIO.h>
12 #include <Pushbutton.h>
13 #include <AStar32U4LCD.h>
14 #include <AStar32U4Buzzer.h>
15 #include <AStar32U4Buttons.h>
16 #include <AStar32U4Motors.h>
17 #include <avr/io.h>
18 #include <stdint.h>
19 
27 inline void ledRed(bool on)
28 {
30 }
31 
35 inline void ledYellow(bool on)
36 {
38 }
39 
47 inline void ledGreen(bool on)
48 {
50 }
51 
60 inline bool usbPowerPresent()
61 {
62  return USBSTA >> VBUS & 1;
63 }
64 
82 inline uint16_t readBatteryMillivoltsLV4(uint8_t pin = A1)
83 {
84  const uint8_t sampleCount = 8;
85  uint16_t sum = 0;
86  for (uint8_t i = 0; i < sampleCount; i++)
87  {
88  sum += analogRead(pin);
89  }
90 
91  // VBAT = 4 * millivolt reading = 4 * raw * 5000/1024
92  // = raw * 625 / 32
93  // The correction number below makes it so that we round to the nearest
94  // whole number instead of always rounding down.
95  const uint16_t correction = 16 * sampleCount - 1;
96  return ((uint32_t)sum * 625 + correction) / (32 * sampleCount);
97 }
98 
117 inline uint16_t readBatteryMillivoltsLV3(uint8_t pin = A1)
118 {
119  const uint8_t sampleCount = 8;
120  uint16_t sum = 0;
121  for (uint8_t i = 0; i < sampleCount; i++)
122  {
123  sum += analogRead(pin);
124  }
125 
126  // VBAT = 3 * millivolt reading = 3 * raw * 5000/1024
127  // = raw * 1875 / 128
128  // The correction number below makes it so that we round to the nearest
129  // whole number instead of always rounding down.
130  const uint16_t correction = 64 * sampleCount - 1;
131  return ((uint32_t)sum * 1875 + correction) / (128 * sampleCount);
132 }
133 
152 inline uint16_t readBatteryMillivoltsSV(uint8_t pin = A1)
153 {
154  const uint8_t sampleCount = 8;
155  uint16_t sum = 0;
156  for (uint8_t i = 0; i < sampleCount; i++)
157  {
158  sum += analogRead(pin);
159  }
160 
161  // VBAT = 8 * millivolt reading = 8 * raw * 5000/1024
162  // = raw * 625 / 16
163  // The correction number below makes it so that we round to the nearest
164  // whole number instead of always rounding down.
165  const uint16_t correction = 8 * sampleCount - 1;
166  return ((uint32_t)sum * 625 + correction) / (16 * sampleCount);
167 }
168 
172 inline uint16_t readBatteryMillivolts(uint8_t pin = A1)
173 {
174  return readBatteryMillivoltsLV3(pin);
175 }
176 
180 inline uint16_t readBatteryMillivoltsLV(uint8_t pin = A1)
181 {
182  return readBatteryMillivoltsLV3(pin);
183 }
static void setOutput(bool value) __attribute__((always_inline))
Sets the pin as an output.
Definition: FastGPIO.h:260
void ledYellow(bool on)
Turns the yellow user LED on pin 13 on or off.
Definition: AStar32U4.h:35
uint16_t readBatteryMillivoltsLV3(uint8_t pin=A1)
Reads the battery voltage for an A-Star 32U4 Prime LV (ac03b) or Robot Controller LV and returns it i...
Definition: AStar32U4.h:117
bool usbPowerPresent()
Returns true if USB power is detected.
Definition: AStar32U4.h:60
uint16_t readBatteryMillivoltsSV(uint8_t pin=A1)
Reads the battery voltage for an A-Star 32U4 Prime SV or Robot Controller SV and returns it in milliv...
Definition: AStar32U4.h:152
uint16_t readBatteryMillivolts(uint8_t pin=A1)
Definition: AStar32U4.h:172
uint16_t readBatteryMillivoltsLV(uint8_t pin=A1)
Definition: AStar32U4.h:180
uint16_t readBatteryMillivoltsLV4(uint8_t pin=A1)
Reads the battery voltage for an A-Star 32U4 Prime LV (ac03e) and returns it in millivolts.
Definition: AStar32U4.h:82
void ledGreen(bool on)
Turns the green user LED (TX) on or off.
Definition: AStar32U4.h:47
void ledRed(bool on)
Turns the red user LED (RX) on or off.
Definition: AStar32U4.h:27