Transistor Gijutsu, October 2014, Special Features Chapter 9, Software of the Function Generator トランジスタ技術2014年10月号 特集第9章のソフトウェア わがまま波形発生器のソフトウェア

Dependencies:   USBDevice mbed

Information

tg_201410s8_AD7714 トランジスタ技術 2014年 10月号 第9章のソフトウェア

Program for Section 9 in October. 2014 issue of the Transistor Gijutsu
(Japanese electronics magazine)

概要

このプログラムは、ソフトウエアDDSにより、任意の波形を出力(2ch)します。 特徴は次のとおりです。

  • PWM出力をDAコンバータとして利用します。
  • 周波数や波形、バースト条件などを個別に設定できる独立した出力を2チャネル持っています。
  • 周波数分解能0.023mHz
  • 周波数範囲0.023mHz~10kHz
  • 各チャネルにそれぞれ、波形の先頭で出力されるトリガ出力があります。
  • 出力波形を関数で定義できます。
  • 休止波数、出力波数、を設定することでバースト波形が出力できます。

ファイル

このソフトウエアは、次のファイルから構成されています。

  • DDS.cpp - DDSによる波形発生
  • main.cpp - main()関数

詳細については、10月号の記事および上記ファイル中のコメントを参照してください。

Committer:
Dance
Date:
Fri Aug 29 08:33:17 2014 +0000
Revision:
0:f1ecca559ec3
Transistor Gijutsu, October 2014, Special Features Chapter 9; ????????2014?10??????9????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Dance 0:f1ecca559ec3 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
Dance 0:f1ecca559ec3 2 *
Dance 0:f1ecca559ec3 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Dance 0:f1ecca559ec3 4 * and associated documentation files (the "Software"), to deal in the Software without
Dance 0:f1ecca559ec3 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Dance 0:f1ecca559ec3 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Dance 0:f1ecca559ec3 7 * Software is furnished to do so, subject to the following conditions:
Dance 0:f1ecca559ec3 8 *
Dance 0:f1ecca559ec3 9 * The above copyright notice and this permission notice shall be included in all copies or
Dance 0:f1ecca559ec3 10 * substantial portions of the Software.
Dance 0:f1ecca559ec3 11 *
Dance 0:f1ecca559ec3 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Dance 0:f1ecca559ec3 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Dance 0:f1ecca559ec3 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Dance 0:f1ecca559ec3 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Dance 0:f1ecca559ec3 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Dance 0:f1ecca559ec3 17 */
Dance 0:f1ecca559ec3 18
Dance 0:f1ecca559ec3 19 #include "stdint.h"
Dance 0:f1ecca559ec3 20 #include "USBMouse.h"
Dance 0:f1ecca559ec3 21
Dance 0:f1ecca559ec3 22 bool USBMouse::update(int16_t x, int16_t y, uint8_t button, int8_t z) {
Dance 0:f1ecca559ec3 23 switch (mouse_type) {
Dance 0:f1ecca559ec3 24 case REL_MOUSE:
Dance 0:f1ecca559ec3 25 while (x > 127) {
Dance 0:f1ecca559ec3 26 if (!mouseSend(127, 0, button, z)) return false;
Dance 0:f1ecca559ec3 27 x = x - 127;
Dance 0:f1ecca559ec3 28 }
Dance 0:f1ecca559ec3 29 while (x < -128) {
Dance 0:f1ecca559ec3 30 if (!mouseSend(-128, 0, button, z)) return false;
Dance 0:f1ecca559ec3 31 x = x + 128;
Dance 0:f1ecca559ec3 32 }
Dance 0:f1ecca559ec3 33 while (y > 127) {
Dance 0:f1ecca559ec3 34 if (!mouseSend(0, 127, button, z)) return false;
Dance 0:f1ecca559ec3 35 y = y - 127;
Dance 0:f1ecca559ec3 36 }
Dance 0:f1ecca559ec3 37 while (y < -128) {
Dance 0:f1ecca559ec3 38 if (!mouseSend(0, -128, button, z)) return false;
Dance 0:f1ecca559ec3 39 y = y + 128;
Dance 0:f1ecca559ec3 40 }
Dance 0:f1ecca559ec3 41 return mouseSend(x, y, button, z);
Dance 0:f1ecca559ec3 42 case ABS_MOUSE:
Dance 0:f1ecca559ec3 43 HID_REPORT report;
Dance 0:f1ecca559ec3 44
Dance 0:f1ecca559ec3 45 report.data[0] = x & 0xff;
Dance 0:f1ecca559ec3 46 report.data[1] = (x >> 8) & 0xff;
Dance 0:f1ecca559ec3 47 report.data[2] = y & 0xff;
Dance 0:f1ecca559ec3 48 report.data[3] = (y >> 8) & 0xff;
Dance 0:f1ecca559ec3 49 report.data[4] = -z;
Dance 0:f1ecca559ec3 50 report.data[5] = button & 0x07;
Dance 0:f1ecca559ec3 51
Dance 0:f1ecca559ec3 52 report.length = 6;
Dance 0:f1ecca559ec3 53
Dance 0:f1ecca559ec3 54 return send(&report);
Dance 0:f1ecca559ec3 55 default:
Dance 0:f1ecca559ec3 56 return false;
Dance 0:f1ecca559ec3 57 }
Dance 0:f1ecca559ec3 58 }
Dance 0:f1ecca559ec3 59
Dance 0:f1ecca559ec3 60 bool USBMouse::mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z) {
Dance 0:f1ecca559ec3 61 HID_REPORT report;
Dance 0:f1ecca559ec3 62 report.data[0] = buttons & 0x07;
Dance 0:f1ecca559ec3 63 report.data[1] = x;
Dance 0:f1ecca559ec3 64 report.data[2] = y;
Dance 0:f1ecca559ec3 65 report.data[3] = -z; // >0 to scroll down, <0 to scroll up
Dance 0:f1ecca559ec3 66
Dance 0:f1ecca559ec3 67 report.length = 4;
Dance 0:f1ecca559ec3 68
Dance 0:f1ecca559ec3 69 return send(&report);
Dance 0:f1ecca559ec3 70 }
Dance 0:f1ecca559ec3 71
Dance 0:f1ecca559ec3 72 bool USBMouse::move(int16_t x, int16_t y) {
Dance 0:f1ecca559ec3 73 return update(x, y, button, 0);
Dance 0:f1ecca559ec3 74 }
Dance 0:f1ecca559ec3 75
Dance 0:f1ecca559ec3 76 bool USBMouse::scroll(int8_t z) {
Dance 0:f1ecca559ec3 77 return update(0, 0, button, z);
Dance 0:f1ecca559ec3 78 }
Dance 0:f1ecca559ec3 79
Dance 0:f1ecca559ec3 80
Dance 0:f1ecca559ec3 81 bool USBMouse::doubleClick() {
Dance 0:f1ecca559ec3 82 if (!click(MOUSE_LEFT))
Dance 0:f1ecca559ec3 83 return false;
Dance 0:f1ecca559ec3 84 wait(0.1);
Dance 0:f1ecca559ec3 85 return click(MOUSE_LEFT);
Dance 0:f1ecca559ec3 86 }
Dance 0:f1ecca559ec3 87
Dance 0:f1ecca559ec3 88 bool USBMouse::click(uint8_t button) {
Dance 0:f1ecca559ec3 89 if (!update(0, 0, button, 0))
Dance 0:f1ecca559ec3 90 return false;
Dance 0:f1ecca559ec3 91 wait(0.01);
Dance 0:f1ecca559ec3 92 return update(0, 0, 0, 0);
Dance 0:f1ecca559ec3 93 }
Dance 0:f1ecca559ec3 94
Dance 0:f1ecca559ec3 95 bool USBMouse::press(uint8_t button_) {
Dance 0:f1ecca559ec3 96 button = button_ & 0x07;
Dance 0:f1ecca559ec3 97 return update(0, 0, button, 0);
Dance 0:f1ecca559ec3 98 }
Dance 0:f1ecca559ec3 99
Dance 0:f1ecca559ec3 100 bool USBMouse::release(uint8_t button_) {
Dance 0:f1ecca559ec3 101 button = (button & (~button_)) & 0x07;
Dance 0:f1ecca559ec3 102 return update(0, 0, button, 0);
Dance 0:f1ecca559ec3 103 }
Dance 0:f1ecca559ec3 104
Dance 0:f1ecca559ec3 105
Dance 0:f1ecca559ec3 106 uint8_t * USBMouse::reportDesc() {
Dance 0:f1ecca559ec3 107
Dance 0:f1ecca559ec3 108 if (mouse_type == REL_MOUSE) {
Dance 0:f1ecca559ec3 109 static uint8_t reportDescriptor[] = {
Dance 0:f1ecca559ec3 110 USAGE_PAGE(1), 0x01, // Genric Desktop
Dance 0:f1ecca559ec3 111 USAGE(1), 0x02, // Mouse
Dance 0:f1ecca559ec3 112 COLLECTION(1), 0x01, // Application
Dance 0:f1ecca559ec3 113 USAGE(1), 0x01, // Pointer
Dance 0:f1ecca559ec3 114 COLLECTION(1), 0x00, // Physical
Dance 0:f1ecca559ec3 115
Dance 0:f1ecca559ec3 116 REPORT_COUNT(1), 0x03,
Dance 0:f1ecca559ec3 117 REPORT_SIZE(1), 0x01,
Dance 0:f1ecca559ec3 118 USAGE_PAGE(1), 0x09, // Buttons
Dance 0:f1ecca559ec3 119 USAGE_MINIMUM(1), 0x1,
Dance 0:f1ecca559ec3 120 USAGE_MAXIMUM(1), 0x3,
Dance 0:f1ecca559ec3 121 LOGICAL_MINIMUM(1), 0x00,
Dance 0:f1ecca559ec3 122 LOGICAL_MAXIMUM(1), 0x01,
Dance 0:f1ecca559ec3 123 INPUT(1), 0x02,
Dance 0:f1ecca559ec3 124 REPORT_COUNT(1), 0x01,
Dance 0:f1ecca559ec3 125 REPORT_SIZE(1), 0x05,
Dance 0:f1ecca559ec3 126 INPUT(1), 0x01,
Dance 0:f1ecca559ec3 127
Dance 0:f1ecca559ec3 128 REPORT_COUNT(1), 0x03,
Dance 0:f1ecca559ec3 129 REPORT_SIZE(1), 0x08,
Dance 0:f1ecca559ec3 130 USAGE_PAGE(1), 0x01,
Dance 0:f1ecca559ec3 131 USAGE(1), 0x30, // X
Dance 0:f1ecca559ec3 132 USAGE(1), 0x31, // Y
Dance 0:f1ecca559ec3 133 USAGE(1), 0x38, // scroll
Dance 0:f1ecca559ec3 134 LOGICAL_MINIMUM(1), 0x81,
Dance 0:f1ecca559ec3 135 LOGICAL_MAXIMUM(1), 0x7f,
Dance 0:f1ecca559ec3 136 INPUT(1), 0x06, // Relative data
Dance 0:f1ecca559ec3 137
Dance 0:f1ecca559ec3 138 END_COLLECTION(0),
Dance 0:f1ecca559ec3 139 END_COLLECTION(0),
Dance 0:f1ecca559ec3 140 };
Dance 0:f1ecca559ec3 141 reportLength = sizeof(reportDescriptor);
Dance 0:f1ecca559ec3 142 return reportDescriptor;
Dance 0:f1ecca559ec3 143 } else if (mouse_type == ABS_MOUSE) {
Dance 0:f1ecca559ec3 144 static uint8_t reportDescriptor[] = {
Dance 0:f1ecca559ec3 145
Dance 0:f1ecca559ec3 146 USAGE_PAGE(1), 0x01, // Generic Desktop
Dance 0:f1ecca559ec3 147 USAGE(1), 0x02, // Mouse
Dance 0:f1ecca559ec3 148 COLLECTION(1), 0x01, // Application
Dance 0:f1ecca559ec3 149 USAGE(1), 0x01, // Pointer
Dance 0:f1ecca559ec3 150 COLLECTION(1), 0x00, // Physical
Dance 0:f1ecca559ec3 151
Dance 0:f1ecca559ec3 152 USAGE_PAGE(1), 0x01, // Generic Desktop
Dance 0:f1ecca559ec3 153 USAGE(1), 0x30, // X
Dance 0:f1ecca559ec3 154 USAGE(1), 0x31, // Y
Dance 0:f1ecca559ec3 155 LOGICAL_MINIMUM(1), 0x00, // 0
Dance 0:f1ecca559ec3 156 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
Dance 0:f1ecca559ec3 157 REPORT_SIZE(1), 0x10,
Dance 0:f1ecca559ec3 158 REPORT_COUNT(1), 0x02,
Dance 0:f1ecca559ec3 159 INPUT(1), 0x02, // Data, Variable, Absolute
Dance 0:f1ecca559ec3 160
Dance 0:f1ecca559ec3 161 USAGE_PAGE(1), 0x01, // Generic Desktop
Dance 0:f1ecca559ec3 162 USAGE(1), 0x38, // scroll
Dance 0:f1ecca559ec3 163 LOGICAL_MINIMUM(1), 0x81, // -127
Dance 0:f1ecca559ec3 164 LOGICAL_MAXIMUM(1), 0x7f, // 127
Dance 0:f1ecca559ec3 165 REPORT_SIZE(1), 0x08,
Dance 0:f1ecca559ec3 166 REPORT_COUNT(1), 0x01,
Dance 0:f1ecca559ec3 167 INPUT(1), 0x06, // Data, Variable, Relative
Dance 0:f1ecca559ec3 168
Dance 0:f1ecca559ec3 169 USAGE_PAGE(1), 0x09, // Buttons
Dance 0:f1ecca559ec3 170 USAGE_MINIMUM(1), 0x01,
Dance 0:f1ecca559ec3 171 USAGE_MAXIMUM(1), 0x03,
Dance 0:f1ecca559ec3 172 LOGICAL_MINIMUM(1), 0x00, // 0
Dance 0:f1ecca559ec3 173 LOGICAL_MAXIMUM(1), 0x01, // 1
Dance 0:f1ecca559ec3 174 REPORT_COUNT(1), 0x03,
Dance 0:f1ecca559ec3 175 REPORT_SIZE(1), 0x01,
Dance 0:f1ecca559ec3 176 INPUT(1), 0x02, // Data, Variable, Absolute
Dance 0:f1ecca559ec3 177 REPORT_COUNT(1), 0x01,
Dance 0:f1ecca559ec3 178 REPORT_SIZE(1), 0x05,
Dance 0:f1ecca559ec3 179 INPUT(1), 0x01, // Constant
Dance 0:f1ecca559ec3 180
Dance 0:f1ecca559ec3 181 END_COLLECTION(0),
Dance 0:f1ecca559ec3 182 END_COLLECTION(0)
Dance 0:f1ecca559ec3 183 };
Dance 0:f1ecca559ec3 184 reportLength = sizeof(reportDescriptor);
Dance 0:f1ecca559ec3 185 return reportDescriptor;
Dance 0:f1ecca559ec3 186 }
Dance 0:f1ecca559ec3 187 return NULL;
Dance 0:f1ecca559ec3 188 }
Dance 0:f1ecca559ec3 189
Dance 0:f1ecca559ec3 190 #define DEFAULT_CONFIGURATION (1)
Dance 0:f1ecca559ec3 191 #define TOTAL_DESCRIPTOR_LENGTH ((1 * CONFIGURATION_DESCRIPTOR_LENGTH) \
Dance 0:f1ecca559ec3 192 + (1 * INTERFACE_DESCRIPTOR_LENGTH) \
Dance 0:f1ecca559ec3 193 + (1 * HID_DESCRIPTOR_LENGTH) \
Dance 0:f1ecca559ec3 194 + (2 * ENDPOINT_DESCRIPTOR_LENGTH))
Dance 0:f1ecca559ec3 195
Dance 0:f1ecca559ec3 196 uint8_t * USBMouse::configurationDesc() {
Dance 0:f1ecca559ec3 197 static uint8_t configurationDescriptor[] = {
Dance 0:f1ecca559ec3 198 CONFIGURATION_DESCRIPTOR_LENGTH,// bLength
Dance 0:f1ecca559ec3 199 CONFIGURATION_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 200 LSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (LSB)
Dance 0:f1ecca559ec3 201 MSB(TOTAL_DESCRIPTOR_LENGTH), // wTotalLength (MSB)
Dance 0:f1ecca559ec3 202 0x01, // bNumInterfaces
Dance 0:f1ecca559ec3 203 DEFAULT_CONFIGURATION, // bConfigurationValue
Dance 0:f1ecca559ec3 204 0x00, // iConfiguration
Dance 0:f1ecca559ec3 205 C_RESERVED | C_SELF_POWERED, // bmAttributes
Dance 0:f1ecca559ec3 206 C_POWER(0), // bMaxPowerHello World from Mbed
Dance 0:f1ecca559ec3 207
Dance 0:f1ecca559ec3 208 INTERFACE_DESCRIPTOR_LENGTH, // bLength
Dance 0:f1ecca559ec3 209 INTERFACE_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 210 0x00, // bInterfaceNumber
Dance 0:f1ecca559ec3 211 0x00, // bAlternateSetting
Dance 0:f1ecca559ec3 212 0x02, // bNumEndpoints
Dance 0:f1ecca559ec3 213 HID_CLASS, // bInterfaceClass
Dance 0:f1ecca559ec3 214 1, // bInterfaceSubClass
Dance 0:f1ecca559ec3 215 2, // bInterfaceProtocol (mouse)
Dance 0:f1ecca559ec3 216 0x00, // iInterface
Dance 0:f1ecca559ec3 217
Dance 0:f1ecca559ec3 218 HID_DESCRIPTOR_LENGTH, // bLength
Dance 0:f1ecca559ec3 219 HID_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 220 LSB(HID_VERSION_1_11), // bcdHID (LSB)
Dance 0:f1ecca559ec3 221 MSB(HID_VERSION_1_11), // bcdHID (MSB)
Dance 0:f1ecca559ec3 222 0x00, // bCountryCode
Dance 0:f1ecca559ec3 223 0x01, // bNumDescriptors
Dance 0:f1ecca559ec3 224 REPORT_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 225 (uint8_t)(LSB(reportDescLength())), // wDescriptorLength (LSB)
Dance 0:f1ecca559ec3 226 (uint8_t)(MSB(reportDescLength())), // wDescriptorLength (MSB)
Dance 0:f1ecca559ec3 227
Dance 0:f1ecca559ec3 228 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Dance 0:f1ecca559ec3 229 ENDPOINT_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 230 PHY_TO_DESC(EPINT_IN), // bEndpointAddress
Dance 0:f1ecca559ec3 231 E_INTERRUPT, // bmAttributes
Dance 0:f1ecca559ec3 232 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Dance 0:f1ecca559ec3 233 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Dance 0:f1ecca559ec3 234 1, // bInterval (milliseconds)
Dance 0:f1ecca559ec3 235
Dance 0:f1ecca559ec3 236 ENDPOINT_DESCRIPTOR_LENGTH, // bLength
Dance 0:f1ecca559ec3 237 ENDPOINT_DESCRIPTOR, // bDescriptorType
Dance 0:f1ecca559ec3 238 PHY_TO_DESC(EPINT_OUT), // bEndpointAddress
Dance 0:f1ecca559ec3 239 E_INTERRUPT, // bmAttributes
Dance 0:f1ecca559ec3 240 LSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (LSB)
Dance 0:f1ecca559ec3 241 MSB(MAX_PACKET_SIZE_EPINT), // wMaxPacketSize (MSB)
Dance 0:f1ecca559ec3 242 1, // bInterval (milliseconds)
Dance 0:f1ecca559ec3 243 };
Dance 0:f1ecca559ec3 244 return configurationDescriptor;
Dance 0:f1ecca559ec3 245 }