All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.dvlopt.linux.i2c.I2CBuffer Maven / Gradle / Ivy

/*
 * Copyright 2018 Adam Helinski
 *
 * Licensed 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 io.dvlopt.linux.i2c ;


import com.sun.jna.Memory           ;
import io.dvlopt.linux.NativeMemory ;




/**
 * Class representing a buffer of bytes for directly reading or writing to a slave device.
 */
public class I2CBuffer {


    final Memory memory ;

    /**
     * The length of this buffer.
     */
    public final int length ;




    /**
     * Allocates a buffer.
     *
     * @param length  The number of bytes.
     */
    public I2CBuffer( int length ) {
    
        this.memory = new Memory( length ) ;
        this.length = length               ;

        this.memory.clear() ;
    }




    /**
     * Retrieves the byte at the given position.
     *
     * @param index  Which byte.
     *
     * @return  An unsigned byte.
     */
    public int get( int index ) {

        return NativeMemory.getUnsignedByte( this.memory ,
                                             index       ) ;
    }




    /**
     * Sets the byte at the given position.
     *
     * @param index  Where.
     *
     * @param b  An unsigned byte.
     *
     * @return  This instance.
     */
    public I2CBuffer set( int index ,
                          int b     ) {

        NativeMemory.setUnsignedByte( this.memory   ,
                                      index         ,
                                      b             ) ;

        return this ;
    }




    /**
     * Fills the buffer with 0.
     *
     * @return  This instance.
     */
    public I2CBuffer clear() {
    
        this.memory.clear() ;

        return this ;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy