java doublebuffer_ByteBufferAsDoubleBuffer.java

这个Java程序展示了如何将一个ByteBuffer包装为一个DoubleBuffer,用于处理双精度浮点数。ByteBufferAsDoubleBuffer类继承了DoubleBuffer,并提供了如asReadOnlyBuffer、compact、duplicate等方法。它维护了自己的位置和限制,不直接依赖于底层的ByteBuffer。程序中包含了各种读写操作,确保数据的正确传输。

/* Licensed to the Apache Software Foundation (ASF) under one or more

* contributor license agreements. See the NOTICE file distributed with

* this work for additional information regarding copyright ownership.

* The ASF licenses this file to You under the Apache License, Version 2.0

* (the "License"); you may not use this file except in compliance with

* the License. You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package java.nio;

import libcore.io.SizeOf;

/**

* This class wraps a byte buffer to be a double buffer.

*

* Implementation notice:

**

After a byte buffer instance is wrapped, it becomes privately owned by

* the adapter. It must NOT be accessed outside the adapter any more.

*

The byte buffer's position and limit are NOT linked with the adapter.

* The adapter extends Buffer, thus has its own position and limit.

*

*

*

*/

final class ByteBufferAsDoubleBuffer extends DoubleBuffer {

private final ByteBuffer byteBuffer;

static DoubleBuffer asDoubleBuffer(ByteBuffer byteBuffer) {

ByteBuffer slice = byteBuffer.slice();

slice.order(byteBuffer.order());

return new ByteBufferAsDoubleBuffer(slice);

}

private ByteBufferAsDoubleBuffer(ByteBuffer byteBuffer) {

super(byteBuffer.capacity() / SizeOf.DOUBLE, byteBuffer.effectiveDirectAddress);

this.byteBuffer = byteBuffer;

this.byteBuffer.clear();

}

@Override

public DoubleBuffer asReadOnlyBuffer() {

ByteBufferAsDoubleBuffer buf = new ByteBufferAsDoubleBuffer(byteBuffer.asReadOnlyBuffer());

buf.limit = limit;

buf.position = position;

buf.mark = mark;

buf.byteBuffer.order = byteBuffer.order;

return buf;

}

@Override

public DoubleBuffer compact() {

if (byteBuffer.isReadOnly()) {

throw new ReadOnlyBufferException();

}

byteBuffer.limit(limit * SizeOf.DOUBLE);

byteBuffer.position(position * SizeOf.DOUBLE);

byteBuffer.compact();

byteBuffer.clear();

position = limit - position;

limit = capacity;

mark = UNSET_MARK;

return this;

}

@Override

public DoubleBuffer duplicate() {

ByteBuffer bb = byteBuffer.duplicate().order(byteBuffer.order());

ByteBufferAsDoubleBuffer buf = new ByteBufferAsDoubleBuffer(bb);

buf.limit = limit;

buf.position = position;

buf.mark = mark;

return buf;

}

@Override

public double get() {

if (position == limit) {

throw new BufferUnderflowException();

}

return byteBuffer.getDouble(position++ * SizeOf.DOUBLE);

}

@Override

public double get(int index) {

checkIndex(index);

return byteBuffer.getDouble(index * SizeOf.DOUBLE);

}

@Override

public DoubleBuffer get(double[] dst, int dstOffset, int doubleCount) {

byteBuffer.limit(limit * SizeOf.DOUBLE);

byteBuffer.position(position * SizeOf.DOUBLE);

if (byteBuffer instanceof DirectByteBuffer) {

((DirectByteBuffer) byteBuffer).get(dst, dstOffset, doubleCount);

} else {

((ByteArrayBuffer) byteBuffer).get(dst, dstOffset, doubleCount);

}

this.position += doubleCount;

return this;

}

@Override

public boolean isDirect() {

return byteBuffer.isDirect();

}

@Override

public boolean isReadOnly() {

return byteBuffer.isReadOnly();

}

@Override

public ByteOrder order() {

return byteBuffer.order();

}

@Override double[] protectedArray() {

throw new UnsupportedOperationException();

}

@Override int protectedArrayOffset() {

throw new UnsupportedOperationException();

}

@Override boolean protectedHasArray() {

return false;

}

@Override

public DoubleBuffer put(double c) {

if (position == limit) {

throw new BufferOverflowException();

}

byteBuffer.putDouble(position++ * SizeOf.DOUBLE, c);

return this;

}

@Override

public DoubleBuffer put(int index, double c) {

checkIndex(index);

byteBuffer.putDouble(index * SizeOf.DOUBLE, c);

return this;

}

@Override

public DoubleBuffer put(double[] src, int srcOffset, int doubleCount) {

byteBuffer.limit(limit * SizeOf.DOUBLE);

byteBuffer.position(position * SizeOf.DOUBLE);

if (byteBuffer instanceof DirectByteBuffer) {

((DirectByteBuffer) byteBuffer).put(src, srcOffset, doubleCount);

} else {

((ByteArrayBuffer) byteBuffer).put(src, srcOffset, doubleCount);

}

this.position += doubleCount;

return this;

}

@Override

public DoubleBuffer slice() {

byteBuffer.limit(limit * SizeOf.DOUBLE);

byteBuffer.position(position * SizeOf.DOUBLE);

ByteBuffer bb = byteBuffer.slice().order(byteBuffer.order());

DoubleBuffer result = new ByteBufferAsDoubleBuffer(bb);

byteBuffer.clear();

return result;

}

}

