MCP4922にADSR波形を出力。

Dependencies:   mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AverageAnalogIn.h Source File

AverageAnalogIn.h

00001 #ifndef _AVERAGE_ANALOG_H_
00002 #define _AVERAGE_ANALOG_H_
00003 
00004 #include "mbed.h"
00005 
00006 class AverageAnalogIn {
00007 public:
00008     AverageAnalogIn(PinName _pin, int _bufferSize=8) : m_AnalogIn(_pin), bufferSize(_bufferSize), index(0) {
00009         buffer = new unsigned short[bufferSize];
00010     }
00011     
00012     ~AverageAnalogIn() {
00013         delete[] buffer;
00014     }
00015     
00016     unsigned short read_u16() {
00017         buffer[index] = m_AnalogIn.read_u16();
00018         index++;
00019         if (index == bufferSize) {
00020             index = 0;
00021         }
00022         unsigned int sum = 0;
00023         for (int i = 0; i < bufferSize; i++) {
00024             sum += buffer[i];
00025         }
00026         return sum / bufferSize;
00027     }
00028     
00029     float read() {
00030         unsigned short value = read_u16();
00031         return (float)value * (1.0f / (float)0xFFFF);
00032     }
00033 
00034 private:
00035     AnalogIn m_AnalogIn;
00036     int bufferSize;
00037     int index;
00038     unsigned short *buffer;
00039 };
00040 
00041 #endif //_AVERAGE_ANALOG_H_