<commit_before>
// Copyright (c) 2011, <NAME>
// Released under MIT and GPLv2

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <OneWire.h>
#define num(array) (sizeof(array)/sizeof(array[0]))

// Ethernet Configuration

byte mac[] = { 0xEE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED   };
//byte ip[] = { 10, 94, 54, 2   };
//byte gateway[] = { 10, 94, 54, 1 };
IPAddress ip(10, 0, 3, 201 );
IPAddress gateway( 10, 0, 3, 1 );
IPAddress subnet( 255, 255, 255, 0 );

EthernetServer server(1111);
EthernetClient client(255);

unsigned long requests = 0;

// Sensor Configuration

OneWire ds(8);

int analog[2];
struct Temp {
  unsigned int code; 
  int data;
} temp[4] = {{0,0}};

unsigned int last = 100;
unsigned int powersave = 0;
unsigned long topOfHour = 0;
unsigned long crc_errs = 0;

// Arduino Setup and Loop

void setup() {
  Serial.begin(115200L);
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
}

void loop() {
  sample();    // every second or so
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
  serve();     // whenever web requests come in
  digitalWrite(13,LOW);
}

// Sample and Hold Analog and One-Wire Temperature Data

void sample() {
  unsigned int now = millis();
  if ((now-last) >= 1000) {
    last = now;
    powerClock();
    analogSample();
    tempSample();
  }
}

void powerClock() {
  boolean poweron = powersave == 0;
  pinMode(2,OUTPUT);
  digitalWrite(2,poweron);
  if (!poweron) {
    powersave--;
  }
}

void analogSample() {
  for (int i = 0; i < num(analog); i++) {
    analog[i] = analogRead(i);
  }
}

byte data[12];
unsigned int id;
int ch = -1;

void tempSample() {
  finishTempSample();
  startTempSample();
}

void startTempSample() {
  if (ch < 0) {
    ds.reset_search();
  }
  if (!ds.search(data)) {
    ch = -1;
  } 
  else {
    if (OneWire::crc8(data, 7) == data[7] && 0x28 == data[0]) {
      id = data[2]*256u+data[1];
      ch = channel (id);
      ds.reset();
      ds.select(data);
      ds.write(0x44,1);         // start conversion, with parasite power on at the end
    } else {
      crc_errs++;
      Serial.print(id);
      Serial.println(F(" a-err"));
    }
  }
}

void finishTempSample() {
  if (ch >= 0) {                // if we've discovered a devise and started a conversion
    ds.reset();
    ds.select(data);
    ds.write(0xBE);             // Read Scratchpad
    for (int i = 0; i < 9; i++) {
      data[i] = ds.read();
    }
    if (OneWire::crc8(data, 8) == data[8]) {
      temp[ch].data = data[1]*256+data[0];
      temp[ch].code = id;      // don't set this too early or we could report bad data
    } else {
      crc_errs++;
      Serial.print(id);
      Serial.println(F(" d-err"));
    }
  }
}

int channel(int id) {
  for (int ch=0; ch<num(temp); ch++) {
    if (temp[ch].code == id || temp[ch].code == 0) {
      return ch;
    }
  }
  return 0;
}

// Accept Web Requests for Held Data

void serve() {
  client = server.available();
  if (client) {
    requests++;
    boolean blank = true;
    boolean slash = false;
    char code = 0;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.print(c);
        if (c == '\n' && blank) {
          report(code);
          break;
        }
        if (c == '\n') {
          blank = true;
        } 
        else if (c != '\r') {
          blank = false;
          if (slash && code == 0) {
            code = c;
          }
          slash = c == '/';
        }
      }
    }
    client.stop();
  }
}

// Respond to Web Requests in HTML and JSON

void report(char code) {
  if (code == ' ') {
    htmlReport();
  } else if (code == 'g') {
    jsonReport();
  } else if (code == 'f') {
    faviconReport();
  } else if (code == 's') {
    topOfHour = millis();
  } else if (code == 'p') {
    powersave = 55*60;
  } else {
    errorReport();
  }
}

void p(char s) { client.print(s); }
void p(__FlashStringHelper* s) { client.print(s); }
void n(char s) { p(s); p(F("\r\n")); }
void n(__FlashStringHelper* s) { p(s); p(F("\r\n")); }
void a() { p(F("http://wiki.org/")); }

void code(__FlashStringHelper* s) { p(F("HTTP/1.1 ")); n(s);}
void mime(__FlashStringHelper* s) { p(F("Content-Type: ")); n(s); n(F("")); }
void link(__FlashStringHelper* s) { p(F("<link href='")); a(); p(s); n(F("' rel='stylesheet' type='text/css'>")); }<commit_msg>globalize 'now' ... unsigned long returned from millis()<commit_after>
// Copyright (c) 2011, <NAME>
// Released under MIT and GPLv2

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <OneWire.h>
#define num(array) (sizeof(array)/sizeof(array[0]))

// Ethernet Configuration

byte mac[] = { 0xEE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED   };
//byte ip[] = { 10, 94, 54, 2   };
//byte gateway[] = { 10, 94, 54, 1 };
IPAddress ip(10, 0, 3, 201 );
IPAddress gateway( 10, 0, 3, 1 );
IPAddress subnet( 255, 255, 255, 0 );

EthernetServer server(1111);
EthernetClient client(255);

unsigned long requests = 0;

// Sensor Configuration

OneWire ds(8);

int analog[2];
struct Temp {
  unsigned int code; 
  int data;
} temp[4] = {{0,0}};

unsigned int last = 100;
unsigned int powersave = 0;
unsigned long now = 0;
unsigned long topOfHour = 0;
unsigned long crc_errs = 0;

// Arduino Setup and Loop

void setup() {
  Serial.begin(115200L);
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();
}

void loop() {
  sample();    // every second or so
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
  serve();     // whenever web requests come in
  digitalWrite(13,LOW);
}

// Sample and Hold Analog and One-Wire Temperature Data

void sample() {
  now = millis();
  if ((now-last) >= 1000) {
    last = now;
    powerClock();
    analogSample();
    tempSample();
  }
}

void powerClock() {
  boolean poweron = powersave == 0;
  pinMode(2,OUTPUT);
  digitalWrite(2,poweron);
  if (!poweron) {
    powersave--;
  }
}

void analogSample() {
  for (int i = 0; i < num(analog); i++) {
    analog[i] = analogRead(i);
  }
}

byte data[12];
unsigned int id;
int ch = -1;

void tempSample() {
  finishTempSample();
  startTempSample();
}

void startTempSample() {
  if (ch < 0) {
    ds.reset_search();
  }
  if (!ds.search(data)) {
    ch = -1;
  } 
  else {
    if (OneWire::crc8(data, 7) == data[7] && 0x28 == data[0]) {
      id = data[2]*256u+data[1];
      ch = channel (id);
      ds.reset();
      ds.select(data);
      ds.write(0x44,1);         // start conversion, with parasite power on at the end
    } else {
      crc_errs++;
      Serial.print(id);
      Serial.println(F(" a-err"));
    }
  }
}

void finishTempSample() {
  if (ch >= 0) {                // if we've discovered a devise and started a conversion
    ds.reset();
    ds.select(data);
    ds.write(0xBE);             // Read Scratchpad
    for (int i = 0; i < 9; i++) {
      data[i] = ds.read();
    }
    if (OneWire::crc8(data, 8) == data[8]) {
      temp[ch].data = data[1]*256+data[0];
      temp[ch].code = id;      // don't set this too early or we could report bad data
    } else {
      crc_errs++;
      Serial.print(id);
      Serial.println(F(" d-err"));
    }
  }
}

int channel(int id) {
  for (int ch=0; ch<num(temp); ch++) {
    if (temp[ch].code == id || temp[ch].code == 0) {
      return ch;
    }
  }
  return 0;
}

// Accept Web Requests for Held Data

void serve() {
  client = server.available();
  if (client) {
    requests++;
    boolean blank = true;
    boolean slash = false;
    char code = 0;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.print(c);
        if (c == '\n' && blank) {
          report(code);
          break;
        }
        if (c == '\n') {
          blank = true;
        } 
        else if (c != '\r') {
          blank = false;
          if (slash && code == 0) {
            code = c;
          }
          slash = c == '/';
        }
      }
    }
    client.stop();
  }
}

// Respond to Web Requests in HTML and JSON

void report(char code) {
  if (code == ' ') {
    htmlReport();
  } else if (code == 'g') {
    jsonReport();
  } else if (code == 'f') {
    faviconReport();
  } else if (code == 's') {
    topOfHour = now = millis();
  } else if (code == 'p') {
    powersave = 55*60;
  } else {
    errorReport();
  }
}

void p(char s) { client.print(s); }
void p(__FlashStringHelper* s) { client.print(s); }
void n(char s) { p(s); p(F("\r\n")); }
void n(__FlashStringHelper* s) { p(s); p(F("\r\n")); }
void a() { p(F("http://wiki.org/")); }

void code(__FlashStringHelper* s) { p(F("HTTP/1.1 ")); n(s);}
void mime(__FlashStringHelper* s) { p(F("Content-Type: ")); n(s); n(F("")); }
void link(__FlashStringHelper* s) { p(F("<link href='")); a(); p(s); n(F("' rel='stylesheet' type='text/css'>")); }
{'fraction_non_alphanumeric': 0.18495404516055827, 'fraction_numerical': 0.029842278452286396, 'mean_word_length': 2.5087579617834397, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6576312', 'n_tokens_mistral': 3465, 'n_tokens_neox': 3225, 'n_words': 867}