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 #ifndef USBMOUSE_H
Dance 0:f1ecca559ec3 20 #define USBMOUSE_H
Dance 0:f1ecca559ec3 21
Dance 0:f1ecca559ec3 22 #include "USBHID.h"
Dance 0:f1ecca559ec3 23
Dance 0:f1ecca559ec3 24 #define REPORT_ID_MOUSE 2
Dance 0:f1ecca559ec3 25
Dance 0:f1ecca559ec3 26 /* Common usage */
Dance 0:f1ecca559ec3 27
Dance 0:f1ecca559ec3 28 enum MOUSE_BUTTON
Dance 0:f1ecca559ec3 29 {
Dance 0:f1ecca559ec3 30 MOUSE_LEFT = 1,
Dance 0:f1ecca559ec3 31 MOUSE_RIGHT = 2,
Dance 0:f1ecca559ec3 32 MOUSE_MIDDLE = 4,
Dance 0:f1ecca559ec3 33 };
Dance 0:f1ecca559ec3 34
Dance 0:f1ecca559ec3 35 /* X and Y limits */
Dance 0:f1ecca559ec3 36 /* These values do not directly map to screen pixels */
Dance 0:f1ecca559ec3 37 /* Zero may be interpreted as meaning 'no movement' */
Dance 0:f1ecca559ec3 38 #define X_MIN_ABS (1) /*!< Minimum value on x-axis */
Dance 0:f1ecca559ec3 39 #define Y_MIN_ABS (1) /*!< Minimum value on y-axis */
Dance 0:f1ecca559ec3 40 #define X_MAX_ABS (0x7fff) /*!< Maximum value on x-axis */
Dance 0:f1ecca559ec3 41 #define Y_MAX_ABS (0x7fff) /*!< Maximum value on y-axis */
Dance 0:f1ecca559ec3 42
Dance 0:f1ecca559ec3 43 #define X_MIN_REL (-127) /*!< The maximum value that we can move to the left on the x-axis */
Dance 0:f1ecca559ec3 44 #define Y_MIN_REL (-127) /*!< The maximum value that we can move up on the y-axis */
Dance 0:f1ecca559ec3 45 #define X_MAX_REL (127) /*!< The maximum value that we can move to the right on the x-axis */
Dance 0:f1ecca559ec3 46 #define Y_MAX_REL (127) /*!< The maximum value that we can move down on the y-axis */
Dance 0:f1ecca559ec3 47
Dance 0:f1ecca559ec3 48 enum MOUSE_TYPE
Dance 0:f1ecca559ec3 49 {
Dance 0:f1ecca559ec3 50 ABS_MOUSE,
Dance 0:f1ecca559ec3 51 REL_MOUSE,
Dance 0:f1ecca559ec3 52 };
Dance 0:f1ecca559ec3 53
Dance 0:f1ecca559ec3 54 /**
Dance 0:f1ecca559ec3 55 *
Dance 0:f1ecca559ec3 56 * USBMouse example
Dance 0:f1ecca559ec3 57 * @code
Dance 0:f1ecca559ec3 58 * #include "mbed.h"
Dance 0:f1ecca559ec3 59 * #include "USBMouse.h"
Dance 0:f1ecca559ec3 60 *
Dance 0:f1ecca559ec3 61 * USBMouse mouse;
Dance 0:f1ecca559ec3 62 *
Dance 0:f1ecca559ec3 63 * int main(void)
Dance 0:f1ecca559ec3 64 * {
Dance 0:f1ecca559ec3 65 * while (1)
Dance 0:f1ecca559ec3 66 * {
Dance 0:f1ecca559ec3 67 * mouse.move(20, 0);
Dance 0:f1ecca559ec3 68 * wait(0.5);
Dance 0:f1ecca559ec3 69 * }
Dance 0:f1ecca559ec3 70 * }
Dance 0:f1ecca559ec3 71 *
Dance 0:f1ecca559ec3 72 * @endcode
Dance 0:f1ecca559ec3 73 *
Dance 0:f1ecca559ec3 74 *
Dance 0:f1ecca559ec3 75 * @code
Dance 0:f1ecca559ec3 76 * #include "mbed.h"
Dance 0:f1ecca559ec3 77 * #include "USBMouse.h"
Dance 0:f1ecca559ec3 78 * #include <math.h>
Dance 0:f1ecca559ec3 79 *
Dance 0:f1ecca559ec3 80 * USBMouse mouse(ABS_MOUSE);
Dance 0:f1ecca559ec3 81 *
Dance 0:f1ecca559ec3 82 * int main(void)
Dance 0:f1ecca559ec3 83 * {
Dance 0:f1ecca559ec3 84 * uint16_t x_center = (X_MAX_ABS - X_MIN_ABS)/2;
Dance 0:f1ecca559ec3 85 * uint16_t y_center = (Y_MAX_ABS - Y_MIN_ABS)/2;
Dance 0:f1ecca559ec3 86 * uint16_t x_screen = 0;
Dance 0:f1ecca559ec3 87 * uint16_t y_screen = 0;
Dance 0:f1ecca559ec3 88 *
Dance 0:f1ecca559ec3 89 * uint32_t x_origin = x_center;
Dance 0:f1ecca559ec3 90 * uint32_t y_origin = y_center;
Dance 0:f1ecca559ec3 91 * uint32_t radius = 5000;
Dance 0:f1ecca559ec3 92 * uint32_t angle = 0;
Dance 0:f1ecca559ec3 93 *
Dance 0:f1ecca559ec3 94 * while (1)
Dance 0:f1ecca559ec3 95 * {
Dance 0:f1ecca559ec3 96 * x_screen = x_origin + cos((double)angle*3.14/180.0)*radius;
Dance 0:f1ecca559ec3 97 * y_screen = y_origin + sin((double)angle*3.14/180.0)*radius;
Dance 0:f1ecca559ec3 98 *
Dance 0:f1ecca559ec3 99 * mouse.move(x_screen, y_screen);
Dance 0:f1ecca559ec3 100 * angle += 3;
Dance 0:f1ecca559ec3 101 * wait(0.01);
Dance 0:f1ecca559ec3 102 * }
Dance 0:f1ecca559ec3 103 * }
Dance 0:f1ecca559ec3 104 *
Dance 0:f1ecca559ec3 105 * @endcode
Dance 0:f1ecca559ec3 106 */
Dance 0:f1ecca559ec3 107 class USBMouse: public USBHID
Dance 0:f1ecca559ec3 108 {
Dance 0:f1ecca559ec3 109 public:
Dance 0:f1ecca559ec3 110
Dance 0:f1ecca559ec3 111 /**
Dance 0:f1ecca559ec3 112 * Constructor
Dance 0:f1ecca559ec3 113 *
Dance 0:f1ecca559ec3 114 * @param mouse_type Mouse type: ABS_MOUSE (absolute mouse) or REL_MOUSE (relative mouse) (default: REL_MOUSE)
Dance 0:f1ecca559ec3 115 * @param vendor_id Your vendor_id (default: 0x1234)
Dance 0:f1ecca559ec3 116 * @param product_id Your product_id (default: 0x0001)
Dance 0:f1ecca559ec3 117 * @param product_release Your preoduct_release (default: 0x0001)
Dance 0:f1ecca559ec3 118 *
Dance 0:f1ecca559ec3 119 */
Dance 0:f1ecca559ec3 120 USBMouse(MOUSE_TYPE mouse_type = REL_MOUSE, uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0001, uint16_t product_release = 0x0001):
Dance 0:f1ecca559ec3 121 USBHID(0, 0, vendor_id, product_id, product_release, false)
Dance 0:f1ecca559ec3 122 {
Dance 0:f1ecca559ec3 123 button = 0;
Dance 0:f1ecca559ec3 124 this->mouse_type = mouse_type;
Dance 0:f1ecca559ec3 125 connect();
Dance 0:f1ecca559ec3 126 };
Dance 0:f1ecca559ec3 127
Dance 0:f1ecca559ec3 128 /**
Dance 0:f1ecca559ec3 129 * Write a state of the mouse
Dance 0:f1ecca559ec3 130 *
Dance 0:f1ecca559ec3 131 * @param x x-axis position
Dance 0:f1ecca559ec3 132 * @param y y-axis position
Dance 0:f1ecca559ec3 133 * @param buttons buttons state (first bit represents MOUSE_LEFT, second bit MOUSE_RIGHT and third bit MOUSE_MIDDLE)
Dance 0:f1ecca559ec3 134 * @param z wheel state (>0 to scroll down, <0 to scroll up)
Dance 0:f1ecca559ec3 135 * @returns true if there is no error, false otherwise
Dance 0:f1ecca559ec3 136 */
Dance 0:f1ecca559ec3 137 bool update(int16_t x, int16_t y, uint8_t buttons, int8_t z);
Dance 0:f1ecca559ec3 138
Dance 0:f1ecca559ec3 139
Dance 0:f1ecca559ec3 140 /**
Dance 0:f1ecca559ec3 141 * Move the cursor to (x, y)
Dance 0:f1ecca559ec3 142 *
Dance 0:f1ecca559ec3 143 * @param x-axis position
Dance 0:f1ecca559ec3 144 * @param y-axis position
Dance 0:f1ecca559ec3 145 * @returns true if there is no error, false otherwise
Dance 0:f1ecca559ec3 146 */
Dance 0:f1ecca559ec3 147 bool move(int16_t x, int16_t y);
Dance 0:f1ecca559ec3 148
Dance 0:f1ecca559ec3 149 /**
Dance 0:f1ecca559ec3 150 * Press one or several buttons
Dance 0:f1ecca559ec3 151 *
Dance 0:f1ecca559ec3 152 * @param button button state (ex: press(MOUSE_LEFT))
Dance 0:f1ecca559ec3 153 * @returns true if there is no error, false otherwise
Dance 0:f1ecca559ec3 154 */
Dance 0:f1ecca559ec3 155 bool press(uint8_t button);
Dance 0:f1ecca559ec3 156
Dance 0:f1ecca559ec3 157 /**
Dance 0:f1ecca559ec3 158 * Release one or several buttons
Dance 0:f1ecca559ec3 159 *
Dance 0:f1ecca559ec3 160 * @param button button state (ex: release(MOUSE_LEFT))
Dance 0:f1ecca559ec3 161 * @returns true if there is no error, false otherwise
Dance 0:f1ecca559ec3 162 */
Dance 0:f1ecca559ec3 163 bool release(uint8_t button);
Dance 0:f1ecca559ec3 164
Dance 0:f1ecca559ec3 165 /**
Dance 0:f1ecca559ec3 166 * Double click (MOUSE_LEFT)
Dance 0:f1ecca559ec3 167 *
Dance 0:f1ecca559ec3 168 * @returns true if there is no error, false otherwise
Dance 0:f1ecca559ec3 169 */
Dance 0:f1ecca559ec3 170 bool doubleClick();
Dance 0:f1ecca559ec3 171
Dance 0:f1ecca559ec3 172 /**
Dance 0:f1ecca559ec3 173 * Click
Dance 0:f1ecca559ec3 174 *
Dance 0:f1ecca559ec3 175 * @param button state of the buttons ( ex: clic(MOUSE_LEFT))
Dance 0:f1ecca559ec3 176 * @returns true if there is no error, false otherwise
Dance 0:f1ecca559ec3 177 */
Dance 0:f1ecca559ec3 178 bool click(uint8_t button);
Dance 0:f1ecca559ec3 179
Dance 0:f1ecca559ec3 180 /**
Dance 0:f1ecca559ec3 181 * Scrolling
Dance 0:f1ecca559ec3 182 *
Dance 0:f1ecca559ec3 183 * @param z value of the wheel (>0 to go down, <0 to go up)
Dance 0:f1ecca559ec3 184 * @returns true if there is no error, false otherwise
Dance 0:f1ecca559ec3 185 */
Dance 0:f1ecca559ec3 186 bool scroll(int8_t z);
Dance 0:f1ecca559ec3 187
Dance 0:f1ecca559ec3 188 /*
Dance 0:f1ecca559ec3 189 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
Dance 0:f1ecca559ec3 190 *
Dance 0:f1ecca559ec3 191 * @returns pointer to the report descriptor
Dance 0:f1ecca559ec3 192 */
Dance 0:f1ecca559ec3 193 virtual uint8_t * reportDesc();
Dance 0:f1ecca559ec3 194
Dance 0:f1ecca559ec3 195 protected:
Dance 0:f1ecca559ec3 196 /*
Dance 0:f1ecca559ec3 197 * Get configuration descriptor
Dance 0:f1ecca559ec3 198 *
Dance 0:f1ecca559ec3 199 * @returns pointer to the configuration descriptor
Dance 0:f1ecca559ec3 200 */
Dance 0:f1ecca559ec3 201 virtual uint8_t * configurationDesc();
Dance 0:f1ecca559ec3 202
Dance 0:f1ecca559ec3 203 private:
Dance 0:f1ecca559ec3 204 MOUSE_TYPE mouse_type;
Dance 0:f1ecca559ec3 205 uint8_t button;
Dance 0:f1ecca559ec3 206 bool mouseSend(int8_t x, int8_t y, uint8_t buttons, int8_t z);
Dance 0:f1ecca559ec3 207 };
Dance 0:f1ecca559ec3 208
Dance 0:f1ecca559ec3 209 #endif