I2C1.setup({scl:b6, sda:b7});
function Sensor (theType, theAddress) {
this.type = theType; //i.e. PH
this.address = theAddress; //i2c Address
this.sensorResult = 0; //Store sensor result
this.cmdTable = {
'Calibrate' : { //List of Calibration commands and timeout value.
"clear" : { "cmd" : "Cal,Clear", "wait" : 300 },
"mid" : { "cmd" : "Cal,mid,7.00", "wait" : 1300 },
"low" : { "cmd" : "Cal,low,4.00", "wait" : 1300 },
"high" : { "cmd" : "Cal,high,10.00", "wait" : 1300 },
"query" : { "cmd" : "Cal,?", "wait" : 300 }
},
'Information' : { //Device Information
},
'LED' : { //Enable / Disable or Query the LEDs
"L0" : { "cmd" : "L,0", "wait" : 300 },
"L1" : { "cmd" : "L,1", "wait" : 300 },
"L?" : { "cmd" : "L,?", "wait" : 300 }
},
'Reading' : { //Takes a single reading
"R" : { "cmd" : "R,?", "wait" : 1000 } //Takes a single temperature compensated reading
},
'Serial' : { //Switch back to UART mode
},
'Sleep' : { //Enter low power sleep mode
},
'Status' : { //Retrieve status information
},
'Temperature' : { //Set or Query the temperature compensation
"T" : { "cmd" : "T,19.5", "wait" : 300 }, //Where the temperature is any value; floating point, or int, in ASCII form
"T?" : { "cmd" : "T,?", "wait" : 300 } //Query the set temerature
},
'Factory' : { //Factory reset
},
};
}
Sensor.prototype = {
constructor: Sensor,
getSensorType:function () {
return this.type; //Get Sensor type
},
getSensorAddress:function () {
return this.address; //Get Sensor address
},
getSensorResult:function () {
//I2C1.read
},
storeSensorResult:function () {
},
updateResTemp:function (temp) {
var reading = cmdTable.Reading.R.cmd;
reading = "R," + temp;
Console.log(reading);
}
};
var ph = new Sensor("ph", 0x63);
ph.updateResTemp(90.5);
Is that what you are refering too? I am new to Javascript objects and etc... For example, in order for me to get the sensor result I would invoke getSensorResult and grab the values from the cooresponding Sensor object and setTimeout to whatever the wait value is of the specific cmd?
Also, to take a Reading in i2c I am required to pass the temperature along with the R to get a temperature compensated result. I have the default command as R,19.5. How would I update the cmd to the newely taken temperature value? I tried to change the value and I receive the following error:
Uncaught Error: Field or method does not already exist, and can't
create it on undefined at line 2 col 27
var reading = cmdTable.Reading.R.cmd; e ^ in function "updateResTemp" called rom line 1 col 22
ph.updateResTemp(90.5);
I fixed the error
new and complete code:
I2C1.setup({scl:b6, sda:b7});
function Sensor (theType, theAddress) {
this.type = theType; //i.e. PH
this.address = theAddress; //i2c Address
this.sensorResult = 0; //Store sensor result
this.cmdTable = {
"Calibrate" : { //List of Calibration commands and timeout value.
"clear" : { "cmd" : "Cal,Clear", "wait" : 300 },
"mid" : { "cmd" : "Cal,mid,7.00", "wait" : 1300 },
"low" : { "cmd" : "Cal,low,4.00", "wait" : 1300 },
"high" : { "cmd" : "Cal,high,10.00", "wait" : 1300 },
"query" : { "cmd" : "Cal,?", "wait" : 300 }
},
"Information" : { //Device Information
},
"LED" : { //Enable / Disable or Query the LEDs
"L0" : { "cmd" : "L,0", "wait" : 300 },
"L1" : { "cmd" : "L,1", "wait" : 300 },
"L?" : { "cmd" : "L,?", "wait" : 300 }
},
"Reading" : { //Takes a single reading
"R" : { "cmd" : "R,?", "wait" : 1000 } //Takes a single temperature compensated reading
},
"Serial" : { //Switch back to UART mode
},
"Sleep" : { //Enter low power sleep mode
},
"Status" : { //Retrieve status information
},
"Temperature" : { //Set or Query the temperature compensation
"T" : { "cmd" : "T,19.5", "wait" : 300 }, //Where the temperature is any value; floating point, or int, in ASCII form
"T?" : { "cmd" : "T,?", "wait" : 300 } //Query the set temerature
},
"Factory" : { //Factory reset
},
};
}
Sensor.prototype = {
constructor: Sensor,
getSensorType:function () {
return this.type; //Get Sensor type
},
getSensorAddress:function () {
return this.address; //Get Sensor address
},
getSensorResult:function () {
//I2C1.read
},
storeSensorResult:function () {
},
updateResTemp:function (temp) {
console.log("Before: " + this.cmdTable.Reading.R.cmd);
//reading;
//reading = "R," + temp;
//Console.log(reading);
this.cmdTable.Reading.R.cmd = "R," + temp;
console.log("After: " + this.cmdTable.Reading.R.cmd);
}
};
var ph = new Sensor("ph", 0x63);
ph.updateResTemp(90.5);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
@allObjects So far I have:
Is that what you are refering too? I am new to Javascript objects and etc... For example, in order for me to get the sensor result I would invoke getSensorResult and grab the values from the cooresponding Sensor object and setTimeout to whatever the wait value is of the specific cmd?
Also, to take a Reading in i2c I am required to pass the temperature along with the R to get a temperature compensated result. I have the default command as R,19.5. How would I update the cmd to the newely taken temperature value? I tried to change the value and I receive the following error:
I fixed the error
new and complete code: