Serial Wombat Arduino Library

Loading...
Searching...
No Matches
SerialWombatPWM.h
Go to the documentation of this file.
1#pragma once
2/*
3 Copyright 2020-2024 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"
32
33 typedef enum
34{
35 SW4AB_PWMFrequency_1_Hz = 0x76,
36 SW4AB_PWMFrequency_2_Hz = 0x66,
37 SW4AB_PWMFrequency_4_Hz = 0x56,
38 SW4AB_PWMFrequency_8_Hz = 0x46,
39 SW4AB_PWMFrequency_16_Hz = 0x75,
40 SW4AB_PWMFrequency_32_Hz = 0x65,
41 SW4AB_PWMFrequency_63_Hz = 0x55,
42 SW4AB_PWMFrequency_125_Hz = 0x45,
43 SW4AB_PWMFrequency_244_Hz = 0x71,
44 SW4AB_PWMFrequency_488_Hz = 0x61,
45 SW4AB_PWMFrequency_976_Hz = 0x51,
46 SW4AB_PWMFrequency_1952_Hz = 0x41,
47 SW4AB_PWMFrequency_3900_Hz = 0x31,
48 SW4AB_PWMFrequency_7800_Hz = 0x21,
49 SW4AB_PWMFrequency_15625_Hz = 0x11,
50 SW4AB_PWMFrequency_31250_Hz = 0x01,
51}Wombat4A_B_PWMFrequencyValues_t;
52
79
80 class SerialWombatPWM : public SerialWombatPin
81{
82 public:
87 SerialWombatPWM(SerialWombatChip& serialWombat) :SerialWombatPin(serialWombat) {}
88
95 int16_t begin(uint8_t pin, uint16_t dutyCycle = 0,bool invert = false)
96 {
97 _pin = pin;
98 _pinMode = (uint8_t)PIN_MODE_PWM;
99 /*
100 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE0,_pin,PIN_MODE_PWM,_pin,(uint8_t)(dutyCycle & 0xFF),(uint8_t)(dutyCycle >> 8),invert,0x55 };
101 _sw.sendPacket(tx);
102 */
103 return initPacketNoResponse(0,_pin,SW_LE16(dutyCycle),(uint8_t) invert);
104 }
105
106 /*
107 \brief Set PWM duty cycle
108 \param dutyCycle A value from 0 to 65535 representing duty cycle
109 */
110 void writeDutyCycle(uint16_t dutyCycle)
111 {
112 writePublicData(dutyCycle);
113 }
114
115 private:
116};
117
118
120 class SerialWombatPWM_4AB : public SerialWombatPWM
121{
122 public:
123 SerialWombatPWM_4AB(SerialWombatChip& serialWombat):SerialWombatPWM(serialWombat){}
124 /*
125 \brief Set PWM Frequency (Adjusts all PWM outputs' frequency on a SerialWombat 4A/B chip)
126 \param frequency A value of the #Wombat4A_B_PWMFrequencyValues_t enumeration
127
128 This function changes the Serial Wombat 4A and 4B PWM output frequncy by adjusting
129 the clock divisor for the PWM generation hardware. By default the value is 31250Hz.
130 Changing the frequency may reduce PWM resolution from 10 bits to 8 bits for some
131 frequencies. However, the input value for duty cycle for methods of this class
132 continue to be 0 to 65535 and are scaled accordingly.
133
134 \warning This function will likely not be compatible with other models in the Serial Wombat
135 family based on other hardware that are released in the future because it is tightly coupled to the
136 PIC16F15214 hardware.
137 */
138 int16_t setFrequency_SW4AB(Wombat4A_B_PWMFrequencyValues_t frequency)
139 {
140 /*
141 uint8_t tx[] = { (uint8_t)SerialWombatCommands::CONFIGURE_PIN_MODE_HW_0,_pin,PIN_MODE_PWM,(uint8_t)(frequency),0x55,0x55,0x55,0x55 };
142 _sw.sendPacket(tx);*/
143 return initPacketNoResponse(20,(uint8_t) frequency);
144 }
145};
146
148 class SerialWombatPWM_18AB: public SerialWombatPWM, public SerialWombatAbstractScaledOutput
149{
150 public:
151 SerialWombatPWM_18AB(SerialWombatChip& serialWombat) :SerialWombatPWM(serialWombat), SerialWombatAbstractScaledOutput(serialWombat)
152 {}
153
159 void writeFrequency_Hz(uint32_t frequency_Hz)
160 {
161 if (frequency_Hz == 0)
162 {
163 frequency_Hz = 1;
164 }
165 uint8_t tx[] = { 220,_pin,PIN_MODE_PWM,SW_LE32(1000000 / frequency_Hz),0x55 };
166 _sw.sendPacket(tx);
167
168 }
169
175 void writePeriod_uS(uint32_t period_uS)
176 {
177 uint8_t tx[] = { 220,_pin,PIN_MODE_PWM,SW_LE32(period_uS),0x55 };
178 _sw.sendPacket(tx);
179 }
180
185 uint8_t pin()
186{
187 return SerialWombatPin::_pin;
188}
189
193 uint8_t swPinModeNumber()
194 {
195 return SerialWombatPin::_pinMode;
196 }
197};
198
SerialWombat.h
SW_LE16
#define SW_LE16(_a)
Convert a uint16_t to two bytes in little endian format for array initialization.
Definition SerialWombat.h:46
SW_LE32
#define SW_LE32(_a)
Convert a uint32_t to four bytes in little endian format for array initialization.
Definition SerialWombat.h:49
PIN_MODE_PWM
@ PIN_MODE_PWM
(16)
Definition SerialWombat.h:276
Wombat4A_B_PWMFrequencyValues_t
Wombat4A_B_PWMFrequencyValues_t
Definition SerialWombatPWM.h:34
SW4AB_PWMFrequency_3900_Hz
@ SW4AB_PWMFrequency_3900_Hz
Definition SerialWombatPWM.h:47
SW4AB_PWMFrequency_8_Hz
@ SW4AB_PWMFrequency_8_Hz
Definition SerialWombatPWM.h:38
SW4AB_PWMFrequency_244_Hz
@ SW4AB_PWMFrequency_244_Hz
Definition SerialWombatPWM.h:43
SW4AB_PWMFrequency_4_Hz
@ SW4AB_PWMFrequency_4_Hz
Definition SerialWombatPWM.h:37
SW4AB_PWMFrequency_32_Hz
@ SW4AB_PWMFrequency_32_Hz
Definition SerialWombatPWM.h:40
SW4AB_PWMFrequency_63_Hz
@ SW4AB_PWMFrequency_63_Hz
Definition SerialWombatPWM.h:41
SW4AB_PWMFrequency_31250_Hz
@ SW4AB_PWMFrequency_31250_Hz
Definition SerialWombatPWM.h:50
SW4AB_PWMFrequency_125_Hz
@ SW4AB_PWMFrequency_125_Hz
Definition SerialWombatPWM.h:42
SW4AB_PWMFrequency_976_Hz
@ SW4AB_PWMFrequency_976_Hz
Definition SerialWombatPWM.h:45
SW4AB_PWMFrequency_488_Hz
@ SW4AB_PWMFrequency_488_Hz
Definition SerialWombatPWM.h:44
SW4AB_PWMFrequency_16_Hz
@ SW4AB_PWMFrequency_16_Hz
Definition SerialWombatPWM.h:39
SW4AB_PWMFrequency_15625_Hz
@ SW4AB_PWMFrequency_15625_Hz
Definition SerialWombatPWM.h:49
SW4AB_PWMFrequency_1952_Hz
@ SW4AB_PWMFrequency_1952_Hz
Definition SerialWombatPWM.h:46
SW4AB_PWMFrequency_2_Hz
@ SW4AB_PWMFrequency_2_Hz
Definition SerialWombatPWM.h:36
SW4AB_PWMFrequency_1_Hz
@ SW4AB_PWMFrequency_1_Hz
Definition SerialWombatPWM.h:35
SW4AB_PWMFrequency_7800_Hz
@ SW4AB_PWMFrequency_7800_Hz
Definition SerialWombatPWM.h:48
SerialWombatAbstractScaledOutput::SerialWombatAbstractScaledOutput
SerialWombatAbstractScaledOutput(SerialWombatChip &sw)
Constructor for the SerialWombatAbstractScaledOutput Class.
Definition SerialWombatAbstractScaledOutput.h:70
SerialWombatChip
Class for a Serial Wombat chip. Each Serial Wombat chip on a project should have its own instance.
Definition SerialWombat.h:315
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:175
SerialWombatPWM_18AB::SerialWombatPWM_18AB
SerialWombatPWM_18AB(SerialWombatChip &serialWombat)
Definition SerialWombatPWM.h:151
SerialWombatPWM_18AB::pin
uint8_t pin()
fulfills a virtual function requirement of SerialWombatAbstractScaledOutput
Definition SerialWombatPWM.h:185
SerialWombatPWM_18AB::swPinModeNumber
uint8_t swPinModeNumber()
fulfills a virtual function requirement of SerialWombatAbstractScaledOutput
Definition SerialWombatPWM.h:193
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:159
SerialWombatPWM_4AB::setFrequency_SW4AB
int16_t setFrequency_SW4AB(Wombat4A_B_PWMFrequencyValues_t frequency)
Definition SerialWombatPWM.h:138
SerialWombatPWM_4AB::SerialWombatPWM_4AB
SerialWombatPWM_4AB(SerialWombatChip &serialWombat)
Definition SerialWombatPWM.h:123
SerialWombatPWM::writeDutyCycle
void writeDutyCycle(uint16_t dutyCycle)
Definition SerialWombatPWM.h:110
SerialWombatPWM::SerialWombatPWM
SerialWombatPWM(SerialWombatChip &serialWombat)
Constructor for SerialWombatPWM class.
Definition SerialWombatPWM.h:87
SerialWombatPWM::begin
int16_t begin(uint8_t pin, uint16_t dutyCycle=0, bool invert=false)
Initialize a pin that has been declared as PWM.
Definition SerialWombatPWM.h:95
SerialWombatPin::writePublicData
uint16_t writePublicData(uint16_t value)
Write a 16 bit value to this pin.
Definition SerialWombatPin.h:113
SerialWombatPin::_sw
SerialWombatChip & _sw
Definition SerialWombatPin.h:214
SerialWombatPin::SerialWombatPin
SerialWombatPin(SerialWombatChip &serialWombatChip)
Instantiates a Serial Wombat Pin.
Definition SerialWombatPin.h:45
SerialWombatPin::initPacketNoResponse
int16_t initPacketNoResponse(uint8_t packetNumber, uint8_t param0=0x55, uint8_t param1=0x55, uint8_t param2=0x55, uint8_t param3=0x55, uint8_t param4=0x55)
Definition SerialWombatPin.h:130
SerialWombatPin::_pin
uint8_t _pin
Definition SerialWombatPin.h:213
SerialWombatPin::pin
uint8_t pin()
Returns the current SW pin number. Used primarily for virtual calls by derived classes.
Definition SerialWombatPin.h:120
SerialWombatPin::_pinMode
uint8_t _pinMode
Definition SerialWombatPin.h:215