Java程序

|

179行

|

5.39 KB

/* Licensed to the Apache Software Foundation (ASF) under one or more

* contributor license agreements. See the NOTICE file distributed with

* this work for additional information regarding copyright ownership.

* The ASF licenses this file to You under the Apache License, Version 2.0

* (the "License"); you may not use this file except in compliance with

* the License. You may obtain a copy of the License at

*

* http://www.apache.org/licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS,

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package java.nio;

import libcore.io.SizeOf;

/**

* This class wraps a byte buffer to be a double buffer.

*

* Implementation notice:

*

*

After a byte buffer instance is wrapped, it becomes privately owned by

* the adapter. It must NOT be accessed outside the adapter any more.

*

The byte buffer's position and limit are NOT linked with the adapter.

* The adapter extends Buffer, thus has its own position and limit.

*

*

*

*/

final class ByteBufferAsDoubleBuffer extends DoubleBuffer {

private final ByteBuffer byteBuffer;

static DoubleBuffer asDoubleBuffer(ByteBuffer byteBuffer) {

ByteBuffer slice = byteBuffer.slice();

slice.order(byteBuffer.order());

return new ByteBufferAsDoubleBuffer(slice);

}

private ByteBufferAsDoubleBuffer(ByteBuffer byteBuffer) {

super(byteBuffer.capacity() / SizeOf.DOUBLE, byteBuffer.effectiveDirectAddress);

this.byteBuffer = byteBuffer;

this.byteBuffer.clear();

}

@Override

public DoubleBuffer asReadOnlyBuffer() {

ByteBufferAsDoubleBuffer buf = new ByteBufferAsDoubleBuffer(byteBuffer.asReadOnlyBuffer());

buf.limit = limit;

buf.position = position;

buf.mark = mark;

buf.byteBuffer.order = byteBuffer.order;

return buf;

}

@Override

public DoubleBuffer compact() {

if (byteBuffer.isReadOnly()) {

throw new ReadOnlyBufferException();

}

byteBuffer.limit(limit * SizeOf.DOUBLE);

byteBuffer.position(position * SizeOf.DOUBLE);

byteBuffer.compact();

byteBuffer.clear();

position = limit - position;

limit = capacity;

mark = UNSET_MARK;

return this;

}

@Override

public DoubleBuffer duplicate() {

ByteBuffer bb = byteBuffer.duplicate().order(byteBuffer.order());

ByteBufferAsDoubleBuffer buf = new ByteBufferAsDoubleBuffer(bb);

buf.limit = limit;

buf.position = position;

buf.mark = mark;

return buf;

}

@Override

public double get() {

if (position == limit) {

throw new BufferUnderflowException();

}

return byteBuffer.getDouble(position++ * SizeOf.DOUBLE);

}

@Override

public double get(int index) {

checkIndex(index);

return byteBuffer.getDouble(index * SizeOf.DOUBLE);

}

@Override

public DoubleBuffer get(double[] dst, int dstOffset, int doubleCount) {

byteBuffer.limit(limit * SizeOf.DOUBLE);

byteBuffer.position(position * SizeOf.DOUBLE);

if (byteBuffer instanceof DirectByteBuffer) {

((DirectByteBuffer) byteBuffer).get(dst, dstOffset, doubleCount);

} else {

((ByteArrayBuffer) byteBuffer).get(dst, dstOffset, doubleCount);

}

this.position += doubleCount;

return this;

}

@Override

public boolean isDirect() {

return byteBuffer.isDirect();

}

@Override

public boolean isReadOnly() {

return byteBuffer.isReadOnly();

}

@Override

public ByteOrder order() {

return byteBuffer.order();

}

@Override double[] protectedArray() {

throw new UnsupportedOperationException();

}

@Override int protectedArrayOffset() {

throw new UnsupportedOperationException();

}

@Override boolean protectedHasArray() {

return false;

}

@Override

public DoubleBuffer put(double c) {

if (position == limit) {

throw new BufferOverflowException();

}

byteBuffer.putDouble(position++ * SizeOf.DOUBLE, c);

return this;

}

@Override

public DoubleBuffer put(int index, double c) {

checkIndex(index);

byteBuffer.putDouble(index * SizeOf.DOUBLE, c);

return this;

}

@Override

public DoubleBuffer put(double[] src, int srcOffset, int doubleCount) {

byteBuffer.limit(limit * SizeOf.DOUBLE);

byteBuffer.position(position * SizeOf.DOUBLE);

if (byteBuffer instanceof DirectByteBuffer) {

((DirectByteBuffer) byteBuffer).put(src, srcOffset, doubleCount);

} else {

((ByteArrayBuffer) byteBuffer).put(src, srcOffset, doubleCount);

}

this.position += doubleCount;

return this;

}

@Override

public DoubleBuffer slice() {

byteBuffer.limit(limit * SizeOf.DOUBLE);

byteBuffer.position(position * SizeOf.DOUBLE);

ByteBuffer bb = byteBuffer.slice().order(byteBuffer.order());

DoubleBuffer result = new ByteBufferAsDoubleBuffer(bb);

byteBuffer.clear();

return result;

}

}

如果我要改成单通道+dma传输,怎么改#include “./adc/bsp_adc.h” #include “bsp_GeneralTim.h” #include “stm32f4xx_adc.h” __IO uint16_t ADC_ConvertedValue[1]={0}; volatile uint8_t adc_buffer_ready = 0; extern volatile uint32_t time; static void ADC_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /=通道1==/ // 使能 GPIO 时钟 RCC_AHB1PeriphClockCmd(ADC_GPIO_CLK1,ENABLE); // 配置 IO GPIO_InitStructure.GPIO_Pin = ADC_GPIO_PIN1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //不上拉不下拉 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(ADC_GPIO_PORT1, &GPIO_InitStructure); } void ADC_DMA_Config(void) { DMA_InitTypeDef DMA_InitStructure; // 1. 使能 DMA 时钟 RCC_AHB1PeriphClockCmd(ADC_DMA_CLK, ENABLE); // 2. 配置 DMA 参数 DMA_InitStructure.DMA_Channel = ADC_DMA_CHANNEL; // DMA 通道 0 DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ADC_ConvertedValue; // 目标地址:内存缓冲区 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; // 修正外设地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; // 外设到内存 DMA_InitStructure.DMA_BufferSize = 1; // 缓冲区大小 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 外设地址不递增 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; // 单通道禁用地址递增 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; // 外设数据大小:半字(16位) DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // 内存数据大小:半字(16位) DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; // 循环模式 DMA_InitStructure.DMA_Priority = DMA_Priority_High; // 高优先级 DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; // 禁用 FIFO 模式 DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; // FIFO 阈值 DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; // 内存突发传输:单次 DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; // 外设突发传输:单次 // 3. 初始化 DMA DMA_Init(ADC_DMA_STREAM, &DMA_InitStructure); // 4. 使能 DMA 中断(传输完成、传输错误) DMA_ITConfig(ADC_DMA_STREAM, DMA_IT_TC | DMA_IT_TE | DMA_IT_HT, ENABLE); // 5. 使能 DMA 流 DMA_Cmd(ADC_DMA_STREAM, ENABLE); } void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; // 1. 使能 ADC 时钟 RCC_APB2PeriphClockCmd(ADC_CLK, ENABLE); // 2. 配置 ADC 通用参数 ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; // 独立模式 ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; // ADC 时钟分频:PCLK2/4 ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1 ; // DMA 访问模式 ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; // 采样延迟 ADC_CommonInit(&ADC_CommonInitStructure); // 3. 配置 ADC 参数 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; // 12位分辨率 ADC_InitStructure.ADC_ScanConvMode = DISABLE; // 扫描模式使能 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; // 连续转换模式 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; // 软件触发 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; // 任意值 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; // 数据右对齐 ADC_InitStructure.ADC_NbrOfConversion = 1 ; // 转换通道数 ADC_Init(ADC_, &ADC_InitStructure); // 4. 配置 ADC 通道(通道4,PA4) ADC_RegularChannelConfig(ADC_, ADC_Channel_4, 1, ADC_SampleTime_84Cycles); // 5. 使能 ADC DMA ADC_DMACmd(ADC_, ENABLE); // 6. 使能 ADC ADC_Cmd(ADC_, ENABLE); ADC_DMARequestAfterLastTransferCmd(ADC_,ENABLE);// 在单ADC模式下,启用或禁用最后一次传输后的ADC DMA请求 // 7. 启动 ADC 转换 ADC_SoftwareStartConv(ADC_); } static void ADC_NVIC_Config(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void Adc_Init(void) { ADC_GPIO_Config(); ADC_DMA_Config(); ADC_Config(); ADC_NVIC_Config(); } float ADC_GetVoltage(void) { uint32_t sum = 0; // 等待新数据就绪 while(!adc_buffer_ready); // 计算缓冲区平均值 for(int i = 0; i < ADC_BUFFER_SIZE; i++) { sum += ADC_ConvertedValue[i]; } adc_buffer_ready = 0; return (sum * 3.3f) / (ADC_BUFFER_SIZE * 4096.0f); } //float ADC_ReadVoltage(void) // { // // static uint8_t initialized = 0; // if(!initialized) // { // Adc_Init(); // initialized = 1; // } // // // // 直接读取全局数组 // return ADC_ConvertedValue[0] * 3.3f /4096; //}#ifndef __BSP_ADC_H #define __BSP_ADC_H #include "stm32f4xx.h" #define ADC_BUFFER_SIZE 32 // 增加缓冲区大小 #define ADC_CHANNELS 1 /*=====================通道1 IO======================*/ // ADC IO宏定义 #define ADC_GPIO_PORT1 GPIOA #define ADC_GPIO_PIN1 GPIO_Pin_4 #define ADC_GPIO_CLK1 RCC_AHB1Periph_GPIOA #define ADC_CHANNEL1 ADC_Channel_4 // ADC 序号宏定义 #define ADC_ ADC1 #define ADC_CLK RCC_APB2Periph_ADC1 // ADC DR寄存器宏定义,ADC转换后的数字值则存放在这里 #define RHEOSTAT_ADC_DR_ADDR ((u32)ADC1+0x4c) // ADC DMA 通道宏定义,这里我们使用DMA传输 // DMA 配置 #define ADC_DMA_CLK RCC_AHB1Periph_DMA2 #define ADC_DMA_CHANNEL DMA_Channel_0 #define ADC_DMA_STREAM DMA2_Stream0 void Adc_Init(void); float ADC_GetVoltage(void); //float ADC_ReadVoltage(void); #endif /* __BSP_ADC_H */ buck稳压
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值