Home | History | Annotate | Download | only in sx6119
      1 /*
      2  * Author: Jon Trulson <jtrulson (at) ics.com>
      3  * Copyright (c) 2015 Intel Corporation.
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining
      6  * a copy of this software and associated documentation files (the
      7  * "Software"), to deal in the Software without restriction, including
      8  * without limitation the rights to use, copy, modify, merge, publish,
      9  * distribute, sublicense, and/or sell copies of the Software, and to
     10  * permit persons to whom the Software is furnished to do so, subject to
     11  * the following conditions:
     12  *
     13  * The above copyright notice and this permission notice shall be
     14  * included in all copies or substantial portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
     19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
     20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     23  */
     24 
     25 #include <unistd.h>
     26 #include <iostream>
     27 #include <string>
     28 #include <stdexcept>
     29 
     30 #include "sx6119.h"
     31 
     32 using namespace upm;
     33 using namespace std;
     34 
     35 SX6119::SX6119(int powerPin, int seekPin)
     36 {
     37   if ( !(m_gpioPower = mraa_gpio_init(powerPin)) )
     38     {
     39       throw std::invalid_argument(std::string(__FUNCTION__) +
     40                                   ": mraa_gpio_init(power) failed, invalid pin?");
     41       return;
     42     }
     43 
     44   mraa_gpio_dir(m_gpioPower, MRAA_GPIO_OUT);
     45   mraa_gpio_write(m_gpioPower, 1);
     46 
     47   if ( !(m_gpioSeek = mraa_gpio_init(seekPin)) )
     48     {
     49       throw std::invalid_argument(std::string(__FUNCTION__) +
     50                                   ": mraa_gpio_init(seek) failed, invalid pin?");
     51       return;
     52     }
     53 
     54   mraa_gpio_dir(m_gpioSeek, MRAA_GPIO_OUT);
     55   mraa_gpio_write(m_gpioSeek, 1);
     56 }
     57 
     58 SX6119::~SX6119()
     59 {
     60   mraa_gpio_close(m_gpioPower);
     61   mraa_gpio_close(m_gpioSeek);
     62 }
     63 
     64 void SX6119::togglePower()
     65 {
     66   // this is just a toggle -- we set LOW for one second and power will
     67   // be turned on or off depending on the previous condition.
     68   mraa_gpio_write(m_gpioPower, 0);
     69   sleep(1);
     70   mraa_gpio_write(m_gpioPower, 1);
     71 }
     72 
     73 void SX6119::seek()
     74 {
     75   // this is just a trigger -- we set LOW for 500ms to seek to the
     76   // next available station, wrapping around when we reach the end.
     77   mraa_gpio_write(m_gpioSeek, 0);
     78   usleep(500000);
     79   mraa_gpio_write(m_gpioSeek, 1);
     80 }
     81