Skip to content

Commit

Permalink
Merge pull request #62 from ghi-electronics/tweak
Browse files Browse the repository at this point in the history
Tweak
  • Loading branch information
Palomino34 authored Oct 9, 2024
2 parents 5d548da + 8639406 commit f948c08
Show file tree
Hide file tree
Showing 3 changed files with 454 additions and 105 deletions.
2 changes: 1 addition & 1 deletion docs/common/html/id_sidemenu.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<div class="dropdown-container">
<a href="https://demo.duelink.com/p/ledmatrix.html">LED Matrix</a>
<!-- <a href="#">Servo moto</a> -->
<a href="https://demo.duelink.com/p/i2c_qwiicjoystick.html">QWIIC Joystick</a>
<!-- <a href="#">Stepper moto</a> -->
<a href="https://demo.duelink.com/p/piano.html">Piano</a>
<!-- <a href="#">Temperture</a> -->
Expand Down
273 changes: 169 additions & 104 deletions docs/p/i2c.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,52 +60,82 @@
<div class="center">

<div style="border: solid 1px; padding: 0.5em; margin-top:0.5em; ">

<table >
<tr>
<td width="120">X value: </td>
<td width="200">
<span id="joyStickX"></span>
<td width="200">Slave address (7 bits)</td>
<td width="200">
<input type="text" id="id-address" value=""/><br>
</td>

</tr>


<tr>
<td width="200">Data to write: (0,1,2... 255)</td>
<td width="200">
<input type="text" id="id-dataWrite" value="1,2,3"/><br>
</td>

</tr>
<tr>
<td width="120">Y value: </td>
<td width="200">
<span id="joyStickY"></span>
<button class="btn btn-primary" onclick="doWrite()" id="id-btWrite" disabled="true" >Write</button> <br><br>
</td>

</tr>


<tr>
<td width="200">Read byte count:</td>
<td width="200">
<input type="text" id="id-readCount" value="1"/><br>
</td>

</tr>
<tr>
<td width="120">Button pressed: </td>
<td width="200">
<span id="joyStickButton"></span>
<button class="btn btn-primary" onclick="doRead()" id="id-btRead" disabled="true" >Read</button> <br><br>
</td>

</tr>

<tr>
<td width="200">
<button class="btn btn-primary" onclick="doWriteRead()" id="id-btWriteRead" disabled="true" >WriteRead</button> <br><br>
</td>

</tr>

</table>
<table>
<canvas id="myChart" > </canvas>
<script>
$("#myChart").hide();
</script>
</table>
</div>

</div>
<div style="border: solid 1px; padding: 0.5em; margin-top:0.5em; " >
Received data
<table >

<tr>
<td width="200">
<textarea rows="6" cols="50" id="id-dataReceived" disabled>

</div>

<div class="row">
<table >
<tr>
<td width="120"/>
</textarea>
</td>
</tr>

<tr>
<td width="200">
<button class="btn btn-primary" id="id-buttonRead" onclick="startReading()" disabled="true" >Start Reading</button>
<button class="btn btn-primary" onclick="doClear()" id="id-btClear" disabled="true" >Clear</button> <br><br>
</td>
</tr>
</table>

</tr>

</table>

</div>

</div>

</div>


</div>

