You are reading a single comment by @StuntMonkeh and its replies. Click here to read the full conversation.
  • I'm not sure I have got the grasp of these modules.

    How does something like this look?

    /* Copyright (c) 2014 Your Name. See the file LICENSE for copying permission. */
    /*
    Quick description of my module...
    */
    
    //private
    var C = {
      dstStatus : ,          // description
      rtcDate: ,     // description
      rtcTime: ,  // description
    };
    
    //private functions
    // Convert Decimal value to BCD
    function dec2bcd(val) {
      return parseInt(val, 16);
    }
    
    // Convert BCD value to decimal
    function bcd2dec(val) {
      return ("0"+val.toString(16)).substr(-2);
    }
    
    //DST
    function isDST(date,month,dow)
        {
            //January, february, and december are out.
            if (month < 3 || month > 11) {
        return false;
        }
            //April to October are in
            if (month > 3 && month < 11) {
        return true;
        }
        var previousSunday = day - dow;
            //In march, we are DST if our previous sunday was on or after the 8th.
            if (month == 3) {
        return previousSunday >= 8;
        }
            //In november we must be before the first sunday to be dst.
            //That means the previous sunday must be before the 1st.
            return previousSunday <= 0;
        }
    
    //public
    DS3231.prototype.C = {
      i2c_address = 0x68,
      rtcTime : ,          // description
      rtcDate : ,     // description
      rtcDay : ,  // description
      dstShift :
    };
    
    //public functions
    /* Put most of my comments outside the functions... */
    DS3231.prototype.setDate = function(date,month,year) {
      I2C1.writeTo(i2c_address,[0x04, (dec2bcd(date))]);
      I2C1.writeTo(i2c_address,[0x05, (dec2bcd(month))]);
      I2C1.writeTo(i2c_address,[0x06, (dec2bcd(year))]);
    }
    
    DS3231.prototype.setTime = function(hour,minute) {
      I2C1.writeTo(i2c_address,[0x00, 0]);
      I2C1.writeTo(i2c_address,[0x01, (dec2bcd(minute))]);
      if (dstStatus === true) {
        I2C1.writeTo(i2c_address,[0x02, (dec2bcd(hour-1))]);
      }
      else
        I2C1.writeTo(i2c_address,[0x02, (dec2bcd(hour))]);
    }
    
    DS3231.prototype.setDay = function (val) {
      var days = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
      var idx = days.indexOf(val);
      if (idx<0) {
        print("Not a valid day");
      }
      else {
        I2C1.writeTo(i2c_address,[0x03, (dec2bcd(1+idx))]);
     }
    }
    
    DS3231.prototype.setDstShift = function (hours) {
      dstShift = hours;
    }
    
    DS3231.prototype.readDateTime = function () {
      I2C1.writeTo(i2c_address, 0x00/* address*/);
      var data = I2C1.readFrom(i2c_address, 7/* bytes */); //read number of bytes from address
      var seconds = bcd2dec(data[0]);
      var minutes = (bcd2dec(data[1]));
      var hours = (data[2]);
      var dow = (bcd2dec(data[3]));
      var date = (bcd2dec(data[4]));
      var month = (bcd2dec(data[5]));
      var year = (bcd2dec(data[6]));
    
      if ((isDST(date,month,dow)) === true) {
        hours = bcd2dec((hours)+(dstShift));
        dstStatus = true;
      }
      else {
          hours = bcd2dec(hours);
        dstStatus = false;
      }
    
      rtcDate = date+"/"+month+"/"+year;
      rtcTime = hours+":"+minutes+":"+seconds;
      rtcDateTime = rtcDate+" "+rtcTime;
      rtcDay = dow;
      return rtcDateTime;
    };
    
About

Avatar for StuntMonkeh @StuntMonkeh started