mirror of
https://github.com/eclipse/upm.git
synced 2025-07-01 01:11:10 +03:00
TCA9548A : initial implementation
merged with #413 from maleek Signed-off-by: g-vidal <gerard.vidal@ens-lyon.fr> Signed-off-by: Keelan Lightfoot <keelanlightfoot@gmail.com> Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
103
examples/c++/tca9548a.cxx
Normal file
103
examples/c++/tca9548a.cxx
Normal file
@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Author: Gerard Vidal <gerard.vidal@ens-lyon.fr>
|
||||
* 2017 IFE ENS de Lyon
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "tca9548a.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace upm;
|
||||
|
||||
/*
|
||||
* This examble is built to multiplex channel 2 and Channel 4
|
||||
*
|
||||
*/
|
||||
|
||||
void
|
||||
showAllPorts(upm::TCA9548A *multiplex) {
|
||||
int i;
|
||||
char ports [80];
|
||||
std::string convert;
|
||||
for (i = 0; i<8; i++) {
|
||||
if (multiplex->getPort(i))
|
||||
strcat (ports, "1." );
|
||||
else
|
||||
strcat (ports,"0.");
|
||||
}
|
||||
cout << "Ports: " << ports;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
|
||||
// Instantiate a TCA9548A instance of i2c multiplexer
|
||||
upm::TCA9548A *multiplex = new upm::TCA9548A(0);
|
||||
|
||||
//Clean
|
||||
multiplex->disableAllPorts();
|
||||
cout << "All ports disabled.";
|
||||
|
||||
// Open i2c bus on multiplexer channel 4
|
||||
multiplex->setPort(4,multiplex->ENABLED,multiplex->EXCLUSIVE);
|
||||
cout << "Port 4 enabled with exclusive access.";
|
||||
showAllPorts(multiplex);
|
||||
|
||||
// Close com with channel 4
|
||||
multiplex->disableAllPorts();
|
||||
|
||||
// Open i2c bus on multiplexer channel 2
|
||||
multiplex->setPort(2,multiplex->ENABLED,multiplex->EXCLUSIVE);
|
||||
cout << "Port 2 enabled with exclusive access.";
|
||||
showAllPorts(multiplex);
|
||||
|
||||
//close com with channel 2
|
||||
multiplex->disableAllPorts();
|
||||
|
||||
// Open 2 i2c bus on multiplexer channel 4 and channel2
|
||||
multiplex->setPort(4,multiplex->ENABLED,multiplex->EXCLUSIVE);
|
||||
multiplex->setPort(2,multiplex->ENABLED,multiplex->INCLUSIVE);
|
||||
cout << "Port 4 enabled with exclusive access.";
|
||||
cout << "Port 2 enabled with exclusive access.";
|
||||
showAllPorts(multiplex);
|
||||
|
||||
//close com with all channels
|
||||
multiplex->disableAllPorts();
|
||||
|
||||
// Open i2c bus on all multiplexer channels
|
||||
multiplex->enableAllPorts();
|
||||
cout << "All ports enabled.";
|
||||
showAllPorts(multiplex);
|
||||
|
||||
//close com with all channels
|
||||
multiplex->disableAllPorts();
|
||||
|
||||
delete multiplex;
|
||||
|
||||
return 0;
|
||||
}
|
75
examples/javascript/tca9548.js
Normal file
75
examples/javascript/tca9548.js
Normal file
@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Author: Keelan Lightfoot <keelanlightfoot@gmail.com>
|
||||
* Copyright (c) 2015 Intel Corporation.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* On its own, a i2c switch doesn't really do anything, so the best I can do is
|
||||
* flips some ports on and off to demonstrate the capabilities of the library.
|
||||
*/
|
||||
|
||||
var tca = require('jsupm_tca9548a');
|
||||
|
||||
// Assumes default bus and address
|
||||
var sw = new tca.TCA9548A(tca.TCA9548A_I2C_BUS,
|
||||
tca.TCA9548A_DEFAULT_ADDR);
|
||||
|
||||
// Start with clean slate
|
||||
sw.disableAllPorts();
|
||||
console.log("All ports disabled.");
|
||||
showAllPorts();
|
||||
|
||||
// Enable port 4
|
||||
sw.setPort(4, tca.TCA9548A.ENABLED, tca.TCA9548A.EXCLUSIVE);
|
||||
console.log("Port 4 enabled with exclusive access.");
|
||||
showAllPorts();
|
||||
|
||||
// Enable port 5
|
||||
sw.setPort(5, tca.TCA9548A.ENABLED, tca.TCA9548A.INCLUSIVE);
|
||||
console.log("Port 5 enabled with inclusive access.");
|
||||
showAllPorts();
|
||||
|
||||
// Disable port 5
|
||||
sw.setPort(5, tca.TCA9548A.DISABLED, tca.TCA9548A.INCLUSIVE);
|
||||
console.log("Port 5 disabled with inclusive access.");
|
||||
showAllPorts();
|
||||
|
||||
// Enable all ports
|
||||
sw.enableAllPorts();
|
||||
console.log("All ports enabled.");
|
||||
showAllPorts();
|
||||
|
||||
// Clean up our mess, in an oblique way.
|
||||
sw.setPort(4, tca.TCA9548A.DISABLED, tca.TCA9548A.EXCLUSIVE);
|
||||
console.log("Port 4 disabled with exclusive access.");
|
||||
showAllPorts();
|
||||
|
||||
// fin
|
||||
process.exit(0);
|
||||
|
||||
function showAllPorts() {
|
||||
var ports = "";
|
||||
for (var i = 0; i<8; i++) {
|
||||
ports += (sw.getPort(i)?i:".");
|
||||
}
|
||||
console.log("Ports: "+ports);
|
||||
}
|
Reference in New Issue
Block a user