mock: added AIO pin and logic
Signed-off-by: Alex Tereschenko <alext.mkrs@gmail.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
917a1bd371
commit
63b244cfc3
12
docs/mock.md
12
docs/mock.md
@@ -12,17 +12,17 @@ Board configuration
|
|||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
This feature is yet in the experimental mode and not all functionality is available.
|
This feature is yet in the experimental mode and not all functionality is available.
|
||||||
Right now we simulate a single generic board with only GPIO (without ISR) working.
|
Right now we simulate a single generic board with GPIO (without ISR) and
|
||||||
It also reports having an ADC with 10 (std)/12 (max) bit resolution, but the ADC
|
an ADC with 10 (std)/12 (max) bit resolution, which returns random values on read.
|
||||||
functionality itself is not yet implemented.
|
|
||||||
|
|
||||||
We plan to develop it further and all [contributions](../CONTRIBUTING.md) are more than welcome.
|
We plan to develop it further and all [contributions](../CONTRIBUTING.md) are more than welcome.
|
||||||
|
|
||||||
See the table below for pin layout and features
|
See the table below for pin layout and features
|
||||||
|
|
||||||
| MRAA Number | Pin Name | Notes |
|
| MRAA Number | Pin Name | Notes |
|
||||||
|-------------|----------|-----------------------------|
|
|-------------|----------|---------------------------------------|
|
||||||
| 0 | GPIO0 | GPIO pin, no muxing, no ISR |
|
| 0 | GPIO0 | GPIO pin, no muxing, no ISR |
|
||||||
|
| 1 | ADC0 | AIO pin, returns random value on read |
|
||||||
|
|
||||||
Building
|
Building
|
||||||
--------
|
--------
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ extern "C" {
|
|||||||
|
|
||||||
#include "mraa_internal.h"
|
#include "mraa_internal.h"
|
||||||
|
|
||||||
#define MRAA_MOCK_PINCOUNT 1
|
#define MRAA_MOCK_PINCOUNT 2
|
||||||
|
|
||||||
mraa_board_t*
|
mraa_board_t*
|
||||||
mraa_mock_board();
|
mraa_mock_board();
|
||||||
|
|||||||
@@ -131,6 +131,29 @@ mraa_mock_gpio_mode_replace(mraa_gpio_context dev, mraa_gpio_mode_t mode)
|
|||||||
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
return MRAA_ERROR_FEATURE_NOT_IMPLEMENTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_aio_init_internal_replace(mraa_aio_context dev, int pin)
|
||||||
|
{
|
||||||
|
dev->channel = pin;
|
||||||
|
return MRAA_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
mraa_result_t
|
||||||
|
mraa_mock_aio_close_replace(mraa_aio_context dev)
|
||||||
|
{
|
||||||
|
free(dev);
|
||||||
|
return MRAA_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
mraa_mock_aio_read_replace(mraa_aio_context dev)
|
||||||
|
{
|
||||||
|
// return some random number between 0 and max value, based on the resolution
|
||||||
|
int max_value = (1 << dev->value_bit) - 1;
|
||||||
|
srand(time(NULL));
|
||||||
|
return rand() % max_value;
|
||||||
|
}
|
||||||
|
|
||||||
mraa_board_t*
|
mraa_board_t*
|
||||||
mraa_mock_board()
|
mraa_mock_board()
|
||||||
{
|
{
|
||||||
@@ -142,6 +165,7 @@ mraa_mock_board()
|
|||||||
// General board definitions
|
// General board definitions
|
||||||
b->platform_name = PLATFORM_NAME;
|
b->platform_name = PLATFORM_NAME;
|
||||||
b->phy_pin_count = MRAA_MOCK_PINCOUNT;
|
b->phy_pin_count = MRAA_MOCK_PINCOUNT;
|
||||||
|
b->gpio_count = 1;
|
||||||
b->aio_count = 1;
|
b->aio_count = 1;
|
||||||
b->adc_raw = 12;
|
b->adc_raw = 12;
|
||||||
b->adc_supported = 10;
|
b->adc_supported = 10;
|
||||||
@@ -174,6 +198,9 @@ mraa_mock_board()
|
|||||||
b->adv_func->gpio_isr_replace = &mraa_mock_gpio_isr_replace;
|
b->adv_func->gpio_isr_replace = &mraa_mock_gpio_isr_replace;
|
||||||
b->adv_func->gpio_isr_exit_replace = &mraa_mock_gpio_isr_exit_replace;
|
b->adv_func->gpio_isr_exit_replace = &mraa_mock_gpio_isr_exit_replace;
|
||||||
b->adv_func->gpio_mode_replace = &mraa_mock_gpio_mode_replace;
|
b->adv_func->gpio_mode_replace = &mraa_mock_gpio_mode_replace;
|
||||||
|
b->adv_func->aio_init_internal_replace = &mraa_mock_aio_init_internal_replace;
|
||||||
|
b->adv_func->aio_close_replace = &mraa_mock_aio_close_replace;
|
||||||
|
b->adv_func->aio_read_replace = &mraa_mock_aio_read_replace;
|
||||||
|
|
||||||
// Pin definitions
|
// Pin definitions
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
@@ -184,6 +211,12 @@ mraa_mock_board()
|
|||||||
b->pins[pos].gpio.mux_total = 0;
|
b->pins[pos].gpio.mux_total = 0;
|
||||||
pos++;
|
pos++;
|
||||||
|
|
||||||
|
strncpy(b->pins[pos].name, "ADC0", 8);
|
||||||
|
b->pins[pos].capabilites = (mraa_pincapabilities_t){ 1, 0, 0, 0, 0, 0, 1, 0 };
|
||||||
|
b->pins[pos].aio.pinmap = 0;
|
||||||
|
b->pins[pos].aio.mux_total = 0;
|
||||||
|
pos++;
|
||||||
|
|
||||||
return b;
|
return b;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ add_test (NAME py_gpio_edge COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT
|
|||||||
add_test (NAME py_gpio_isr COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/gpio_checks_isr.py)
|
add_test (NAME py_gpio_isr COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/gpio_checks_isr.py)
|
||||||
add_test (NAME py_gpio_mode COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/gpio_checks_mode.py)
|
add_test (NAME py_gpio_mode COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/gpio_checks_mode.py)
|
||||||
|
|
||||||
|
add_test (NAME py_aio COMMAND ${PYTHON_DEFAULT_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/aio_checks.py)
|
||||||
|
|
||||||
set_tests_properties(py_general
|
set_tests_properties(py_general
|
||||||
py_platform
|
py_platform
|
||||||
py_gpio_basic
|
py_gpio_basic
|
||||||
@@ -16,4 +18,5 @@ set_tests_properties(py_general
|
|||||||
py_gpio_edge
|
py_gpio_edge
|
||||||
py_gpio_isr
|
py_gpio_isr
|
||||||
py_gpio_mode
|
py_gpio_mode
|
||||||
|
py_aio
|
||||||
PROPERTIES ENVIRONMENT "PYTHONPATH=${PYTHON_DEFAULT_PYTHONPATH}")
|
PROPERTIES ENVIRONMENT "PYTHONPATH=${PYTHON_DEFAULT_PYTHONPATH}")
|
||||||
|
|||||||
58
tests/mock/aio_checks.py
Normal file
58
tests/mock/aio_checks.py
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Author: Alex Tereschenko <alext.mkrs@gmail.com>
|
||||||
|
# Copyright (c) 2016 Alex Tereschenko.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
# a copy of this software and associated documentation files (the
|
||||||
|
# "Software"), to deal in the Software without restriction, including
|
||||||
|
# without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
# permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
# the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be
|
||||||
|
# included in all copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import mraa as m
|
||||||
|
import unittest as u
|
||||||
|
|
||||||
|
MRAA_AIO_TEST_PIN = 0
|
||||||
|
PLATFORM_STD_ADC_RES_BITS = 10
|
||||||
|
PLATFORM_MAX_ADC_RES_BITS = 12
|
||||||
|
|
||||||
|
class AioChecks(u.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.pin = m.Aio(MRAA_AIO_TEST_PIN)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
del self.pin
|
||||||
|
|
||||||
|
def test_aio_get_bit(self):
|
||||||
|
self.assertEqual(self.pin.getBit(), PLATFORM_STD_ADC_RES_BITS, "Wrong ADC resolution reported")
|
||||||
|
|
||||||
|
def test_aio_set_bit(self):
|
||||||
|
self.pin.setBit(PLATFORM_MAX_ADC_RES_BITS)
|
||||||
|
self.assertEqual(self.pin.getBit(), PLATFORM_MAX_ADC_RES_BITS, "Wrong ADC resolution reported after setBit()")
|
||||||
|
|
||||||
|
def test_aio_read(self):
|
||||||
|
self.assertNotEqual(self.pin.read(), -1, "Error returned when reading ADC value")
|
||||||
|
|
||||||
|
def test_aio_read_float_std_res(self):
|
||||||
|
self.pin.setBit(PLATFORM_STD_ADC_RES_BITS)
|
||||||
|
self.assertNotEqual(self.pin.readFloat(), -1, "Error returned when reading float ADC value at standard resolution")
|
||||||
|
|
||||||
|
def test_aio_read_float_max_res(self):
|
||||||
|
self.pin.setBit(PLATFORM_MAX_ADC_RES_BITS)
|
||||||
|
self.assertNotEqual(self.pin.readFloat(), -1, "Error returned when reading float ADC value at maximum resolution")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
u.main()
|
||||||
@@ -27,7 +27,7 @@
|
|||||||
import mraa as m
|
import mraa as m
|
||||||
import unittest as u
|
import unittest as u
|
||||||
|
|
||||||
PLATFORM_PINCOUNT = 1
|
PLATFORM_PINCOUNT = 2
|
||||||
PLATFORM_STD_ADC_RES_BITS = 10
|
PLATFORM_STD_ADC_RES_BITS = 10
|
||||||
PLATFORM_MAX_ADC_RES_BITS = 12
|
PLATFORM_MAX_ADC_RES_BITS = 12
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user