//
// h8300-hms-gcc -O -mh -mint32 -r -fno-common -o measure measure.c
//
// 3 jun 04
//

#include <mes.h>

// 10 jun 04
struct st_ad {
unsigned short ADDRA;
unsigned short ADDRB;
unsigned short ADDRC;
unsigned short ADDRD;
union {
unsigned char BYTE;
struct {
unsigned char ADF :1;
unsigned char ADIE:1;
unsigned char ADST:1;
unsigned char SCAN:1;
unsigned char CKS :1;
unsigned char CH :3;
} BIT;
} ADCSR;
union {
unsigned char BYTE;
struct {
unsigned char TRGE:1;
} BIT;
} ADCR;
};

#define AD (*(volatile struct st_ad *)0xFFFFE0)

#define MASTER 24
#define MISTRESS 48

int second;
int minute;
int hour;
int date;
int month;
int year;

signed int heat[MASTER];

// 11 jun 04
void twins(signed int temperature)
{

if (temperature < 10){
printf("0");
}
printf("%d", temperature);

}

// 11 jun 04
signed int wet(signed int temperature)
{

signed int express;
express = (signed int)(temperature * 500 / 1023);
return express;

}

// 11 jun 04
signed int cheerful(signed int temperature)
{

signed int express;
express = (signed int)((1023 - temperature) * 100 / 1023);
return express;

}

// 5 jun 04
void takeout()
{

int flag, fd, size, opt, banner;
char* name = "/ram0/present";
char keep[7];
char* data;

data = &(keep[0]);
opt = READ_FILE;
size = 7;

fd = open(name, opt);
flag = read(fd, data, size);
banner = close(fd);

// printf("read %d %d %d\n",fd,flag,banner);

second = keep[0];
minute = keep[1];
hour = keep[2];
date = keep[3];
month = keep[4];
year = keep[5] * 100 + keep[6];

}

// 5 jun 04
void publish(signed int temperature)
{

signed int express;
express = (signed int)(temperature * 1000 / 1023 - 300);

if (express == 0){
printf("0.0");
return;
}

if (express < 0){
printf("-");
express = -1 * express;
}

if (express < 10){
printf("0.%d",express);
return;
}

printf("%d",express/10);
printf(".%d",express%10);

}

// 5 jun 04 --- 10 jun 04
signed int thermometer(char channel)
{
signed int temperature;
unsigned short atmospheric;

AD.ADCSR.BIT.ADF = 0;
AD.ADCSR.BIT.ADIE = 0;
AD.ADCSR.BIT.ADST = 0;
AD.ADCSR.BIT.SCAN = 0;
AD.ADCSR.BIT.CKS = 0;

AD.ADCSR.BIT.CH = channel;
AD.ADCSR.BIT.ADST = 1;
while (AD.ADCSR.BIT.ADF == 0);
AD.ADCSR.BIT.ADF = 0;

if(channel == 0 || channel == 4){
atmospheric = AD.ADDRA >> 6;
if (atmospheric < 1){ atmospheric = 1; }
temperature = (signed int)(atmospheric);
}

if(channel == 1 || channel == 5){
atmospheric = AD.ADDRB >> 6;
if (atmospheric < 1){ atmospheric = 1; }
temperature = (signed int)(atmospheric);
}

if(channel == 2 || channel == 6){
atmospheric = AD.ADDRC >> 6;
if (atmospheric < 1){ atmospheric = 1; }
temperature = (signed int)(atmospheric);
}

if(channel == 3 || channel == 7){
atmospheric = AD.ADDRD >> 6;
if (atmospheric < 1){ atmospheric = 1; }
temperature = (signed int)(atmospheric);
}

return temperature;
}

// 5 jun 04 --- 10 jun 04
void getacross(char channel)
{

int flag, fd, size, opt, banner;
char* name;

if (channel == 0){ name = "/ram0/zero"; }
if (channel == 1){ name = "/ram0/first"; }
if (channel == 2){ name = "/ram0/second"; }

if (channel == 8){ name = "/ram0/eighth"; }
if (channel == 9){ name = "/ram0/ninth"; }

signed char keep[MISTRESS];
char* data;

data = &(keep[0]);
opt = READ_FILE;
size = MISTRESS;

fd = open(name, opt);
flag = read(fd, data, size);
banner = close(fd);

int j;

for (j=0; j<MASTER; j++){
heat[j] = keep[j*2] * 100 + keep[j*2+1];
}

}

// 3 jun 04
int main(int argc, char **argv)
{

signed int dampen[MASTER], skylight[MASTER];
signed int dateline[MASTER], timetable[MASTER];
int j;

takeout();

getacross(8);
for (j=0; j<MASTER; j++){
dateline[j] = heat[j];
}

getacross(9);
for (j=0; j<MASTER; j++){
timetable[j] = heat[j];
}

getacross(1);
for (j=0; j<MASTER; j++){
skylight[j] = heat[j];
}

getacross(2);
for (j=0; j<MASTER; j++){
dampen[j] = heat[j];
}

getacross(0);

printf("<html><head><title>H8/3069F</title></head>\n");
printf("<body BGCOLOR=\"#EEFFEE\" VLINK=\"#00CC66\"><br />\n");
printf("<div align=\"center\">\n");

signed int m, n, k, l, justnow;
signed char seal[MASTER];

justnow = thermometer(0);
// justnow = heat[0];

m = justnow;
n = justnow;
k = -1;
l = -1;

for (j=0; j<MASTER; j++){
seal[j] = 0;
if (heat[j] > m){
k = j;
m = heat[j];
}
if (heat[j] < n){
l = j;
n = heat[j];
}
}

if (k >= 0){
seal[k] = 1;
}
if (l >= 0){
seal[l] = -1;
}

printf("<p><table BORDER=1><tr><td>date,<td>time,<td>temperature,");
printf("<td>record,<td>humidity,<td>bright\n");

printf("<tr><td ALIGN=Right>%d/", month);
twins(date);
printf(",<td ALIGN=Right>%d:", hour);
twins(minute);

printf(",\n<td ALIGN=Right>");
publish(justnow);

printf(",<td>,<td ALIGN=Right>%d",wet(thermometer(2)));
printf(",<td ALIGN=Right>%d\n",cheerful(thermometer(1)));

for (j=0; j<MASTER; j++){

printf("<tr><td ALIGN=Right>%d/",dateline[j] / 100);
twins(dateline[j] % 100);

printf(",<td ALIGN=Right>%d:",timetable[j] / 100);
twins(timetable[j] % 100);

printf(",\n<td ALIGN=Right>");
publish(heat[j]);
printf(",<td>");

if (seal[j] == 1){
printf("highest");
}

if (seal[j] == -1){
printf("lowest");
}

printf(",<td ALIGN=Right>%d",wet(dampen[j]));
printf(",<td ALIGN=Right>%d\n",cheerful(skylight[j]));
}

printf("</table><p>set the watch\n");
printf("<p><form action=\"specify\"><table>\n");

printf("<tr><td>year");
printf("<td>month");
printf("<td>date");
printf("<td>hour");
printf("<td>minute");
printf("<td>\n");

printf("<tr><td>");
printf("<input type=\"text\" name=\"year\" VALUE=\"%d\" size=\"5\">\n",year);
printf("<td>");
printf("<input type=\"text\" name=\"month\" VALUE=\"%d\" size=\"3\">\n",month);
printf("<td>");
printf("<input type=\"text\" name=\"date\" VALUE=\"%d\" size=\"3\">\n",date);
printf("<td>");
printf("<input type=\"text\" name=\"hour\" VALUE=\"%d\" size=\"3\">\n",hour);
printf("<td>");
printf("<input type=\"text\" name=\"minute\" VALUE=\"%d\" size=\"3\">\n",minute);
printf("<td>");
printf("<input type=\"submit\" value=\"submit\">\n");
printf("</table></form>\n");
// 13 jun 04
printf("<p><A HREF=\"specify\">initialize</A>\n");
printf("</div></body></html>\n");
}