1/*jslint node:true, vars:true, bitwise:true, unparam:true */
2/*jshint unused:true */
3/*
4* Author: Zion Orent <zorent@ics.com>
5* Copyright (c) 2015 Intel Corporation.
6*
7* Permission is hereby granted, free of charge, to any person obtaining
8* a copy of this software and associated documentation files (the
9* "Software"), to deal in the Software without restriction, including
10* without limitation the rights to use, copy, modify, merge, publish,
11* distribute, sublicense, and/or sell copies of the Software, and to
12* permit persons to whom the Software is furnished to do so, subject to
13* the following conditions:
14*
15* The above copyright notice and this permission notice shall be
16* included in all copies or substantial portions of the Software.
17*
18* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25*/
26
27/* functions */
28function printButtons(touchObj)
29{
30	var buttonPressed = false;
31	var buttons = touchObj.getButtons();
32
33	process.stdout.write("Buttons Pressed: ");
34	for (var i=0; i<7; i++)
35	{
36		if (buttons & (1 << i))
37		{
38			process.stdout.write(i + " ");
39			buttonPressed = true;
40		}
41	}
42
43	if (!buttonPressed)
44		process.stdout.write("None");
45
46	console.log(" ");
47
48	if (touchObj.isCalibrating())
49		console.log("Calibration is occurring.");
50
51	if (touchObj.isOverflowed())
52		console.log("Overflow was detected.");
53}
54
55
56/* Global code that runs on startup */
57
58var touchSensor_lib = require('jsupm_at42qt1070');
59
60var I2C_BUS = touchSensor_lib.AT42QT1070_I2C_BUS;
61var DEFAULT_I2C_ADDR = touchSensor_lib.AT42QT1070_DEFAULT_I2C_ADDR;
62// Instantiate an AT42QT1070 on I2C bus 0
63var mytouchSensor_obj = new touchSensor_lib.AT42QT1070(I2C_BUS,
64                                               DEFAULT_I2C_ADDR);
65
66var myInterval = setInterval(function()
67{
68	mytouchSensor_obj.updateState();
69	printButtons(mytouchSensor_obj);
70}, 100);
71
72// Print message when exiting and clear interval/memory
73process.on('SIGINT', function()
74{
75	clearInterval(myInterval);
76	mytouchSensor_obj = null;
77	touchSensor_lib.cleanUp();
78	touchSensor_lib = null;
79	console.log("Exiting");
80	process.exit(0);
81});
82