<div id="id-status" style="display:none">
Expand Down Expand Up @@ -154,7 +184,10 @@
}
$("#id-version").text(duelink.Version);
$("#id-connect").slideUp();
$("#id-buttonRead").prop("disabled", false);
$("#id-btWrite").prop("disabled", false);
$("#id-btRead").prop("disabled", false);
$("#id-btWriteRead").prop("disabled", false);
$("#id-btClear").prop("disabled", false);

} catch (e) {
showError("connect Error" + e);
Expand All @@ -166,6 +199,10 @@
function hideError() {
$("#id-error").hide();
}

function hideStatus() {
$("#id-status").hide();
}

function showStatus(message) {
$("#id-buttonRead").prop("disabled", true);
Expand All @@ -184,101 +221,129 @@
}


function startReading() {
async function doWrite() {
hideError();
hideStatus();

const id = $("#id-address").val().trim();
const data = $("#id-dataWrite").val().trim().split(",");


var canWrite = true;

var dataWrite = new Uint8Array(data.length)

for (let i = 0; i < data.length; i++) {

buttonPressCounter += 1
if ((buttonPressCounter % 2) == 1) {
document.getElementById("id-buttonRead").innerHTML = "Stop Reading";
doActionCallback()
$("#myChart").show();
}
else {
document.getElementById("id-buttonRead").innerHTML = "Start Reading";
$("#myChart").hide();
var x = parseInt(data[i], 10);
if (isNaN(x) == true) {
showError(`Error at index: ${i}, value: ${data[i]}` );
canWrite = false;

break;
}

dataWrite[i] = x
}

if (canWrite) {
await duelink.I2c.Write(id,dataWrite,0,dataWrite.length)

showStatus("Data sent");
}
}

async function doRead() {
hideError();
hideStatus();

async function doActionCallback() {
try
{
if (isReading == false) {
Check();
isReading = true

let writeBuffer = new Uint8Array(1)
let readBuffer = [0,0,0,0,0]
const id = $("#id-address").val().trim();
const count = $("#id-readCount").val().trim();

writeBuffer[0] = 3

await duelink.I2c.WriteRead(0x20,writeBuffer,0,1,readBuffer,0,5)

let x = (readBuffer[0]<<8 | readBuffer[1])>>6
let y = (readBuffer[2]<<8 | readBuffer[3])>>6

document.getElementById("joyStickX").innerHTML = x;
document.getElementById("joyStickY").innerHTML = y;
document.getElementById("joyStickButton").innerHTML = readBuffer[4] == 0 ? "True" : "False"
var canRead = true;


var x = parseInt(count, 10);

if (isNaN(x) == true) {
showError("Error count" );
canRead = false;
}


if (canRead) {
let data = new Uint8Array(x)
await duelink.I2c.Read(id,data,0,data.length)

let s="";

for (let i = 0; i < data.length; i++) {
s += `${data[i]}`

isReading = false

showChart(x, y)
if (i < data.length - 1)
s += ","
}

if (buttonPressCounter % 2 == 1) {
setTimeout(() => {
doActionCallback();
}, intervalCallBack);
}
document.getElementById("id-dataReceived").innerHTML += s
}
catch (err){
isReading = false
showError("Error: " + err)
buttonPressCounter = 0
document.getElementById("buttonRead").innerHTML = "Start Reading";
}
}

}

let lastX = -1
let lastY = -1
async function doWriteRead() {
hideError();
hideStatus();

const id = $("#id-address").val().trim();
const count = $("#id-readCount").val().trim();
const dataWrite = $("#id-dataWrite").val().trim().split(",");

var canWriteRead = true;

var c = parseInt(count, 10);

if (isNaN(c) == true) {
showError("Error count" );
canWriteRead = false;
}

var dataWrite2 = new Uint8Array(dataWrite.length)

for (let i = 0; i < dataWrite.length; i++) {

let x = parseInt(dataWrite[i], 10);

if (isNaN(x) == true) {
showError(`Error at index: ${i}, value: ${dataWrite[i]}` );
canWriteRead = false;

async function showChart(x, y) {

if (lastX != x || lastY != y) {
lastX = x
lastY = y
if(Chart.getChart("myChart")) {
Chart.getChart("myChart")?.destroy()
break;
}

if (this.chart) {
this.chart = null
}
dataWrite2[i] = x
}

if (canWriteRead) {
let dataRead = new Uint8Array(c)
await duelink.I2c.WriteRead(id,dataWrite2,0,dataWrite2.length, dataRead, 0, dataRead.length)

if (!this.chart ) {
this.chart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['X', 'Y'],
datasets: [{
label: 'QWIIC Joytick values',
data: [x, y, ],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
let s="";

for (let i = 0; i < dataRead.length; i++) {
s += `${dataRead[i]}`

if (i < dataRead.length - 1)
s += ","
}
}

document.getElementById("id-dataReceived").innerHTML += s
document.getElementById("id-dataReceived").innerHTML += "\n"

}
}

async function doClear() {
document.getElementById("id-dataReceived").innerHTML = ""
}


</script>
Loading

0 comments on commit f948c08

Please sign in to comment.