Serial Wombat Arduino Library
SerialWombatPWM.h
Go to the documentation of this file.
1 #pragma once
2 /*
3 Copyright 2020-2021 Broadwell Consulting Inc.
4 
5 "Serial Wombat" is a registered trademark of Broadwell Consulting Inc. in
6 the United States. See SerialWombat.com for usage guidance.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25 */
26 
27 
28 #include <stdint.h>
29 #include "SerialWombat.h"
33 typedef enum
34 {
52 
81 {
82 public:
87  SerialWombatPWM(SerialWombatChip& serialWombat) :SerialWombatPin(serialWombat) {}
88 
95  void begin(uint8_t pin, uint16_t dutyCycle = 0,bool invert = false)
96  {
97  _pin = pin;
98  _pinMode = (uint8_t)PIN_MODE_PWM;
99  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE0,_pin,PIN_MODE_PWM,_pin,(uint8_t)(dutyCycle & 0xFF),(uint8_t)(dutyCycle >> 8),invert,0x55 };
100  _sw.sendPacket(tx);
101  }
102 
103  /*
104  \brief Set PWM duty cycle
105  \param dutyCycle A value from 0 to 65535 representing duty cycle
106  */
107  void writeDutyCycle(uint16_t dutyCycle)
108  {
109  uint8_t tx[] = { (uint8_t)SerialWombatCommands::COMMAND_BINARY_SET_PIN_BUFFFER,_pin,(uint8_t)(dutyCycle & 0xFF),(uint8_t)(dutyCycle >>8),255,0x55,0x55 };
110  _sw.sendPacket(tx);
111  }
112 
113 private:
114 };
115 
116 
119 {
120 public:
121  SerialWombatPWM_4AB(SerialWombatChip& serialWombat):SerialWombatPWM(serialWombat){}
122  /*
123  \brief Set PWM Frequency (Adjusts all PWM outputs' frequency on a SerialWombat 4A/B chip)
124  \param frequency A value of the #Wombat4A_B_PWMFrequencyValues_t enumeration
125 
126  This function changes the Serial Wombat 4A and 4B PWM output frequncy by adjusting
127  the clock divisor for the PWM generation hardware. By default the value is 31250Hz.
128  Changing the frequency may reduce PWM resolution from 10 bits to 8 bits for some
129  frequencies. However, the input value for duty cycle for methods of this class
130  continue to be 0 to 65535 and are scaled accordingly.
131 
132  \warning This function will likely not be compatible with other models in the Serial Wombat
133  family based on other hardware that are released in the future because it is tightly coupled to the
134  PIC16F15214 hardware.
135  */
137  {
138  uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE_HW_0,_pin,PIN_MODE_PWM,(uint8_t)(frequency),0x55,0x55,0x55,0x55 };
139  _sw.sendPacket(tx);
140  }
141 };
142 
145 {
146 public:
148  {}
149 
155  void writeFrequency_Hz(uint32_t frequency_Hz)
156  {
157  uint8_t tx[] = { 220,_pin,PIN_MODE_PWM,SW_LE32(1000000 / frequency_Hz),0x55 };
158  _sw.sendPacket(tx);
159 
160  }
161 
167  void writePeriod_uS(uint32_t period_uS)
168  {
169  uint8_t tx[] = { 220,_pin,PIN_MODE_PWM,SW_LE32(period_uS),0x55 };
170  _sw.sendPacket(tx);
171  }
172 
177  uint8_t pin()
178 {
179  return SerialWombatPin::_pin;
180 }
185  uint8_t swPinModeNumber()
186  {
188  }
189 };
190 
SerialWombatPWM_18AB::SerialWombatPWM_18AB
SerialWombatPWM_18AB(SerialWombatChip &serialWombat)
Definition: SerialWombatPWM.h:147
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition: SerialWombat.h:279
SW4AB_PWMFrequency_15625_Hz
@ SW4AB_PWMFrequency_15625_Hz
Definition: SerialWombatPWM.h:49
SerialWombatPWM_18AB::pin
uint8_t pin()
fulfills a virtual function requirement of SerialWombatAbstractScaledOutput
Definition: SerialWombatPWM.h:177
SW4AB_PWMFrequency_31250_Hz
@ SW4AB_PWMFrequency_31250_Hz
Definition: SerialWombatPWM.h:50
SerialWombatCommands::CONFIGURE_PIN_MODE0
@ CONFIGURE_PIN_MODE0
(200)
SW4AB_PWMFrequency_244_Hz
@ SW4AB_PWMFrequency_244_Hz
Definition: SerialWombatPWM.h:43
SW4AB_PWMFrequency_1_Hz
@ SW4AB_PWMFrequency_1_Hz
Definition: SerialWombatPWM.h:35
SW4AB_PWMFrequency_488_Hz
@ SW4AB_PWMFrequency_488_Hz
Definition: SerialWombatPWM.h:44
SW4AB_PWMFrequency_63_Hz
@ SW4AB_PWMFrequency_63_Hz
Definition: SerialWombatPWM.h:41
SerialWombatPWM::SerialWombatPWM
SerialWombatPWM(SerialWombatChip &serialWombat)
Constructor for SerialWombatPWM class.
Definition: SerialWombatPWM.h:87
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition: SerialWombat.h:1566
SW4AB_PWMFrequency_4_Hz
@ SW4AB_PWMFrequency_4_Hz
Definition: SerialWombatPWM.h:37
SerialWombatPWM_4AB
Extends the SerialWombatPWM class with SW4A/SW4B specific functionality.
Definition: SerialWombatPWM.h:118
PIN_MODE_PWM
@ PIN_MODE_PWM
(16)
Definition: SerialWombat.h:252
SW4AB_PWMFrequency_8_Hz
@ SW4AB_PWMFrequency_8_Hz
Definition: SerialWombatPWM.h:38
SerialWombatAbstractScaledOutput
SerialWombatServo, SerialWombatPWM and other proportional ouptut classes inherit from this class....
Definition: SerialWombatAbstractScaledOutput.h:62
SerialWombatPWM
A class representing a Serial Wombat PWM output.
Definition: SerialWombatPWM.h:80
SW4AB_PWMFrequency_976_Hz
@ SW4AB_PWMFrequency_976_Hz
Definition: SerialWombatPWM.h:45
SerialWombatPWM_4AB::setFrequency_SW4AB
void setFrequency_SW4AB(Wombat4A_B_PWMFrequencyValues_t frequency)
Definition: SerialWombatPWM.h:136
SerialWombat.h
SerialWombatPWM::writeDutyCycle
void writeDutyCycle(uint16_t dutyCycle)
Definition: SerialWombatPWM.h:107
SerialWombatPWM_18AB::writePeriod_uS
void writePeriod_uS(uint32_t period_uS)
Set the PWM period on a Serial Wombat 18AB chip's PWM.
Definition: SerialWombatPWM.h:167
SW_LE32
#define SW_LE32(_a)
Convert a uint32_t to four bytes in little endian format for array initialization.
Definition: SerialWombat.h:44
SerialWombatPWM_18AB::swPinModeNumber
uint8_t swPinModeNumber()
fulfills a virtual function requirement of SerialWombatAbstractScaledOutput
Definition: SerialWombatPWM.h:185
SerialWombatPin
Describes a Serial Wombat Pin. Is base class for other pin modes.
Definition: SerialWombat.h:1470
SerialWombatPWM_4AB::SerialWombatPWM_4AB
SerialWombatPWM_4AB(SerialWombatChip &serialWombat)
Definition: SerialWombatPWM.h:121
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition: SerialWombat.h:1567
SerialWombatChip::sendPacket
int sendPacket(uint8_t tx[], uint8_t rx[])
Send an 8 byte packet to the Serial Wombat chip and wait for 8 bytes back.
Definition: SerialWombat.cpp:139
SerialWombatCommands::CONFIGURE_PIN_MODE_HW_0
@ CONFIGURE_PIN_MODE_HW_0
(220)
Wombat4A_B_PWMFrequencyValues_t
Wombat4A_B_PWMFrequencyValues_t
Definition: SerialWombatPWM.h:33
SW4AB_PWMFrequency_1952_Hz
@ SW4AB_PWMFrequency_1952_Hz
Definition: SerialWombatPWM.h:46
SW4AB_PWMFrequency_7800_Hz
@ SW4AB_PWMFrequency_7800_Hz
Definition: SerialWombatPWM.h:48
SerialWombatPin::pin
uint8_t pin()
Returns the current SW pin number. Used primarily for virtual calls by derived classes.
Definition: SerialWombat.h:1553
SerialWombatPin::_pin
uint8_t _pin
Definition: SerialWombat.h:1565
SerialWombatPWM_18AB
Extends the SerialWombatPWM class with SW18AB specific functionality, including SerialWombatAbstractS...
Definition: SerialWombatPWM.h:144
SerialWombatPWM::begin
void begin(uint8_t pin, uint16_t dutyCycle=0, bool invert=false)
Initialize a pin that has been declared as PWM.
Definition: SerialWombatPWM.h:95
SW4AB_PWMFrequency_3900_Hz
@ SW4AB_PWMFrequency_3900_Hz
Definition: SerialWombatPWM.h:47
SW4AB_PWMFrequency_16_Hz
@ SW4AB_PWMFrequency_16_Hz
Definition: SerialWombatPWM.h:39
SerialWombatPWM_18AB::writeFrequency_Hz
void writeFrequency_Hz(uint32_t frequency_Hz)
Set the PWM frequency on a Serial Wombat 18AB chip's PWM.
Definition: SerialWombatPWM.h:155
SW4AB_PWMFrequency_32_Hz
@ SW4AB_PWMFrequency_32_Hz
Definition: SerialWombatPWM.h:40
SW4AB_PWMFrequency_2_Hz
@ SW4AB_PWMFrequency_2_Hz
Definition: SerialWombatPWM.h:36
SerialWombatCommands::COMMAND_BINARY_SET_PIN_BUFFFER
@ COMMAND_BINARY_SET_PIN_BUFFFER
(0x82)
SW4AB_PWMFrequency_125_Hz
@ SW4AB_PWMFrequency_125_Hz
Definition: SerialWombatPWM.h:42